|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { |
| 6 | + knowledgeDocumentContentSelectionKey, |
| 7 | + knowledgeDocumentFilenameSelectionKey, |
| 8 | + knowledgeDocumentTagNameSelectionKey, |
| 9 | + knowledgeDocumentTagValueSelectionKey, |
| 10 | + parseKnowledgeDocumentTagProvenanceTargets, |
| 11 | +} from '@/lib/knowledge/secret-provenance-selection' |
| 12 | +import { selectKnowledgeDocumentWriteSecretProvenance } from '@/tools/knowledge/secret-provenance' |
| 13 | +import { formatDocumentTagsForAPI, parseDocumentTags } from '@/tools/shared/tags' |
| 14 | + |
| 15 | +/** Mirrors the selection keys the knowledge write route derives from the serialized request body. */ |
| 16 | +function serverSelectionKeys(documentTags: unknown): string[] { |
| 17 | + const { documentTagsData } = formatDocumentTagsForAPI(parseDocumentTags(documentTags)) |
| 18 | + return [ |
| 19 | + knowledgeDocumentFilenameSelectionKey(0), |
| 20 | + knowledgeDocumentContentSelectionKey(0), |
| 21 | + ...parseKnowledgeDocumentTagProvenanceTargets(documentTagsData).flatMap((_tag, tagIndex) => [ |
| 22 | + knowledgeDocumentTagNameSelectionKey(0, tagIndex), |
| 23 | + knowledgeDocumentTagValueSelectionKey(0, tagIndex), |
| 24 | + ]), |
| 25 | + ] |
| 26 | +} |
| 27 | + |
| 28 | +const EMPTY_STRINGIFYING_TAG_VALUES = [ |
| 29 | + ['empty array', []], |
| 30 | + ['array of null', [null]], |
| 31 | + ['array of undefined', [undefined]], |
| 32 | + ['object stringifying to empty', { toString: () => '' }], |
| 33 | +] as const |
| 34 | + |
| 35 | +describe('selectKnowledgeDocumentWriteSecretProvenance', () => { |
| 36 | + it.each(EMPTY_STRINGIFYING_TAG_VALUES)( |
| 37 | + 'agrees with the server target count for a tag value that is a %s', |
| 38 | + (_label, tagValue) => { |
| 39 | + const documentTags = [ |
| 40 | + { tagName: 'kept', value: 'value' }, |
| 41 | + { tagName: 'dropped', value: tagValue }, |
| 42 | + ] |
| 43 | + const selections = selectKnowledgeDocumentWriteSecretProvenance({ |
| 44 | + name: 'doc.md', |
| 45 | + content: 'content', |
| 46 | + documentTags, |
| 47 | + }) |
| 48 | + |
| 49 | + expect(selections.map((selection) => selection.key)).toEqual( |
| 50 | + serverSelectionKeys(documentTags) |
| 51 | + ) |
| 52 | + } |
| 53 | + ) |
| 54 | + |
| 55 | + it('keeps tags whose serialized value is non-empty', () => { |
| 56 | + const documentTags = { alpha: 'one', beta: 2, gamma: false } |
| 57 | + const selections = selectKnowledgeDocumentWriteSecretProvenance({ |
| 58 | + name: 'doc.md', |
| 59 | + content: 'content', |
| 60 | + documentTags, |
| 61 | + }) |
| 62 | + |
| 63 | + expect(selections.map((selection) => selection.key)).toEqual(serverSelectionKeys(documentTags)) |
| 64 | + expect(selections).toHaveLength(8) |
| 65 | + }) |
| 66 | +}) |
0 commit comments