|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { linkSpecAndProse } from './spec-prose-linker.js'; |
| 3 | +import type { DocumentMetadata } from '../types.js'; |
| 4 | + |
| 5 | +function makeDoc(overrides: Partial<DocumentMetadata> & { filePath: string }): DocumentMetadata { |
| 6 | + return { |
| 7 | + title: 'Untitled', |
| 8 | + description: undefined, |
| 9 | + frontmatter: {}, |
| 10 | + blocks: [], |
| 11 | + crossRefs: [], |
| 12 | + links: [], |
| 13 | + wordCount: 0, |
| 14 | + headings: [], |
| 15 | + ...overrides, |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +describe('spec-prose linker', () => { |
| 20 | + it('links prose to spec by endpoint mention in text', () => { |
| 21 | + // Use a title that does NOT match the tag name to isolate endpoint matching |
| 22 | + const docs: DocumentMetadata[] = [ |
| 23 | + makeDoc({ |
| 24 | + filePath: 'docs/api-integration.md', |
| 25 | + title: 'API Integration Guide', |
| 26 | + blocks: [{ |
| 27 | + id: 'b1', |
| 28 | + type: 'guide', |
| 29 | + textContent: 'Use POST /pet to add a new pet.', |
| 30 | + annotations: { audience: 'human' as const }, |
| 31 | + codeBlocks: [], |
| 32 | + sourcePath: 'docs/api-integration.md', |
| 33 | + }], |
| 34 | + }), |
| 35 | + makeDoc({ |
| 36 | + filePath: 'openapi:pet', |
| 37 | + title: 'API — pet', |
| 38 | + blocks: [{ |
| 39 | + id: 'op1', |
| 40 | + type: 'reference', |
| 41 | + headingText: 'POST /pet', |
| 42 | + textContent: 'Add a new pet', |
| 43 | + annotations: { type: 'reference' as const, audience: 'agent' as const }, |
| 44 | + codeBlocks: [], |
| 45 | + sourcePath: 'openapi:pet', |
| 46 | + }], |
| 47 | + }), |
| 48 | + ]; |
| 49 | + |
| 50 | + const links = linkSpecAndProse(docs); |
| 51 | + expect(links.length).toBe(1); |
| 52 | + expect(links[0]!.sourceDocPath).toBe('docs/api-integration.md'); |
| 53 | + expect(links[0]!.targetDocPath).toBe('openapi:pet'); |
| 54 | + expect(links[0]!.type).toBe('composes_with'); |
| 55 | + expect(links[0]!.evidence).toContain('endpoint mention'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('links prose to spec by title similarity', () => { |
| 59 | + const docs: DocumentMetadata[] = [ |
| 60 | + makeDoc({ |
| 61 | + filePath: 'docs/payments.md', |
| 62 | + title: 'Payments', |
| 63 | + blocks: [{ |
| 64 | + id: 'b1', |
| 65 | + type: 'guide', |
| 66 | + textContent: 'This guide covers payment processing.', |
| 67 | + annotations: {}, |
| 68 | + codeBlocks: [], |
| 69 | + sourcePath: 'docs/payments.md', |
| 70 | + }], |
| 71 | + }), |
| 72 | + makeDoc({ |
| 73 | + filePath: 'openapi:payments', |
| 74 | + title: 'API — payments', |
| 75 | + blocks: [], |
| 76 | + }), |
| 77 | + ]; |
| 78 | + |
| 79 | + const links = linkSpecAndProse(docs); |
| 80 | + expect(links.length).toBe(1); |
| 81 | + expect(links[0]!.evidence).toContain('title match'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('returns empty when no specs present', () => { |
| 85 | + const docs: DocumentMetadata[] = [ |
| 86 | + makeDoc({ filePath: 'docs/guide.md', title: 'Guide' }), |
| 87 | + ]; |
| 88 | + expect(linkSpecAndProse(docs)).toEqual([]); |
| 89 | + }); |
| 90 | + |
| 91 | + it('returns empty when no prose present', () => { |
| 92 | + const docs: DocumentMetadata[] = [ |
| 93 | + makeDoc({ filePath: 'openapi:pet', title: 'Pet' }), |
| 94 | + ]; |
| 95 | + expect(linkSpecAndProse(docs)).toEqual([]); |
| 96 | + }); |
| 97 | + |
| 98 | + it('skips openapi:schemas and openapi:security documents', () => { |
| 99 | + const docs: DocumentMetadata[] = [ |
| 100 | + makeDoc({ |
| 101 | + filePath: 'docs/auth.md', |
| 102 | + title: 'Authentication', |
| 103 | + blocks: [{ |
| 104 | + id: 'b1', |
| 105 | + type: 'guide', |
| 106 | + textContent: 'Configure security for your API.', |
| 107 | + annotations: {}, |
| 108 | + codeBlocks: [], |
| 109 | + sourcePath: 'docs/auth.md', |
| 110 | + }], |
| 111 | + }), |
| 112 | + makeDoc({ filePath: 'openapi:schemas', title: 'Schemas' }), |
| 113 | + makeDoc({ filePath: 'openapi:security', title: 'Security' }), |
| 114 | + ]; |
| 115 | + |
| 116 | + const links = linkSpecAndProse(docs); |
| 117 | + expect(links).toEqual([]); |
| 118 | + }); |
| 119 | + |
| 120 | + it('links multiple prose docs to different spec tags', () => { |
| 121 | + const docs: DocumentMetadata[] = [ |
| 122 | + makeDoc({ |
| 123 | + filePath: 'docs/pet-guide.md', |
| 124 | + title: 'Pet Guide', |
| 125 | + blocks: [{ |
| 126 | + id: 'b1', |
| 127 | + type: 'guide', |
| 128 | + textContent: 'Use GET /pet/findByStatus to search.', |
| 129 | + annotations: {}, |
| 130 | + codeBlocks: [], |
| 131 | + sourcePath: 'docs/pet-guide.md', |
| 132 | + }], |
| 133 | + }), |
| 134 | + makeDoc({ |
| 135 | + filePath: 'docs/store-guide.md', |
| 136 | + title: 'Store Guide', |
| 137 | + blocks: [{ |
| 138 | + id: 'b2', |
| 139 | + type: 'guide', |
| 140 | + textContent: 'Check inventory with GET /store/inventory.', |
| 141 | + annotations: {}, |
| 142 | + codeBlocks: [], |
| 143 | + sourcePath: 'docs/store-guide.md', |
| 144 | + }], |
| 145 | + }), |
| 146 | + makeDoc({ |
| 147 | + filePath: 'openapi:pet', |
| 148 | + title: 'API — pet', |
| 149 | + blocks: [{ |
| 150 | + id: 'op1', |
| 151 | + type: 'reference', |
| 152 | + headingText: 'GET /pet/findByStatus', |
| 153 | + textContent: 'Find pets by status', |
| 154 | + annotations: { type: 'reference' as const }, |
| 155 | + codeBlocks: [], |
| 156 | + sourcePath: 'openapi:pet', |
| 157 | + }], |
| 158 | + }), |
| 159 | + makeDoc({ |
| 160 | + filePath: 'openapi:store', |
| 161 | + title: 'API — store', |
| 162 | + blocks: [{ |
| 163 | + id: 'op2', |
| 164 | + type: 'reference', |
| 165 | + headingText: 'GET /store/inventory', |
| 166 | + textContent: 'Returns pet inventories', |
| 167 | + annotations: { type: 'reference' as const }, |
| 168 | + codeBlocks: [], |
| 169 | + sourcePath: 'openapi:store', |
| 170 | + }], |
| 171 | + }), |
| 172 | + ]; |
| 173 | + |
| 174 | + const links = linkSpecAndProse(docs); |
| 175 | + expect(links.length).toBe(2); |
| 176 | + |
| 177 | + const petLink = links.find((l) => l.sourceDocPath === 'docs/pet-guide.md'); |
| 178 | + const storeLink = links.find((l) => l.sourceDocPath === 'docs/store-guide.md'); |
| 179 | + |
| 180 | + expect(petLink?.targetDocPath).toBe('openapi:pet'); |
| 181 | + expect(storeLink?.targetDocPath).toBe('openapi:store'); |
| 182 | + }); |
| 183 | +}); |
0 commit comments