@specferret/core 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/README.md +62 -62
  2. package/dist/context/index.d.ts +5 -1
  3. package/dist/context/index.d.ts.map +1 -1
  4. package/dist/context/index.js +47 -1
  5. package/dist/context/index.js.map +1 -1
  6. package/dist/extractor/__fixtures__/typescript/generics.d.ts +6 -0
  7. package/dist/extractor/__fixtures__/typescript/generics.d.ts.map +1 -0
  8. package/dist/extractor/__fixtures__/typescript/generics.js +2 -0
  9. package/dist/extractor/__fixtures__/typescript/generics.js.map +1 -0
  10. package/dist/extractor/__fixtures__/typescript/intersections.d.ts +6 -0
  11. package/dist/extractor/__fixtures__/typescript/intersections.d.ts.map +1 -0
  12. package/dist/extractor/__fixtures__/typescript/intersections.js +2 -0
  13. package/dist/extractor/__fixtures__/typescript/intersections.js.map +1 -0
  14. package/dist/extractor/__fixtures__/typescript/optional-nested.d.ts +8 -0
  15. package/dist/extractor/__fixtures__/typescript/optional-nested.d.ts.map +1 -0
  16. package/dist/extractor/__fixtures__/typescript/optional-nested.js +2 -0
  17. package/dist/extractor/__fixtures__/typescript/optional-nested.js.map +1 -0
  18. package/dist/extractor/__fixtures__/typescript/unions.d.ts +5 -0
  19. package/dist/extractor/__fixtures__/typescript/unions.d.ts.map +1 -0
  20. package/dist/extractor/__fixtures__/typescript/unions.js +2 -0
  21. package/dist/extractor/__fixtures__/typescript/unions.js.map +1 -0
  22. package/dist/extractor/__fixtures__/typescript/unsupported-syntax.d.ts +7 -0
  23. package/dist/extractor/__fixtures__/typescript/unsupported-syntax.d.ts.map +1 -0
  24. package/dist/extractor/__fixtures__/typescript/unsupported-syntax.js +2 -0
  25. package/dist/extractor/__fixtures__/typescript/unsupported-syntax.js.map +1 -0
  26. package/dist/extractor/contract-types.d.ts +5 -0
  27. package/dist/extractor/contract-types.d.ts.map +1 -0
  28. package/dist/extractor/contract-types.js +8 -0
  29. package/dist/extractor/contract-types.js.map +1 -0
  30. package/dist/extractor/frontmatter.d.ts +6 -1
  31. package/dist/extractor/frontmatter.d.ts.map +1 -1
  32. package/dist/extractor/frontmatter.js +17 -6
  33. package/dist/extractor/frontmatter.js.map +1 -1
  34. package/dist/extractor/typescript.d.ts +6 -1
  35. package/dist/extractor/typescript.d.ts.map +1 -1
  36. package/dist/extractor/typescript.js +403 -185
  37. package/dist/extractor/typescript.js.map +1 -1
  38. package/dist/extractor/upward-classifier.d.ts +25 -0
  39. package/dist/extractor/upward-classifier.d.ts.map +1 -0
  40. package/dist/extractor/upward-classifier.js +48 -0
  41. package/dist/extractor/upward-classifier.js.map +1 -0
  42. package/dist/extractor/validator.d.ts +9 -0
  43. package/dist/extractor/validator.d.ts.map +1 -1
  44. package/dist/extractor/validator.js +28 -9
  45. package/dist/extractor/validator.js.map +1 -1
  46. package/dist/index.d.ts +14 -12
  47. package/dist/index.d.ts.map +1 -1
  48. package/dist/index.js +14 -12
  49. package/dist/index.js.map +1 -1
  50. package/dist/reconciler/index.d.ts +10 -0
  51. package/dist/reconciler/index.d.ts.map +1 -1
  52. package/dist/reconciler/index.js +76 -0
  53. package/dist/reconciler/index.js.map +1 -1
  54. package/dist/store/sqlite.d.ts +1 -1
  55. package/dist/store/sqlite.d.ts.map +1 -1
  56. package/dist/store/sqlite.js +106 -101
  57. package/dist/store/sqlite.js.map +1 -1
  58. package/dist/store/types.d.ts +6 -2
  59. package/dist/store/types.d.ts.map +1 -1
  60. package/package.json +47 -44
  61. package/src/config.ts +48 -0
  62. package/src/context/index.test.ts +274 -0
  63. package/src/context/index.ts +178 -0
  64. package/src/extractor/__fixtures__/typescript/generics.golden.json +36 -0
  65. package/src/extractor/__fixtures__/typescript/generics.ts +5 -0
  66. package/src/extractor/__fixtures__/typescript/intersections.golden.json +18 -0
  67. package/src/extractor/__fixtures__/typescript/intersections.ts +5 -0
  68. package/src/extractor/__fixtures__/typescript/optional-nested.golden.json +41 -0
  69. package/src/extractor/__fixtures__/typescript/optional-nested.ts +7 -0
  70. package/src/extractor/__fixtures__/typescript/unions.golden.json +30 -0
  71. package/src/extractor/__fixtures__/typescript/unions.ts +4 -0
  72. package/src/extractor/__fixtures__/typescript/unsupported-syntax.golden.json +20 -0
  73. package/src/extractor/__fixtures__/typescript/unsupported-syntax.ts +6 -0
  74. package/src/extractor/contract-types.ts +11 -0
  75. package/src/extractor/frontmatter.test.ts +291 -0
  76. package/src/extractor/frontmatter.ts +87 -0
  77. package/src/extractor/hash.ts +26 -0
  78. package/src/extractor/typescript.test.ts +339 -0
  79. package/src/extractor/typescript.ts +597 -0
  80. package/src/extractor/upward-classifier.test.ts +188 -0
  81. package/src/extractor/upward-classifier.ts +68 -0
  82. package/src/extractor/validator.test.ts +228 -0
  83. package/src/extractor/validator.ts +199 -0
  84. package/src/index.ts +17 -0
  85. package/src/reconciler/import-suggestions.test.ts +101 -0
  86. package/src/reconciler/import-suggestions.ts +157 -0
  87. package/src/reconciler/index.test.ts +248 -0
  88. package/src/reconciler/index.ts +445 -0
  89. package/src/store/factory.ts +54 -0
  90. package/src/store/postgres.ts +84 -0
  91. package/src/store/sqlite.test.ts +328 -0
  92. package/src/store/sqlite.ts +231 -0
  93. package/src/store/types.ts +101 -0
  94. package/src/utils/paths.ts +24 -0
