|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { |
| 6 | + preserveServerOwnedSourceConfig, |
| 7 | + RESERVED_SOURCE_CONFIG_KEYS, |
| 8 | + sanitizeConnectorSourceConfig, |
| 9 | +} from '@/lib/knowledge/connectors/source-config' |
| 10 | + |
| 11 | +describe('sanitizeConnectorSourceConfig', () => { |
| 12 | + /** |
| 13 | + * The tenancy control for `sim`-mode connectors: the engine derives the workspace |
| 14 | + * from the knowledge_base row, so a caller-supplied one must never be persisted |
| 15 | + * where a connector could read it back. |
| 16 | + */ |
| 17 | + it('strips every reserved key a caller could use to widen scope', () => { |
| 18 | + expect( |
| 19 | + sanitizeConnectorSourceConfig({ |
| 20 | + workspaceId: 'victim-ws', |
| 21 | + knowledgeBaseId: 'victim-kb', |
| 22 | + tagSlotMapping: { folderPath: 'tag7' }, |
| 23 | + folderId: 'f-1', |
| 24 | + recursive: 'false', |
| 25 | + }) |
| 26 | + ).toEqual({ folderId: 'f-1', recursive: 'false' }) |
| 27 | + }) |
| 28 | + |
| 29 | + it('covers the whole declared reserved list', () => { |
| 30 | + const everyReserved = Object.fromEntries(RESERVED_SOURCE_CONFIG_KEYS.map((k) => [k, 'x'])) |
| 31 | + expect(sanitizeConnectorSourceConfig(everyReserved)).toEqual({}) |
| 32 | + }) |
| 33 | + |
| 34 | + it('leaves unreserved keys untouched, including falsy values', () => { |
| 35 | + const input = { folderId: '', recursive: 'false', maxFiles: 0 } |
| 36 | + expect(sanitizeConnectorSourceConfig(input)).toEqual(input) |
| 37 | + }) |
| 38 | + |
| 39 | + it('does not mutate the caller object', () => { |
| 40 | + const input = { workspaceId: 'victim-ws', folderId: 'f-1' } |
| 41 | + sanitizeConnectorSourceConfig(input) |
| 42 | + expect(input.workspaceId).toBe('victim-ws') |
| 43 | + }) |
| 44 | +}) |
| 45 | + |
| 46 | +describe('preserveServerOwnedSourceConfig', () => { |
| 47 | + /** |
| 48 | + * Update replaces `sourceConfig` wholesale. Without this, sanitizing would drop |
| 49 | + * `tagSlotMapping` on every edit and the connector would silently stop writing |
| 50 | + * tags — for every connector that declares tagDefinitions, not just the sim ones. |
| 51 | + */ |
| 52 | + it('carries the stored tagSlotMapping across an edit that does not resend it', () => { |
| 53 | + expect( |
| 54 | + preserveServerOwnedSourceConfig( |
| 55 | + { folderId: 'new-folder' }, |
| 56 | + { folderId: 'old-folder', tagSlotMapping: { folderPath: 'tag1' } } |
| 57 | + ) |
| 58 | + ).toEqual({ folderId: 'new-folder', tagSlotMapping: { folderPath: 'tag1' } }) |
| 59 | + }) |
| 60 | + |
| 61 | + /** The stored mapping wins: a caller cannot claim slots it was not allocated. */ |
| 62 | + it('prefers the stored mapping over anything left in the update', () => { |
| 63 | + const result = preserveServerOwnedSourceConfig( |
| 64 | + { tagSlotMapping: { folderPath: 'tag7' } } as Record<string, unknown>, |
| 65 | + { tagSlotMapping: { folderPath: 'tag1' } } |
| 66 | + ) |
| 67 | + expect(result.tagSlotMapping).toEqual({ folderPath: 'tag1' }) |
| 68 | + }) |
| 69 | + |
| 70 | + /** workspaceId/knowledgeBaseId are never persisted, so nothing should resurrect them. */ |
| 71 | + it('does not resurrect keys that are never persisted', () => { |
| 72 | + const result = preserveServerOwnedSourceConfig( |
| 73 | + { folderId: 'f-1' }, |
| 74 | + { workspaceId: 'victim-ws', knowledgeBaseId: 'victim-kb' } |
| 75 | + ) |
| 76 | + expect(result).toEqual({ folderId: 'f-1' }) |
| 77 | + }) |
| 78 | + |
| 79 | + it('tolerates a connector row with no stored config', () => { |
| 80 | + expect(preserveServerOwnedSourceConfig({ folderId: 'f-1' }, null)).toEqual({ folderId: 'f-1' }) |
| 81 | + expect(preserveServerOwnedSourceConfig({ folderId: 'f-1' }, undefined)).toEqual({ |
| 82 | + folderId: 'f-1', |
| 83 | + }) |
| 84 | + }) |
| 85 | +}) |
0 commit comments