Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

2.3 KB
Raw Blame
const fs = require('fs');
const path = require('path');
const { ResourceType } = require('librechat-data-provider');

/**
 * Maps each ResourceType to the cleanup function name that must appear in
 * deleteUserController's source to prove it is handled during user deletion.
 *
 * When a new ResourceType is added, this test will fail until a corresponding
 * entry is added here (or to NO_USER_CLEANUP_NEEDED) AND the actual cleanup
 * logic is implemented.
 */
const HANDLED_RESOURCE_TYPES = {
  [ResourceType.AGENT]: 'deleteUserAgents',
  [ResourceType.CODE_ENVIRONMENT]: 'deleteUserCodeEnvironments',
  [ResourceType.REMOTE_AGENT]: 'deleteUserAgents',
  [ResourceType.PROMPTGROUP]: 'deleteUserPrompts',
  [ResourceType.MCPSERVER]: 'deleteUserMCPServers',
  [ResourceType.SKILL]: 'deleteUserSkills',
  [ResourceType.SHARED_LINK]: 'deleteAllSharedLinksWithCleanup',
};

/**
 * ResourceTypes that are ACL-tracked but have no per-user deletion semantics
 * (e.g., system resources, public-only). Must be explicitly listed here with
 * a justification to prevent silent omissions.
 */
const NO_USER_CLEANUP_NEEDED = new Set([
  // Example: ResourceType.SYSTEM_TEMPLATE — public/system; not user-owned
]);

describe('deleteUserController - resource type coverage guard', () => {
  let controllerSource;

  beforeAll(() => {
    /** The controller delegates the data cascade to the shared erasure service. */
    controllerSource = [
      path.resolve(__dirname, '../UserController.js'),
      path.resolve(__dirname, '../../../../packages/api/src/user/erasure.ts'),
    ]
      .map((file) => fs.readFileSync(file, 'utf-8'))
      .join('\n');
  });

  test('every ResourceType must have a documented cleanup handler or explicit exclusion', () => {
    const allTypes = Object.values(ResourceType);
    const handledTypes = Object.keys(HANDLED_RESOURCE_TYPES);
    const unhandledTypes = allTypes.filter(
      (t) => !handledTypes.includes(t) && !NO_USER_CLEANUP_NEEDED.has(t),
    );

    expect(unhandledTypes).toEqual([]);
  });

  test('every cleanup handler referenced in HANDLED_RESOURCE_TYPES must appear in the controller source', () => {
    const uniqueHandlers = [...new Set(Object.values(HANDLED_RESOURCE_TYPES))];

    for (const handler of uniqueHandlers) {
      expect(controllerSource).toContain(handler);
    }
  });
});