@@ -0,0 +1,339 @@
1
+ import assert from 'node:assert/strict';
2
+ import * as fs from 'node:fs';
3
+ import * as path from 'node:path';
4
+ import { describe, it } from 'bun:test';
5
+ import { CONTRACT_TYPES } from './contract-types.js';
6
+ import { extractContractsFromTypeScript } from './typescript.js';
7
+
8
+ describe('extractContractsFromTypeScript — S28 acceptance criteria', () => {
9
+ it('extracts deterministic contract shapes from exported interfaces without annotations', () => {
10
+ const src = `
11
+ export interface GetUsersResponse {
12
+ id: string;
13
+ email: string;
14
+ active: boolean;
15
+ createdAt: Date;
16
+ }
17
+ `;
18
+
19
+ const first = extractContractsFromTypeScript('src/users.ts', src);
20
+ const second = extractContractsFromTypeScript('src/users.ts', src);
21
+
22
+ assert.equal(first.diagnostics.length, 0);
23
+ assert.equal(JSON.stringify(first), JSON.stringify(second));
24
+ assert.equal(first.contracts.length, 1);
25
+
26
+ const contract = first.contracts[0];
27
+ assert.equal(contract.id, 'type.src/users/getusersresponse');
28
+ assert.equal(contract.type, 'type');
29
+ assert.equal(contract.sourceSymbol, 'GetUsersResponse');
30
+
31
+ const shape = contract.shape as Record<string, unknown>;
32
+ assert.equal(shape.type, 'object');
33
+ });
34
+
35
+ it('extracts enum and function signatures from exported declarations', () => {
36
+ const src = `
37
+ export enum UserRole {
38
+ Admin,
39
+ Viewer,
40
+ }
41
+
42
+ export function getUser(id: string): UserRole {
43
+ return UserRole.Admin;
44
+ }
45
+ `;
46
+
47
+ const result = extractContractsFromTypeScript('src/roles.ts', src);
48
+
49
+ assert.equal(result.contracts.length, 2);
50
+ const enumContract = result.contracts.find((contract) => contract.sourceSymbol === 'UserRole');
51
+ const functionContract = result.contracts.find((contract) => contract.sourceSymbol === 'getUser');
52
+
53
+ assert.ok(enumContract);
54
+ assert.ok(functionContract);
55
+
56
+ const enumShape = enumContract?.shape as Record<string, unknown>;
57
+ assert.equal(enumShape.type, 'string');
58
+
59
+ const functionShape = functionContract?.shape as Record<string, unknown>;
60
+ assert.equal(functionShape.type, 'object');
61
+ });
62
+
63
+ it('records diagnostics for unsupported unions', () => {
64
+ const src = `
65
+ export interface ProfileResponse {
66
+ email: string | null;
67
+ }
68
+ `;
69
+
70
+ const result = extractContractsFromTypeScript('src/profile.ts', src);
71
+
72
+ assert.equal(result.contracts.length, 1);
73
+ assert.ok(result.diagnostics.some((d) => d.includes('Union types are not supported')));
74
+ });
75
+
76
+ it('applies annotation overrides for id and type when present', () => {
77
+ const src = `
78
+ // @ferret-contract: api.GET/profile api
79
+ export interface ProfileResponse {
80
+ email: string;
81
+ }
82
+ `;
83
+ const result = extractContractsFromTypeScript('src/missing.ts', src);
84
+
85
+ assert.equal(result.contracts.length, 1);
86
+ assert.equal(result.contracts[0].id, 'api.GET/profile');
87
+ assert.equal(result.contracts[0].type, 'api');
88
+ });
89
+
90
+ it('accepts all supported annotation contract types', () => {
91
+ for (const contractType of CONTRACT_TYPES) {
92
+ const src = `
93
+ // @ferret-contract: ${contractType}.GET/profile ${contractType}
94
+ export interface ProfileResponse {
95
+ email: string;
96
+ }
97
+ `;
98
+ const result = extractContractsFromTypeScript('src/annotated.ts', src);
99
+ assert.equal(result.errors.length, 0);
100
+ assert.equal(result.contracts.length, 1);
101
+ assert.equal(result.contracts[0].type, contractType);
102
+ }
103
+ });
104
+
105
+ it('reports unmatched annotations', () => {
106
+ const src = `
107
+ // @ferret-contract: api.GET/missing api
108
+ interface MissingContract {
109
+ id: string;
110
+ }
111
+ `;
112
+ const result = extractContractsFromTypeScript('src/missing.ts', src);
113
+
114
+ assert.equal(result.contracts.length, 0);
115
+ assert.ok(result.errors.some((d) => d.includes('did not match')));
116
+ });
117
+
118
+ it('extracts type alias with primitive value', () => {
119
+ const src = `
120
+ export type UserId = string;
121
+ `;
122
+ const result = extractContractsFromTypeScript('src/ids.ts', src);
123
+
124
+ assert.equal(result.contracts.length, 1);
125
+ const contract = result.contracts[0];
126
+ assert.equal(contract.sourceSymbol, 'UserId');
127
+ assert.deepEqual(contract.shape, { type: 'string' });
128
+ });
129
+
130
+ it('extracts enum members with string initialisers', () => {
131
+ const src = `
132
+ export enum Role {
133
+ Admin = "admin",
134
+ Viewer = "viewer",
135
+ }
136
+ `;
137
+ const result = extractContractsFromTypeScript('src/roles.ts', src);
138
+
139
+ assert.equal(result.contracts.length, 1);
140
+ const shape = result.contracts[0].shape as Record<string, unknown>;
141
+ assert.equal(shape.type, 'string');
142
+ assert.deepEqual(shape.enum, ['Admin', 'Viewer']);
143
+ });
144
+
145
+ it('does not mark required methods with optional parameters as optional', () => {
146
+ const src = `
147
+ export interface Service {
148
+ bar: string;
149
+ baz?: string;
150
+ fn(x?: string): void;
151
+ }
152
+ `;
153
+ const result = extractContractsFromTypeScript('src/service.ts', src);
154
+ assert.equal(result.diagnostics.length, 0);
155
+
156
+ const shape = result.contracts[0].shape as {
157
+ required: string[];
158
+ properties: Record<string, unknown>;
159
+ };
160
+ // 'bar' and 'fn' are required; 'baz' is optional
161
+ assert.ok(shape.required.includes('bar'));
162
+ assert.ok(shape.required.includes('fn'));
163
+ assert.ok(!shape.required.includes('baz'));
164
+ });
165
+
166
+ it('does not mark rest parameters as required', () => {
167
+ const src = `
168
+ export function log(message: string, ...args: string[]): void {}
169
+ `;
170
+ const result = extractContractsFromTypeScript('src/log.ts', src);
171
+ assert.equal(result.contracts.length, 1);
172
+
173
+ const shape = result.contracts[0].shape as {
174
+ properties: { params: { required: string[] } };
175
+ };
176
+ const paramsRequired = shape.properties.params.required;
177
+ assert.ok(paramsRequired.includes('message'), 'message should be required');
178
+ assert.ok(!paramsRequired.includes('args'), 'rest param args should not be required');
179
+ });
180
+
181
+ it('extracts function with typed parameters', () => {
182
+ const src = `
183
+ export function getUser(id: string, active: boolean): void {}
184
+ `;
185
+ const result = extractContractsFromTypeScript('src/user.ts', src);
186
+ assert.equal(result.contracts.length, 1);
187
+
188
+ const shape = result.contracts[0].shape as {
189
+ properties: {
190
+ params: { properties: Record<string, unknown>; required: string[] };
191
+ };
192
+ };
193
+ assert.deepEqual(shape.properties.params.properties['id'], {
194
+ type: 'string',
195
+ });
196
+ assert.deepEqual(shape.properties.params.properties['active'], {
197
+ type: 'boolean',
198
+ });
199
+ assert.deepEqual(shape.properties.params.required, ['id', 'active']);
200
+ });
201
+
202
+ it('normalizes absolute paths to deterministic ids', () => {
203
+ const src = `
204
+ export interface ProfileResponse {
205
+ id: string;
206
+ }
207
+ `;
208
+
209
+ const windowsPath = 'C:/Users/alice/work/specferret/src/contracts/profile.ts';
210
+ const unixPath = '/home/bob/work/specferret/src/contracts/profile.ts';
211
+
212
+ const windowsResult = extractContractsFromTypeScript(windowsPath, src);
213
+ const unixResult = extractContractsFromTypeScript(unixPath, src);
214
+
215
+ assert.equal(windowsResult.contracts.length, 1);
216
+ assert.equal(unixResult.contracts.length, 1);
217
+ assert.equal(windowsResult.contracts[0].id, 'type.src/contracts/profile/profileresponse');
218
+ assert.equal(unixResult.contracts[0].id, 'type.src/contracts/profile/profileresponse');
219
+ });
220
+
221
+ it('falls back to last 3 path segments for absolute paths without src/', () => {
222
+ const src = `
223
+ export interface Config {
224
+ debug: boolean;
225
+ }
226
+ `;
227
+
228
+ // Without a recognizable src/ segment the normalizer falls back to the
229
+ // last 3 path segments. This limits machine-specific prefix noise but
230
+ // does NOT guarantee cross-machine determinism (usernames may differ).
231
+ const result = extractContractsFromTypeScript('/home/bob/myproject/config.ts', src);
232
+
233
+ assert.equal(result.contracts.length, 1);
234
+ assert.equal(result.contracts[0].id, 'type.bob/myproject/config/config');
235
+ });
236
+
237
+ it('falls back to core contract type for inferred declarations', () => {
238
+ const src = `
239
+ export interface Team {
240
+ id: string;
241
+ }
242
+ `;
243
+
244
+ const result = extractContractsFromTypeScript('src/team.ts', src);
245
+
246
+ assert.equal(result.contracts.length, 1);
247
+ assert.equal(result.contracts[0].type, 'type');
248
+ });
249
+
250
+ it('deterministically suffixes inferred ids when collisions occur', () => {
251
+ const src = `
252
+ export interface UserProfile {
253
+ id: string;
254
+ }
255
+
256
+ export interface userprofile {
257
+ id: string;
258
+ }
259
+ `;
260
+
261
+ const result = extractContractsFromTypeScript('src/collision.ts', src);
262
+
263
+ assert.equal(result.contracts.length, 2);
264
+ const bySymbol = new Map(result.contracts.map((contract) => [contract.sourceSymbol, contract.id]));
265
+ assert.equal(bySymbol.get('UserProfile'), 'type.src/collision/userprofile');
266
+ assert.equal(bySymbol.get('userprofile'), 'type.src/collision/userprofile-2');
267
+ });
268
+
269
+ it('suffixes inferred id when explicit override already uses base id', () => {
270
+ const src = `
271
+ // @ferret-contract: type.src/collision/userprofile type
272
+ export interface Explicit {
273
+ id: string;
274
+ }
275
+
276
+ export interface UserProfile {
277
+ id: string;
278
+ }
279
+ `;
280
+
281
+ const result = extractContractsFromTypeScript('src/collision.ts', src);
282
+
283
+ assert.equal(result.contracts.length, 2);
284
+ const bySymbol = new Map(result.contracts.map((contract) => [contract.sourceSymbol, contract.id]));
285
+
286
+ assert.equal(bySymbol.get('Explicit'), 'type.src/collision/userprofile');
287
+ assert.equal(bySymbol.get('UserProfile'), 'type.src/collision/userprofile-2');
288
+ assert.ok(result.diagnostics.some((d) => d.includes('Inferred id collision') && d.includes('userprofile-2')));
289
+ });
290
+
291
+ it('matches golden outputs for fixture-driven regression cases', () => {
292
+ const fixtureDir = path.join(import.meta.dir, '__fixtures__', 'typescript');
293
+ const fixtureFiles = fs
294
+ .readdirSync(fixtureDir)
295
+ .filter((name) => name.endsWith('.ts'))
296
+ .sort();
297
+
298
+ assert.ok(fixtureFiles.length > 0, 'expected at least one fixture file');
299
+
300
+ for (const fixtureFile of fixtureFiles) {
301
+ const fixturePath = path.join(fixtureDir, fixtureFile);
302
+ const goldenPath = path.join(fixtureDir, fixtureFile.replace(/\.ts$/, '.golden.json'));
303
+
304
+ assert.ok(fs.existsSync(goldenPath), `missing golden snapshot for ${fixtureFile}`);
305
+
306
+ const source = fs.readFileSync(fixturePath, 'utf-8');
307
+ const logicalPath = `src/fixtures/${fixtureFile}`;
308
+
309
+ const first = extractContractsFromTypeScript(logicalPath, source);
310
+ const second = extractContractsFromTypeScript(logicalPath, source);
311
+ const golden = JSON.parse(fs.readFileSync(goldenPath, 'utf-8'));
312
+
313
+ assert.deepEqual(first, golden, `fixture output mismatch for ${fixtureFile}`);
314
+ assert.deepEqual(second, golden, `fixture output was non-deterministic for ${fixtureFile}`);
315
+ }
316
+ });
317
+
318
+ it('captures unsupported syntax as deterministic diagnostics without crashing', () => {
319
+ const fixtureDir = path.join(import.meta.dir, '__fixtures__', 'typescript');
320
+ const filePath = path.join(fixtureDir, 'unsupported-syntax.ts');
321
+ const source = fs.readFileSync(filePath, 'utf-8');
322
+
323
+ const first = extractContractsFromTypeScript('src/fixtures/unsupported-syntax.ts', source);
324
+ const second = extractContractsFromTypeScript('src/fixtures/unsupported-syntax.ts', source);
325
+
326
+ assert.equal(first.errors.length, 0);
327
+ assert.ok(first.contracts.length > 0);
328
+ assert.ok(
329
+ first.diagnostics.some(
330
+ (d) =>
331
+ d.includes('Unsupported node type') ||
332
+ d.includes('Unsupported object member type') ||
333
+ d.includes('Intersection types are not supported') ||
334
+ d.includes('Union types are not supported'),
335
+ ),
336
+ );
337
+ assert.deepEqual(first.diagnostics, second.diagnostics);
338
+ });
339
+ });