|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { readdirSync, readFileSync } from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | +import { |
| 6 | + clusterRecordSchema, |
| 7 | + comparisonObservationSchema, |
| 8 | + createStableId, |
| 9 | + parseClusterRecord, |
| 10 | + parseComparisonObservation, |
| 11 | + parseSignatureRecord, |
| 12 | + renderSubjectSchema, |
| 13 | + sourceAnchorSchema, |
| 14 | + signatureRecordSchema, |
| 15 | +} from './index.js'; |
| 16 | + |
| 17 | +const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); |
| 18 | +const sourceRoot = path.join(packageRoot, 'src'); |
| 19 | + |
| 20 | +const sourceDocument = { |
| 21 | + sourceKey: 'corpus/basic/table.docx', |
| 22 | + originalSha256: 'sha256-original', |
| 23 | + normalizedSha256: 'sha256-normalized', |
| 24 | +}; |
| 25 | + |
| 26 | +const sourceAnchor = { |
| 27 | + sourceNodeId: 'node-table-1', |
| 28 | + occurrenceId: 'occurrence-table-1', |
| 29 | + rawFactIds: ['raw-w-tbl-1'], |
| 30 | + schemaQNames: [ |
| 31 | + { |
| 32 | + qName: 'w:tbl', |
| 33 | + namespaceUri: 'http://schemas.openxmlformats.org/wordprocessingml/2006/main', |
| 34 | + localName: 'tbl', |
| 35 | + }, |
| 36 | + ], |
| 37 | + featureKey: 'tables', |
| 38 | + conceptKey: 'docx.table', |
| 39 | + sourceRef: { |
| 40 | + partUri: '/word/document.xml', |
| 41 | + xpathLikePath: '/w:document[1]/w:body[1]/w:tbl[1]', |
| 42 | + rawFactId: 'raw-w-tbl-1', |
| 43 | + occurrenceId: 'occurrence-table-1', |
| 44 | + }, |
| 45 | + anchorConfidence: 'high', |
| 46 | + flowBlockId: 'flow-table-1', |
| 47 | +}; |
| 48 | + |
| 49 | +const observation = { |
| 50 | + observationId: 'observation_1', |
| 51 | + schemaVersion: 1, |
| 52 | + evidenceLevel: 'document', |
| 53 | + evidenceStrength: 'source-linked', |
| 54 | + mechanism: 'layout-json', |
| 55 | + category: 'table', |
| 56 | + sourceDocument, |
| 57 | + sourcePath: 'basic/table.docx.layout.json', |
| 58 | + sourceOccurrenceId: 'occurrence-table-1', |
| 59 | + sourceAnchors: [sourceAnchor], |
| 60 | + pageNumbers: [1], |
| 61 | + jsonPath: '$.pages[0].blocks[3].width', |
| 62 | + normalizedPath: '$.pages[].blocks[].width', |
| 63 | + pathKind: 'table-width', |
| 64 | + diffKind: 'changed', |
| 65 | + deltaBucket: '+1px', |
| 66 | + rawDiffCount: 4, |
| 67 | + summary: 'Table width changed by about 1px.', |
| 68 | + metrics: { deltaPx: 1 }, |
| 69 | + artifactRefs: [{ path: 'results/layout/basic/table.docx.layout.json.diff.json' }], |
| 70 | +}; |
| 71 | + |
| 72 | +const signature = { |
| 73 | + signatureId: 'signature_table_width_1', |
| 74 | + signatureVersion: 'public.v1', |
| 75 | + familyId: 'table-width', |
| 76 | + observationIds: ['observation_1'], |
| 77 | + category: 'table', |
| 78 | + mechanism: 'layout-json', |
| 79 | + normalizedKey: 'table-width|changed|+1px', |
| 80 | + familyKey: 'table-width|changed', |
| 81 | + pathKind: 'table-width', |
| 82 | + normalizedPath: '$.pages[].blocks[].width', |
| 83 | + diffKind: 'changed', |
| 84 | + deltaBucket: '+1px', |
| 85 | + instanceCount: 1, |
| 86 | + documentCount: 1, |
| 87 | + pageCount: 1, |
| 88 | + exampleObservationId: 'observation_1', |
| 89 | + confidence: 'high', |
| 90 | +}; |
| 91 | + |
| 92 | +const cluster = { |
| 93 | + clusterId: 'cluster_table_width_1', |
| 94 | + signatureIds: ['signature_table_width_1'], |
| 95 | + title: 'Table width changed by about 1px', |
| 96 | + instanceCount: 1, |
| 97 | + documentCount: 1, |
| 98 | + pageCount: 1, |
| 99 | + representativeObservationIds: ['observation_1'], |
| 100 | + evidenceStrength: 'source-linked', |
| 101 | + status: 'new', |
| 102 | + category: 'table', |
| 103 | + mechanism: 'layout-json', |
| 104 | + pathKind: 'table-width', |
| 105 | + allObservationIds: ['observation_1'], |
| 106 | + allInstances: [ |
| 107 | + { |
| 108 | + observationId: 'observation_1', |
| 109 | + signatureId: 'signature_table_width_1', |
| 110 | + documentPath: 'basic/table.docx.layout.json', |
| 111 | + sourcePath: 'basic/table.docx.layout.json', |
| 112 | + sourceOccurrenceId: 'occurrence-table-1', |
| 113 | + sourceNodeIds: ['node-table-1'], |
| 114 | + schemaQNames: ['w:tbl'], |
| 115 | + pageNumbers: [1], |
| 116 | + jsonPath: '$.pages[0].blocks[3].width', |
| 117 | + normalizedPath: '$.pages[].blocks[].width', |
| 118 | + pathKind: 'table-width', |
| 119 | + summary: 'Table width changed by about 1px.', |
| 120 | + }, |
| 121 | + ], |
| 122 | +}; |
| 123 | + |
| 124 | +function stableObservationInput(value: ReturnType<typeof parseComparisonObservation>): unknown { |
| 125 | + const { observationId: _observationId, ...rest } = value; |
| 126 | + return rest; |
| 127 | +} |
| 128 | + |
| 129 | +function listSourceFiles(directory: string): string[] { |
| 130 | + return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => { |
| 131 | + const entryPath = path.join(directory, entry.name); |
| 132 | + if (entry.isDirectory()) return listSourceFiles(entryPath); |
| 133 | + return entry.isFile() && entry.name.endsWith('.ts') && !entry.name.endsWith('.test.ts') ? [entryPath] : []; |
| 134 | + }); |
| 135 | +} |
| 136 | + |
| 137 | +describe('public DOCX evidence contracts', () => { |
| 138 | + it('validates minimal source-linked observations', () => { |
| 139 | + const parsed = parseComparisonObservation(observation); |
| 140 | + const reparsed = comparisonObservationSchema.parse(JSON.parse(JSON.stringify(parsed))); |
| 141 | + |
| 142 | + expect(reparsed).toEqual(parsed); |
| 143 | + expect(sourceAnchorSchema.parse(parsed.sourceAnchors?.[0])).toEqual(sourceAnchor); |
| 144 | + }); |
| 145 | + |
| 146 | + it('validates minimal signature and cluster records', () => { |
| 147 | + const parsedSignature = parseSignatureRecord(signature); |
| 148 | + const parsedCluster = parseClusterRecord(cluster); |
| 149 | + |
| 150 | + expect(signatureRecordSchema.parse(JSON.parse(JSON.stringify(parsedSignature)))).toEqual(parsedSignature); |
| 151 | + expect(clusterRecordSchema.parse(JSON.parse(JSON.stringify(parsedCluster)))).toEqual(parsedCluster); |
| 152 | + }); |
| 153 | + |
| 154 | + it('validates render subjects without exposing analysis policy', () => { |
| 155 | + const parsed = renderSubjectSchema.parse({ |
| 156 | + subjectId: 'subject_candidate', |
| 157 | + role: 'superdoc-candidate', |
| 158 | + rendererId: 'superdoc', |
| 159 | + rendererVersion: '1.30.0-next.8', |
| 160 | + evidenceLevel: 'document', |
| 161 | + artifactRefs: [{ path: 'candidate/layout.json' }], |
| 162 | + }); |
| 163 | + |
| 164 | + expect(parsed.role).toBe('superdoc-candidate'); |
| 165 | + }); |
| 166 | + |
| 167 | + it('produces stable IDs from public artifacts', () => { |
| 168 | + const parsed = parseComparisonObservation(observation); |
| 169 | + const first = createStableId('observation', stableObservationInput(parsed)); |
| 170 | + const second = createStableId('observation', stableObservationInput(parsed)); |
| 171 | + |
| 172 | + expect(first).toBe(second); |
| 173 | + expect(first.startsWith('observation_')).toBe(true); |
| 174 | + }); |
| 175 | + |
| 176 | + it('rejects fragment observations without fragment identity', () => { |
| 177 | + expect( |
| 178 | + comparisonObservationSchema.safeParse({ |
| 179 | + ...observation, |
| 180 | + evidenceLevel: 'fragment', |
| 181 | + }).success, |
| 182 | + ).toBe(false); |
| 183 | + }); |
| 184 | + |
| 185 | + it('keeps public source Worker-safe and free of owner-runtime imports', () => { |
| 186 | + for (const sourceFile of listSourceFiles(sourceRoot)) { |
| 187 | + const text = readFileSync(sourceFile, 'utf8'); |
| 188 | + |
| 189 | + expect(text).not.toMatch(/from ['"]node:/); |
| 190 | + expect(text).not.toMatch(/from ['"].*\.\.\/\.\.\/\.\.\/labs/); |
| 191 | + expect(text).not.toMatch(/from ['"]@superdoc\/(super-editor|painter-dom|layout-engine|pm-adapter)/); |
| 192 | + } |
| 193 | + }); |
| 194 | +}); |
0 commit comments