@specferret/core 0.2.0 → 0.3.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.
- package/dist/config.d.ts +3 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js.map +1 -1
- package/dist/contract.d.ts +16 -0
- package/dist/contract.d.ts.map +1 -0
- package/dist/contract.js +17 -0
- package/dist/contract.js.map +1 -0
- package/dist/extractor/__fixtures__/cross-file-id.fixture.d.ts +15 -0
- package/dist/extractor/__fixtures__/cross-file-id.fixture.d.ts.map +1 -0
- package/dist/extractor/__fixtures__/cross-file-id.fixture.js +9 -0
- package/dist/extractor/__fixtures__/cross-file-id.fixture.js.map +1 -0
- package/dist/extractor/__fixtures__/cross-file-unresolved.fixture.d.ts +14 -0
- package/dist/extractor/__fixtures__/cross-file-unresolved.fixture.d.ts.map +1 -0
- package/dist/extractor/__fixtures__/cross-file-unresolved.fixture.js +9 -0
- package/dist/extractor/__fixtures__/cross-file-unresolved.fixture.js.map +1 -0
- package/dist/extractor/__fixtures__/empty.fixture.d.ts +6 -0
- package/dist/extractor/__fixtures__/empty.fixture.d.ts.map +1 -0
- package/dist/extractor/__fixtures__/empty.fixture.js +5 -0
- package/dist/extractor/__fixtures__/empty.fixture.js.map +1 -0
- package/dist/extractor/__fixtures__/external-no-id.fixture.d.ts +8 -0
- package/dist/extractor/__fixtures__/external-no-id.fixture.d.ts.map +1 -0
- package/dist/extractor/__fixtures__/external-no-id.fixture.js +7 -0
- package/dist/extractor/__fixtures__/external-no-id.fixture.js.map +1 -0
- package/dist/extractor/__fixtures__/external-with-id.fixture.d.ts +9 -0
- package/dist/extractor/__fixtures__/external-with-id.fixture.d.ts.map +1 -0
- package/dist/extractor/__fixtures__/external-with-id.fixture.js +8 -0
- package/dist/extractor/__fixtures__/external-with-id.fixture.js.map +1 -0
- package/dist/extractor/__fixtures__/mixed.fixture.d.ts +24 -0
- package/dist/extractor/__fixtures__/mixed.fixture.d.ts.map +1 -0
- package/dist/extractor/__fixtures__/mixed.fixture.js +15 -0
- package/dist/extractor/__fixtures__/mixed.fixture.js.map +1 -0
- package/dist/extractor/__fixtures__/one-contract.fixture.d.ts +10 -0
- package/dist/extractor/__fixtures__/one-contract.fixture.d.ts.map +1 -0
- package/dist/extractor/__fixtures__/one-contract.fixture.js +10 -0
- package/dist/extractor/__fixtures__/one-contract.fixture.js.map +1 -0
- package/dist/extractor/__fixtures__/optional-fields.fixture.d.ts +10 -0
- package/dist/extractor/__fixtures__/optional-fields.fixture.d.ts.map +1 -0
- package/dist/extractor/__fixtures__/optional-fields.fixture.js +10 -0
- package/dist/extractor/__fixtures__/optional-fields.fixture.js.map +1 -0
- package/dist/extractor/__fixtures__/same-file-consumes.fixture.d.ts +20 -0
- package/dist/extractor/__fixtures__/same-file-consumes.fixture.d.ts.map +1 -0
- package/dist/extractor/__fixtures__/same-file-consumes.fixture.js +11 -0
- package/dist/extractor/__fixtures__/same-file-consumes.fixture.js.map +1 -0
- package/dist/extractor/__fixtures__/three-unresolved.fixture.d.ts +38 -0
- package/dist/extractor/__fixtures__/three-unresolved.fixture.d.ts.map +1 -0
- package/dist/extractor/__fixtures__/three-unresolved.fixture.js +21 -0
- package/dist/extractor/__fixtures__/three-unresolved.fixture.js.map +1 -0
- package/dist/extractor/__fixtures__/throws-on-import.fixture.d.ts +2 -0
- package/dist/extractor/__fixtures__/throws-on-import.fixture.d.ts.map +1 -0
- package/dist/extractor/__fixtures__/throws-on-import.fixture.js +5 -0
- package/dist/extractor/__fixtures__/throws-on-import.fixture.js.map +1 -0
- package/dist/extractor/frontmatter.d.ts +1 -1
- package/dist/extractor/frontmatter.d.ts.map +1 -1
- package/dist/extractor/typescript-contract.d.ts +3 -0
- package/dist/extractor/typescript-contract.d.ts.map +1 -0
- package/dist/extractor/typescript-contract.js +67 -0
- package/dist/extractor/typescript-contract.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/status/index.d.ts +20 -0
- package/dist/status/index.d.ts.map +1 -0
- package/dist/status/index.js +46 -0
- package/dist/status/index.js.map +1 -0
- package/package.json +3 -2
- package/src/config.ts +3 -0
- package/src/contract.test.ts +99 -0
- package/src/contract.ts +38 -0
- package/src/extractor/__fixtures__/cross-file-id.fixture.ts +9 -0
- package/src/extractor/__fixtures__/cross-file-unresolved.fixture.ts +9 -0
- package/src/extractor/__fixtures__/empty.fixture.ts +4 -0
- package/src/extractor/__fixtures__/external-no-id.fixture.ts +7 -0
- package/src/extractor/__fixtures__/external-with-id.fixture.ts +8 -0
- package/src/extractor/__fixtures__/mixed.fixture.ts +17 -0
- package/src/extractor/__fixtures__/one-contract.fixture.ts +10 -0
- package/src/extractor/__fixtures__/optional-fields.fixture.ts +10 -0
- package/src/extractor/__fixtures__/same-file-consumes.fixture.ts +12 -0
- package/src/extractor/__fixtures__/three-unresolved.fixture.ts +24 -0
- package/src/extractor/__fixtures__/throws-on-import.fixture.ts +3 -0
- package/src/extractor/frontmatter.ts +1 -1
- package/src/extractor/typescript-contract.test.ts +143 -0
- package/src/extractor/typescript-contract.ts +76 -0
- package/src/index.ts +3 -0
- package/src/status/index.ts +68 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { DBStore, ContractStatus } from '../store/types.js';
|
|
2
|
+
export type StatusContractEntry = {
|
|
3
|
+
id: string;
|
|
4
|
+
status: ContractStatus;
|
|
5
|
+
driftClass: 'breaking' | 'stable' | 'roadmap';
|
|
6
|
+
dependentCount: number;
|
|
7
|
+
dependents: string[];
|
|
8
|
+
};
|
|
9
|
+
export type StatusReport = {
|
|
10
|
+
version: '2.0';
|
|
11
|
+
timestamp: string;
|
|
12
|
+
total: number;
|
|
13
|
+
stable: number;
|
|
14
|
+
roadmap: number;
|
|
15
|
+
needsReview: number;
|
|
16
|
+
contracts: StatusContractEntry[];
|
|
17
|
+
};
|
|
18
|
+
export declare function buildStatusReport(store: DBStore): Promise<StatusReport>;
|
|
19
|
+
export declare function buildStatusMarkdown(report: StatusReport): string;
|
|
20
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/status/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAEjE,MAAM,MAAM,mBAAmB,GAAG;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,cAAc,CAAC;IACvB,UAAU,EAAE,UAAU,GAAG,QAAQ,GAAG,SAAS,CAAC;IAC9C,cAAc,EAAE,MAAM,CAAC;IACvB,UAAU,EAAE,MAAM,EAAE,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IACzB,OAAO,EAAE,KAAK,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,mBAAmB,EAAE,CAAC;CAClC,CAAC;AAEF,wBAAsB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,YAAY,CAAC,CAkC7E;AAED,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,YAAY,GAAG,MAAM,CAWhE"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export async function buildStatusReport(store) {
|
|
2
|
+
const [contracts, nodes, deps] = await Promise.all([store.getContracts(), store.getNodes(), store.getDependencies()]);
|
|
3
|
+
const nodeById = new Map(nodes.map((n) => [n.id, n]));
|
|
4
|
+
const dependentsByContractId = new Map();
|
|
5
|
+
for (const dep of deps) {
|
|
6
|
+
const node = nodeById.get(dep.source_node_id);
|
|
7
|
+
if (!node)
|
|
8
|
+
continue;
|
|
9
|
+
const list = dependentsByContractId.get(dep.target_contract_id) ?? [];
|
|
10
|
+
if (!list.includes(node.file_path))
|
|
11
|
+
list.push(node.file_path);
|
|
12
|
+
dependentsByContractId.set(dep.target_contract_id, list);
|
|
13
|
+
}
|
|
14
|
+
const entries = contracts.map((c) => {
|
|
15
|
+
const dependents = dependentsByContractId.get(c.id) ?? [];
|
|
16
|
+
return {
|
|
17
|
+
id: c.id,
|
|
18
|
+
status: c.status,
|
|
19
|
+
driftClass: c.status === 'needs-review' ? 'breaking' : c.status === 'roadmap' ? 'roadmap' : 'stable',
|
|
20
|
+
dependentCount: dependents.length,
|
|
21
|
+
dependents,
|
|
22
|
+
};
|
|
23
|
+
});
|
|
24
|
+
return {
|
|
25
|
+
version: '2.0',
|
|
26
|
+
timestamp: new Date().toISOString(),
|
|
27
|
+
total: contracts.length,
|
|
28
|
+
stable: contracts.filter((c) => c.status === 'stable').length,
|
|
29
|
+
roadmap: contracts.filter((c) => c.status === 'roadmap').length,
|
|
30
|
+
needsReview: contracts.filter((c) => c.status === 'needs-review').length,
|
|
31
|
+
contracts: entries,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export function buildStatusMarkdown(report) {
|
|
35
|
+
const lines = [
|
|
36
|
+
'# Contract Status',
|
|
37
|
+
'',
|
|
38
|
+
`Generated: ${report.timestamp}`,
|
|
39
|
+
'',
|
|
40
|
+
'| Contract | Status | Drift Class | Dependents |',
|
|
41
|
+
'| --- | --- | --- | --- |',
|
|
42
|
+
...report.contracts.map((c) => `| ${c.id} | ${c.status} | ${c.driftClass} | ${c.dependentCount} |`),
|
|
43
|
+
];
|
|
44
|
+
return lines.join('\n') + '\n';
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/status/index.ts"],"names":[],"mappings":"AAoBA,MAAM,CAAC,KAAK,UAAU,iBAAiB,CAAC,KAAc;IACpD,MAAM,CAAC,SAAS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": "0.3.0",
|
|
4
4
|
"description": "SpecFerret core engine — spec drift detection.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -29,7 +29,8 @@
|
|
|
29
29
|
"gray-matter": "^4.0.3",
|
|
30
30
|
"tree-sitter": "^0.22.4",
|
|
31
31
|
"tree-sitter-typescript": "^0.23.2",
|
|
32
|
-
"zod": "^3.22.4"
|
|
32
|
+
"zod": "^3.22.4",
|
|
33
|
+
"zod-to-json-schema": "^3.25.2"
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
|
35
36
|
"@types/bun": "^1.3.11",
|
package/src/config.ts
CHANGED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { defineContract, isContract } from './contract.js';
|
|
4
|
+
|
|
5
|
+
describe('isContract', () => {
|
|
6
|
+
it('returns true for a minimal valid object', () => {
|
|
7
|
+
expect(isContract({ value: 'x', output: {} })).toBe(true);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('returns true for object with id set', () => {
|
|
11
|
+
expect(isContract({ id: 'auth.jwt', value: 'x', output: {} })).toBe(true);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('returns true for object without id — id is optional and not checked', () => {
|
|
15
|
+
expect(isContract({ value: 'x', output: { name: z.string() } })).toBe(true);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('returns false for null', () => {
|
|
19
|
+
expect(isContract(null)).toBe(false);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('returns false for undefined', () => {
|
|
23
|
+
expect(isContract(undefined)).toBe(false);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('returns false for a number', () => {
|
|
27
|
+
expect(isContract(42)).toBe(false);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('returns false for a string', () => {
|
|
31
|
+
expect(isContract('string')).toBe(false);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('returns false for object missing value', () => {
|
|
35
|
+
expect(isContract({ output: {} })).toBe(false);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('returns false for object missing output', () => {
|
|
39
|
+
expect(isContract({ value: 'x' })).toBe(false);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('returns false when value is not a string', () => {
|
|
43
|
+
expect(isContract({ value: 42, output: {} })).toBe(false);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('returns false when output is null', () => {
|
|
47
|
+
expect(isContract({ value: 'x', output: null })).toBe(false);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('returns false when output is an array', () => {
|
|
51
|
+
expect(isContract({ value: 'x', output: [] })).toBe(false);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe('defineContract', () => {
|
|
56
|
+
it('returns the exact object passed to it when valid', () => {
|
|
57
|
+
const contract = { value: 'x', output: {} };
|
|
58
|
+
expect(defineContract(contract)).toBe(contract);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('does not throw when id is omitted', () => {
|
|
62
|
+
expect(() => defineContract({ value: 'x', output: {} })).not.toThrow();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('does not throw when id is explicitly undefined', () => {
|
|
66
|
+
expect(() => defineContract({ id: undefined, value: 'x', output: {} })).not.toThrow();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('does not throw when id is a valid non-empty string', () => {
|
|
70
|
+
expect(() => defineContract({ id: 'auth.jwt', value: 'x', output: {} })).not.toThrow();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('throws when id is an empty string', () => {
|
|
74
|
+
expect(() => defineContract({ id: '', value: 'x', output: {} })).toThrow(
|
|
75
|
+
'id must be a non-empty string',
|
|
76
|
+
);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('throws when id is whitespace only', () => {
|
|
80
|
+
expect(() => defineContract({ id: ' ', value: 'x', output: {} })).toThrow(
|
|
81
|
+
'id must be a non-empty string',
|
|
82
|
+
);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('enforces typed invariant argument — compile-time check via typed fixture', () => {
|
|
86
|
+
// If r is not typed as { name: string }, the _: string assignment below is a compile error.
|
|
87
|
+
const contract = defineContract({
|
|
88
|
+
value: 'typed invariant test',
|
|
89
|
+
output: { name: z.string() },
|
|
90
|
+
invariants: [
|
|
91
|
+
(r) => {
|
|
92
|
+
const _: string = r.name;
|
|
93
|
+
return _.length > 0;
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
});
|
|
97
|
+
expect(isContract(contract)).toBe(true);
|
|
98
|
+
});
|
|
99
|
+
});
|
package/src/contract.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
export interface Contract<
|
|
4
|
+
T extends Record<string, z.ZodTypeAny> = Record<string, z.ZodTypeAny>,
|
|
5
|
+
> {
|
|
6
|
+
id?: string;
|
|
7
|
+
value: string;
|
|
8
|
+
output: T;
|
|
9
|
+
invariants?: Array<(r: z.infer<z.ZodObject<T>>) => boolean>;
|
|
10
|
+
consumes?: Contract[];
|
|
11
|
+
forbids?: string[];
|
|
12
|
+
status?: 'complete' | 'active' | 'pending';
|
|
13
|
+
closedBy?: string;
|
|
14
|
+
closedWhen?: string;
|
|
15
|
+
dependsOn?: Contract[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function defineContract<T extends Record<string, z.ZodTypeAny>>(
|
|
19
|
+
contract: Contract<T>,
|
|
20
|
+
): Contract<T> {
|
|
21
|
+
if ('id' in contract && contract.id !== undefined && contract.id.trim() === '') {
|
|
22
|
+
throw new Error('defineContract: id must be a non-empty string if provided');
|
|
23
|
+
}
|
|
24
|
+
return contract;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function isContract(value: unknown): value is Contract {
|
|
28
|
+
return (
|
|
29
|
+
typeof value === 'object' &&
|
|
30
|
+
value !== null &&
|
|
31
|
+
'value' in value &&
|
|
32
|
+
typeof (value as Record<string, unknown>).value === 'string' &&
|
|
33
|
+
'output' in value &&
|
|
34
|
+
typeof (value as Record<string, unknown>).output === 'object' &&
|
|
35
|
+
(value as Record<string, unknown>).output !== null &&
|
|
36
|
+
!Array.isArray((value as Record<string, unknown>).output)
|
|
37
|
+
);
|
|
38
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { externalWithId } from './external-with-id.fixture.js';
|
|
3
|
+
|
|
4
|
+
// Consumes an external contract that has an explicit id — resolves via c.id
|
|
5
|
+
export const myContract = {
|
|
6
|
+
value: 'Contract consuming known external',
|
|
7
|
+
output: { result: z.string() },
|
|
8
|
+
consumes: [externalWithId],
|
|
9
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { externalNoId } from './external-no-id.fixture.js';
|
|
3
|
+
|
|
4
|
+
// Consumes an external contract with no id — triggers exactly one warning
|
|
5
|
+
export const myContract = {
|
|
6
|
+
value: 'Contract consuming unresolvable external',
|
|
7
|
+
output: { result: z.string() },
|
|
8
|
+
consumes: [externalNoId],
|
|
9
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
export const validContract = {
|
|
4
|
+
value: 'valid',
|
|
5
|
+
output: { name: z.string() },
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export const anotherValid = {
|
|
9
|
+
value: 'another valid',
|
|
10
|
+
output: { count: z.number() },
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
// These should be skipped by isContract
|
|
14
|
+
export const notAContract = 42;
|
|
15
|
+
export const alsoNotAContract = 'just a string';
|
|
16
|
+
export const missingOutput = { value: 'oops' };
|
|
17
|
+
export const missingValue = { output: { x: z.string() } };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
export const baseContract = {
|
|
4
|
+
value: 'Base contract',
|
|
5
|
+
output: { token: z.string() },
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export const dependentContract = {
|
|
9
|
+
value: 'Dependent contract that consumes baseContract',
|
|
10
|
+
output: { result: z.string() },
|
|
11
|
+
consumes: [baseContract],
|
|
12
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
// Three separate unresolvable external references (not exported, no id)
|
|
4
|
+
const ext1 = { value: 'ext1', output: { a: z.string() } };
|
|
5
|
+
const ext2 = { value: 'ext2', output: { b: z.string() } };
|
|
6
|
+
const ext3 = { value: 'ext3', output: { c: z.string() } };
|
|
7
|
+
|
|
8
|
+
export const contractA = {
|
|
9
|
+
value: 'Contract A',
|
|
10
|
+
output: { x: z.string() },
|
|
11
|
+
consumes: [ext1],
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const contractB = {
|
|
15
|
+
value: 'Contract B',
|
|
16
|
+
output: { y: z.string() },
|
|
17
|
+
consumes: [ext2],
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const contractC = {
|
|
21
|
+
value: 'Contract C',
|
|
22
|
+
output: { zField: z.string() },
|
|
23
|
+
consumes: [ext3],
|
|
24
|
+
};
|
|
@@ -20,7 +20,7 @@ export interface ExtractionResult {
|
|
|
20
20
|
/** TypeScript symbol name (code-first contracts only). */
|
|
21
21
|
sourceSymbol?: string;
|
|
22
22
|
}>;
|
|
23
|
-
extractedBy: 'gray-matter' | 'tree-sitter';
|
|
23
|
+
extractedBy: 'gray-matter' | 'tree-sitter' | 'typescript';
|
|
24
24
|
extractedAt: number; // unix ms
|
|
25
25
|
warning?: 'no-frontmatter';
|
|
26
26
|
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { describe, expect, it, spyOn } from 'bun:test';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { extractFromContractFile } from './typescript-contract.js';
|
|
5
|
+
|
|
6
|
+
const fixtures = (name: string) => join(import.meta.dir, '__fixtures__', name);
|
|
7
|
+
|
|
8
|
+
describe('extractFromContractFile', () => {
|
|
9
|
+
it('single valid export → one contract with correct id, shape, shape_hash, imports', async () => {
|
|
10
|
+
const result = await extractFromContractFile(fixtures('one-contract.fixture.ts'));
|
|
11
|
+
|
|
12
|
+
assert.equal(result.contracts.length, 1);
|
|
13
|
+
assert.equal(result.contracts[0].id, 'userContract');
|
|
14
|
+
assert.equal(typeof result.contracts[0].shape, 'object');
|
|
15
|
+
assert.equal(typeof result.contracts[0].shape_hash, 'string');
|
|
16
|
+
assert.equal(result.contracts[0].shape_hash.length, 64);
|
|
17
|
+
assert.deepEqual(result.contracts[0].imports, []);
|
|
18
|
+
assert.equal(result.warning, undefined);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('multiple exports — only valid contracts extracted, non-contracts skipped', async () => {
|
|
22
|
+
const result = await extractFromContractFile(fixtures('mixed.fixture.ts'));
|
|
23
|
+
|
|
24
|
+
const ids = result.contracts.map((c) => c.id);
|
|
25
|
+
assert.equal(result.contracts.length, 2);
|
|
26
|
+
assert.ok(ids.includes('validContract'));
|
|
27
|
+
assert.ok(ids.includes('anotherValid'));
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('no ferret contracts → empty contracts array + warning: no-frontmatter', async () => {
|
|
31
|
+
const result = await extractFromContractFile(fixtures('empty.fixture.ts'));
|
|
32
|
+
|
|
33
|
+
assert.equal(result.contracts.length, 0);
|
|
34
|
+
assert.equal(result.warning, 'no-frontmatter');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('extractedBy is always "typescript"', async () => {
|
|
38
|
+
const a = await extractFromContractFile(fixtures('one-contract.fixture.ts'));
|
|
39
|
+
const b = await extractFromContractFile(fixtures('empty.fixture.ts'));
|
|
40
|
+
|
|
41
|
+
assert.equal(a.extractedBy, 'typescript');
|
|
42
|
+
assert.equal(b.extractedBy, 'typescript');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('extractedAt is a unix ms timestamp within 1s of Date.now()', async () => {
|
|
46
|
+
const before = Date.now();
|
|
47
|
+
const result = await extractFromContractFile(fixtures('one-contract.fixture.ts'));
|
|
48
|
+
const after = Date.now();
|
|
49
|
+
|
|
50
|
+
assert.ok(result.extractedAt >= before - 1000);
|
|
51
|
+
assert.ok(result.extractedAt <= after + 1000);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('shape hash is deterministic — two runs on same file produce identical hash', async () => {
|
|
55
|
+
// Clear module cache by using a unique query approach isn't possible cleanly in Bun,
|
|
56
|
+
// but the hash must be stable across calls even with the cached module.
|
|
57
|
+
const r1 = await extractFromContractFile(fixtures('one-contract.fixture.ts'));
|
|
58
|
+
const r2 = await extractFromContractFile(fixtures('one-contract.fixture.ts'));
|
|
59
|
+
|
|
60
|
+
assert.equal(r1.contracts[0].shape_hash, r2.contracts[0].shape_hash);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('zod schema with optional fields extracts without throwing', async () => {
|
|
64
|
+
await assert.doesNotReject(() =>
|
|
65
|
+
extractFromContractFile(fixtures('optional-fields.fixture.ts')),
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
const result = await extractFromContractFile(fixtures('optional-fields.fixture.ts'));
|
|
69
|
+
assert.equal(result.contracts.length, 1);
|
|
70
|
+
assert.equal(result.contracts[0].id, 'optionalFieldsContract');
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('same-file consumes reference resolves to the correct export name', async () => {
|
|
74
|
+
const result = await extractFromContractFile(fixtures('same-file-consumes.fixture.ts'));
|
|
75
|
+
|
|
76
|
+
const dependent = result.contracts.find((c) => c.id === 'dependentContract');
|
|
77
|
+
assert.ok(dependent, 'dependentContract not found in result');
|
|
78
|
+
assert.deepEqual(dependent.imports, ['baseContract']);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('cross-file reference with explicit id resolves to c.id', async () => {
|
|
82
|
+
const result = await extractFromContractFile(fixtures('cross-file-id.fixture.ts'));
|
|
83
|
+
|
|
84
|
+
assert.equal(result.contracts.length, 1);
|
|
85
|
+
assert.deepEqual(result.contracts[0].imports, ['external.known.id']);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('cross-file reference without id emits exactly one warning and returns [unresolved]', async () => {
|
|
89
|
+
const spy = spyOn(process.stderr, 'write').mockImplementation(() => true);
|
|
90
|
+
try {
|
|
91
|
+
const result = await extractFromContractFile(fixtures('cross-file-unresolved.fixture.ts'));
|
|
92
|
+
|
|
93
|
+
expect(spy.mock.calls.length).toBe(1);
|
|
94
|
+
assert.deepEqual(result.contracts[0].imports, ['[unresolved]']);
|
|
95
|
+
} finally {
|
|
96
|
+
spy.mockRestore();
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('three contracts each with unresolvable cross-file ref → three warnings emitted', async () => {
|
|
101
|
+
const spy = spyOn(process.stderr, 'write').mockImplementation(() => true);
|
|
102
|
+
try {
|
|
103
|
+
const result = await extractFromContractFile(fixtures('three-unresolved.fixture.ts'));
|
|
104
|
+
|
|
105
|
+
expect(spy.mock.calls.length).toBe(3);
|
|
106
|
+
assert.equal(result.contracts.length, 3);
|
|
107
|
+
result.contracts.forEach((c) => {
|
|
108
|
+
assert.deepEqual(c.imports, ['[unresolved]']);
|
|
109
|
+
});
|
|
110
|
+
} finally {
|
|
111
|
+
spy.mockRestore();
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('filePath and fileType are set correctly in the result', async () => {
|
|
116
|
+
const filePath = fixtures('one-contract.fixture.ts');
|
|
117
|
+
const result = await extractFromContractFile(filePath);
|
|
118
|
+
|
|
119
|
+
assert.equal(result.filePath, filePath);
|
|
120
|
+
assert.equal(result.fileType, 'code');
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('fileType is "code" even when no contracts found', async () => {
|
|
124
|
+
const result = await extractFromContractFile(fixtures('empty.fixture.ts'));
|
|
125
|
+
|
|
126
|
+
assert.equal(result.fileType, 'code');
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('each contract has sourceFile and sourceSymbol populated', async () => {
|
|
130
|
+
const filePath = fixtures('one-contract.fixture.ts');
|
|
131
|
+
const result = await extractFromContractFile(filePath);
|
|
132
|
+
|
|
133
|
+
assert.equal(result.contracts[0].sourceFile, filePath);
|
|
134
|
+
assert.equal(result.contracts[0].sourceSymbol, 'userContract');
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('module that throws at top-level causes extractFromContractFile to reject', async () => {
|
|
138
|
+
await assert.rejects(
|
|
139
|
+
() => extractFromContractFile(fixtures('throws-on-import.fixture.ts')),
|
|
140
|
+
/intentional module-level throw/,
|
|
141
|
+
);
|
|
142
|
+
});
|
|
143
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// Extractor layer — turns a .contract.ts file into an ExtractionResult.
|
|
2
|
+
// Uses Bun's native import() — no ts-morph, no AST, no compile step.
|
|
3
|
+
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
6
|
+
import { isContract } from '../contract.js';
|
|
7
|
+
import { hashSchema } from './hash.js';
|
|
8
|
+
import type { ExtractionResult } from './frontmatter.js';
|
|
9
|
+
|
|
10
|
+
export async function extractFromContractFile(filePath: string): Promise<ExtractionResult> {
|
|
11
|
+
const mod = (await import(filePath)) as Record<string, unknown>;
|
|
12
|
+
|
|
13
|
+
// Pass 1: build Map<contract object reference → export name> for same-file resolution
|
|
14
|
+
const exportNameMap = new Map<object, string>();
|
|
15
|
+
for (const [exportName, exportValue] of Object.entries(mod)) {
|
|
16
|
+
if (isContract(exportValue)) {
|
|
17
|
+
exportNameMap.set(exportValue, exportName);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (exportNameMap.size === 0) {
|
|
22
|
+
return {
|
|
23
|
+
filePath,
|
|
24
|
+
fileType: 'code',
|
|
25
|
+
contracts: [],
|
|
26
|
+
extractedBy: 'typescript',
|
|
27
|
+
extractedAt: Date.now(),
|
|
28
|
+
warning: 'no-frontmatter',
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const contracts: ExtractionResult['contracts'] = [];
|
|
33
|
+
|
|
34
|
+
for (const [exportName, exportValue] of Object.entries(mod)) {
|
|
35
|
+
if (!isContract(exportValue)) continue;
|
|
36
|
+
|
|
37
|
+
const shape = zodToJsonSchema(z.object(exportValue.output), { $refStrategy: 'none' });
|
|
38
|
+
const shape_hash = hashSchema(shape);
|
|
39
|
+
|
|
40
|
+
// Pass 2: resolve consumes → import IDs
|
|
41
|
+
const imports: string[] = [];
|
|
42
|
+
if (exportValue.consumes) {
|
|
43
|
+
for (const consumed of exportValue.consumes) {
|
|
44
|
+
const samefile = exportNameMap.get(consumed);
|
|
45
|
+
if (samefile !== undefined) {
|
|
46
|
+
imports.push(samefile);
|
|
47
|
+
} else if (consumed.id !== undefined) {
|
|
48
|
+
imports.push(consumed.id);
|
|
49
|
+
} else {
|
|
50
|
+
process.stderr.write(
|
|
51
|
+
`[specferret] warning: unresolvable consumes reference in '${filePath}' — export '${exportName}' references a contract with no id and no matching same-file export\n`,
|
|
52
|
+
);
|
|
53
|
+
imports.push('[unresolved]');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
contracts.push({
|
|
59
|
+
id: exportName,
|
|
60
|
+
type: 'type',
|
|
61
|
+
shape,
|
|
62
|
+
shape_hash,
|
|
63
|
+
imports,
|
|
64
|
+
sourceFile: filePath,
|
|
65
|
+
sourceSymbol: exportName,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
filePath,
|
|
71
|
+
fileType: 'code',
|
|
72
|
+
contracts,
|
|
73
|
+
extractedBy: 'typescript',
|
|
74
|
+
extractedAt: Date.now(),
|
|
75
|
+
};
|
|
76
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// Do not export llm-fallback — dynamic import only, never a default export.
|
|
3
3
|
|
|
4
4
|
export * from './extractor/frontmatter.js';
|
|
5
|
+
export * from './extractor/typescript-contract.js';
|
|
5
6
|
export * from './extractor/typescript.js';
|
|
6
7
|
export * from './extractor/validator.js';
|
|
7
8
|
export * from './extractor/contract-types.js';
|
|
@@ -15,3 +16,5 @@ export * from './reconciler/index.js';
|
|
|
15
16
|
export * from './reconciler/import-suggestions.js';
|
|
16
17
|
export * from './config.js';
|
|
17
18
|
export * from './utils/paths.js';
|
|
19
|
+
export * from './contract.js';
|
|
20
|
+
export * from './status/index.js';
|