@specdest/devkit 0.4.6 → 0.4.8
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/README.md +34 -4
- package/dist/npm/canonical-project-operations.d.ts +5 -0
- package/dist/npm/canonical-project-operations.js +5 -0
- package/dist/npm/canonical-project.d.ts +10 -0
- package/dist/npm/canonical-project.js +109 -0
- package/dist/npm/canonical-source.d.ts +14 -0
- package/dist/npm/canonical-source.js +216 -0
- package/dist/npm/catalog-snapshot.d.ts +47 -0
- package/dist/npm/catalog-snapshot.js +28 -0
- package/dist/npm/cli.d.ts +2 -0
- package/dist/npm/cli.js +15 -0
- package/dist/npm/commands.d.ts +9 -0
- package/dist/npm/commands.js +157 -0
- package/dist/npm/flow.d.ts +48 -0
- package/dist/npm/flow.js +396 -0
- package/dist/npm/fuzz.d.ts +138 -0
- package/dist/npm/fuzz.js +327 -0
- package/dist/npm/git-catalog.d.ts +39 -0
- package/dist/npm/git-catalog.js +190 -0
- package/dist/npm/index.d.ts +5 -0
- package/dist/npm/index.js +22 -0
- package/dist/npm/installer-primitives.d.ts +3 -0
- package/dist/npm/installer-primitives.js +50 -0
- package/dist/npm/predefined-packages/abac.d.ts +2 -0
- package/dist/npm/predefined-packages/abac.js +22 -0
- package/dist/npm/predefined-packages/auth.d.ts +2 -0
- package/dist/npm/predefined-packages/auth.js +21 -0
- package/dist/npm/predefined-packages/convex.d.ts +2 -0
- package/dist/npm/predefined-packages/convex.js +53 -0
- package/dist/npm/predefined-packages/expo.d.ts +2 -0
- package/dist/npm/predefined-packages/expo.js +84 -0
- package/dist/npm/predefined-packages/firebase-hosting.d.ts +2 -0
- package/dist/npm/predefined-packages/firebase-hosting.js +24 -0
- package/dist/npm/predefined-packages/health.d.ts +10 -0
- package/dist/npm/predefined-packages/health.js +52 -0
- package/dist/npm/predefined-packages/husky.d.ts +2 -0
- package/dist/npm/predefined-packages/husky.js +57 -0
- package/dist/npm/predefined-packages/index.d.ts +11 -0
- package/dist/npm/predefined-packages/index.js +56 -0
- package/dist/npm/predefined-packages/install.d.ts +19 -0
- package/dist/npm/predefined-packages/install.js +85 -0
- package/dist/npm/predefined-packages/local-logging.d.ts +2 -0
- package/dist/npm/predefined-packages/local-logging.js +17 -0
- package/dist/npm/predefined-packages/logging.d.ts +2 -0
- package/dist/npm/predefined-packages/logging.js +21 -0
- package/dist/npm/predefined-packages/multi-tenancy.d.ts +2 -0
- package/dist/npm/predefined-packages/multi-tenancy.js +21 -0
- package/dist/npm/predefined-packages/nps.d.ts +2 -0
- package/dist/npm/predefined-packages/nps.js +32 -0
- package/dist/npm/predefined-packages/oxfmt.d.ts +2 -0
- package/dist/npm/predefined-packages/oxfmt.js +22 -0
- package/dist/npm/predefined-packages/oxlint.d.ts +2 -0
- package/dist/npm/predefined-packages/oxlint.js +35 -0
- package/dist/npm/predefined-packages/package-json.d.ts +3 -0
- package/dist/npm/predefined-packages/package-json.js +61 -0
- package/dist/npm/predefined-packages/payments.d.ts +2 -0
- package/dist/npm/predefined-packages/payments.js +31 -0
- package/dist/npm/predefined-packages/playwright.d.ts +2 -0
- package/dist/npm/predefined-packages/playwright.js +35 -0
- package/dist/npm/predefined-packages/project-signals.d.ts +4 -0
- package/dist/npm/predefined-packages/project-signals.js +39 -0
- package/dist/npm/predefined-packages/registry.d.ts +3 -0
- package/dist/npm/predefined-packages/registry.js +63 -0
- package/dist/npm/predefined-packages/resolver.d.ts +8 -0
- package/dist/npm/predefined-packages/resolver.js +35 -0
- package/dist/npm/predefined-packages/sentry.d.ts +2 -0
- package/dist/npm/predefined-packages/sentry.js +29 -0
- package/dist/npm/predefined-packages/shadcn.d.ts +2 -0
- package/dist/npm/predefined-packages/shadcn.js +23 -0
- package/dist/npm/predefined-packages/storybook.d.ts +2 -0
- package/dist/npm/predefined-packages/storybook.js +25 -0
- package/dist/npm/predefined-packages/stryker.d.ts +2 -0
- package/dist/npm/predefined-packages/stryker.js +48 -0
- package/dist/npm/predefined-packages/supabase.d.ts +2 -0
- package/dist/npm/predefined-packages/supabase.js +84 -0
- package/dist/npm/predefined-packages/tailwind.d.ts +2 -0
- package/dist/npm/predefined-packages/tailwind.js +18 -0
- package/dist/npm/predefined-packages/tanstack-form.d.ts +2 -0
- package/dist/npm/predefined-packages/tanstack-form.js +19 -0
- package/dist/npm/predefined-packages/tanstack-query.d.ts +2 -0
- package/dist/npm/predefined-packages/tanstack-query.js +15 -0
- package/dist/npm/predefined-packages/tanstack-router.d.ts +2 -0
- package/dist/npm/predefined-packages/tanstack-router.js +18 -0
- package/dist/npm/predefined-packages/types.d.ts +31 -0
- package/dist/npm/predefined-packages/types.js +1 -0
- package/dist/npm/predefined-packages/vite.d.ts +2 -0
- package/dist/npm/predefined-packages/vite.js +123 -0
- package/dist/npm/predefined-packages/vitest.d.ts +2 -0
- package/dist/npm/predefined-packages/vitest.js +20 -0
- package/dist/npm/predefined-packages/zod.d.ts +2 -0
- package/dist/npm/predefined-packages/zod.js +18 -0
- package/dist/npm/project-check.d.ts +6 -0
- package/dist/npm/project-check.js +87 -0
- package/dist/npm/project-context.d.ts +42 -0
- package/dist/npm/project-context.js +192 -0
- package/dist/npm/project-rules.d.ts +22 -0
- package/dist/npm/project-rules.js +66 -0
- package/dist/npm/project-runtime.d.ts +18 -0
- package/dist/npm/project-runtime.js +27 -0
- package/dist/npm/project-status.d.ts +32 -0
- package/dist/npm/project-status.js +56 -0
- package/dist/npm/project-sync.d.ts +7 -0
- package/dist/npm/project-sync.js +135 -0
- package/dist/npm/remote-installer.d.ts +4 -0
- package/dist/npm/remote-installer.js +312 -0
- package/dist/npm/state.d.ts +14 -0
- package/dist/npm/state.js +182 -0
- package/package.json +10 -7
- package/src/canonical-project-operations.js +0 -438
- package/src/canonical-project.js +0 -136
- package/src/canonical-source.js +0 -250
- package/src/catalog-snapshot.js +0 -39
- package/src/catalog.js +0 -117
- package/src/cli.js +0 -14
- package/src/fuzz.js +0 -291
- package/src/git-catalog.js +0 -177
- package/src/index.js +0 -36
- package/src/installer-primitives.js +0 -198
- package/src/project-operations.js +0 -189
- package/src/project-targeting.js +0 -48
- package/src/remote-installer.js +0 -479
- package/src/rule-profile.js +0 -61
- package/src/setup-requirements.js +0 -82
- package/src/shared-file.js +0 -131
- package/src/sync-core.js +0 -390
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
export function zodArbitrary(schema: any): any;
|
|
2
|
+
export function amplifySchema(schema: any, sample: any): ({
|
|
3
|
+
path: any;
|
|
4
|
+
label: string | number | boolean | null[] | undefined[];
|
|
5
|
+
value: string | number | boolean | null[] | undefined[];
|
|
6
|
+
accepted: any;
|
|
7
|
+
concern: string | null;
|
|
8
|
+
} | {
|
|
9
|
+
path: any;
|
|
10
|
+
label: string | any[];
|
|
11
|
+
value: string | any[];
|
|
12
|
+
accepted: any;
|
|
13
|
+
concern: null;
|
|
14
|
+
} | {
|
|
15
|
+
path: any;
|
|
16
|
+
label: string | boolean | null | undefined;
|
|
17
|
+
value: string | boolean | null | undefined;
|
|
18
|
+
accepted: any;
|
|
19
|
+
concern: null;
|
|
20
|
+
})[];
|
|
21
|
+
export function fuzzSchema(schema: any, { seed, numRuns }?: {
|
|
22
|
+
seed?: number | undefined;
|
|
23
|
+
numRuns?: number | undefined;
|
|
24
|
+
}): {
|
|
25
|
+
status: string;
|
|
26
|
+
reason: any;
|
|
27
|
+
seed?: undefined;
|
|
28
|
+
cases?: undefined;
|
|
29
|
+
path?: undefined;
|
|
30
|
+
counterexample?: undefined;
|
|
31
|
+
} | {
|
|
32
|
+
status: string;
|
|
33
|
+
seed: number;
|
|
34
|
+
cases: any[];
|
|
35
|
+
reason?: undefined;
|
|
36
|
+
path?: undefined;
|
|
37
|
+
counterexample?: undefined;
|
|
38
|
+
} | {
|
|
39
|
+
status: string;
|
|
40
|
+
seed: number;
|
|
41
|
+
path: string | null;
|
|
42
|
+
counterexample: any;
|
|
43
|
+
cases: any[];
|
|
44
|
+
reason?: undefined;
|
|
45
|
+
};
|
|
46
|
+
export function discoverFuzzTargets(projectRoot: any): Promise<any>;
|
|
47
|
+
export function runFuzz(projectRoot: any, { seed }?: {
|
|
48
|
+
seed?: number | undefined;
|
|
49
|
+
}): Promise<{
|
|
50
|
+
seed: number;
|
|
51
|
+
targets: any;
|
|
52
|
+
results: ({
|
|
53
|
+
status: string;
|
|
54
|
+
reason: any;
|
|
55
|
+
seed?: undefined;
|
|
56
|
+
cases?: undefined;
|
|
57
|
+
path?: undefined;
|
|
58
|
+
counterexample?: undefined;
|
|
59
|
+
module: any;
|
|
60
|
+
name: string;
|
|
61
|
+
} | {
|
|
62
|
+
status: string;
|
|
63
|
+
seed: number;
|
|
64
|
+
cases: any[];
|
|
65
|
+
reason?: undefined;
|
|
66
|
+
path?: undefined;
|
|
67
|
+
counterexample?: undefined;
|
|
68
|
+
module: any;
|
|
69
|
+
name: string;
|
|
70
|
+
} | {
|
|
71
|
+
status: string;
|
|
72
|
+
seed: number;
|
|
73
|
+
path: string | null;
|
|
74
|
+
counterexample: any;
|
|
75
|
+
cases: any[];
|
|
76
|
+
reason?: undefined;
|
|
77
|
+
module: any;
|
|
78
|
+
name: string;
|
|
79
|
+
})[];
|
|
80
|
+
failed: ({
|
|
81
|
+
status: string;
|
|
82
|
+
reason: any;
|
|
83
|
+
seed?: undefined;
|
|
84
|
+
cases?: undefined;
|
|
85
|
+
path?: undefined;
|
|
86
|
+
counterexample?: undefined;
|
|
87
|
+
module: any;
|
|
88
|
+
name: string;
|
|
89
|
+
} | {
|
|
90
|
+
status: string;
|
|
91
|
+
seed: number;
|
|
92
|
+
cases: any[];
|
|
93
|
+
reason?: undefined;
|
|
94
|
+
path?: undefined;
|
|
95
|
+
counterexample?: undefined;
|
|
96
|
+
module: any;
|
|
97
|
+
name: string;
|
|
98
|
+
} | {
|
|
99
|
+
status: string;
|
|
100
|
+
seed: number;
|
|
101
|
+
path: string | null;
|
|
102
|
+
counterexample: any;
|
|
103
|
+
cases: any[];
|
|
104
|
+
reason?: undefined;
|
|
105
|
+
module: any;
|
|
106
|
+
name: string;
|
|
107
|
+
})[];
|
|
108
|
+
unsupported: ({
|
|
109
|
+
status: string;
|
|
110
|
+
reason: any;
|
|
111
|
+
seed?: undefined;
|
|
112
|
+
cases?: undefined;
|
|
113
|
+
path?: undefined;
|
|
114
|
+
counterexample?: undefined;
|
|
115
|
+
module: any;
|
|
116
|
+
name: string;
|
|
117
|
+
} | {
|
|
118
|
+
status: string;
|
|
119
|
+
seed: number;
|
|
120
|
+
cases: any[];
|
|
121
|
+
reason?: undefined;
|
|
122
|
+
path?: undefined;
|
|
123
|
+
counterexample?: undefined;
|
|
124
|
+
module: any;
|
|
125
|
+
name: string;
|
|
126
|
+
} | {
|
|
127
|
+
status: string;
|
|
128
|
+
seed: number;
|
|
129
|
+
path: string | null;
|
|
130
|
+
counterexample: any;
|
|
131
|
+
cases: any[];
|
|
132
|
+
reason?: undefined;
|
|
133
|
+
module: any;
|
|
134
|
+
name: string;
|
|
135
|
+
})[];
|
|
136
|
+
}>;
|
|
137
|
+
export function createFuzzCommandHandler(projectRoot?: string): (args: any, io: any) => Promise<0 | 1 | 2>;
|
|
138
|
+
export const FUZZ_DEFAULT_SEED: 20260914;
|
package/dist/npm/fuzz.js
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
import { existsSync } from 'node:fs';
|
|
10
|
+
import { readdir, readFile } from 'node:fs/promises';
|
|
11
|
+
import { dirname, join, relative, resolve } from 'node:path';
|
|
12
|
+
import { registerHooks } from 'node:module';
|
|
13
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
14
|
+
import fc from 'fast-check';
|
|
15
|
+
import { ensureMetadataNamespace } from "./state.js";
|
|
16
|
+
const DEFAULT_SUFFIXES = ['.input.schema.ts', '.output.schema.ts', '.form.schema.ts', '.callbackInput.schema.ts'];
|
|
17
|
+
const CONFIG_PATH = '.spd/fuzz.json';
|
|
18
|
+
const DEFAULT_SEED = 20260914;
|
|
19
|
+
function schemaDef(schema) {
|
|
20
|
+
return schema?._zod?.def ?? schema?._def ?? schema?.def;
|
|
21
|
+
}
|
|
22
|
+
function schemaType(schema) {
|
|
23
|
+
return schemaDef(schema)?.type ?? schema?.type;
|
|
24
|
+
}
|
|
25
|
+
function isSchema(value) {
|
|
26
|
+
return value && typeof value.safeParse === 'function' && typeof schemaType(value) === 'string';
|
|
27
|
+
}
|
|
28
|
+
function unwrap(schema) {
|
|
29
|
+
const type = schemaType(schema);
|
|
30
|
+
if (type === 'optional' || type === 'nullable' || type === 'default' || type === 'catch') {
|
|
31
|
+
return unwrap(schemaDef(schema).innerType);
|
|
32
|
+
}
|
|
33
|
+
return schema;
|
|
34
|
+
}
|
|
35
|
+
function boundsFor(schema) {
|
|
36
|
+
const bounds = {
|
|
37
|
+
minLength: Number.isFinite(schema?.minLength) ? schema.minLength : undefined,
|
|
38
|
+
maxLength: Number.isFinite(schema?.maxLength) ? schema.maxLength : undefined,
|
|
39
|
+
minValue: Number.isFinite(schema?.minValue) ? schema.minValue : undefined,
|
|
40
|
+
maxValue: Number.isFinite(schema?.maxValue) ? schema.maxValue : undefined,
|
|
41
|
+
};
|
|
42
|
+
for (const check of schemaDef(schema)?.checks ?? []) {
|
|
43
|
+
const def = check?._zod?.def ?? check?.def;
|
|
44
|
+
if (def?.check === 'min_length')
|
|
45
|
+
bounds.minLength = def.minimum;
|
|
46
|
+
if (def?.check === 'max_length')
|
|
47
|
+
bounds.maxLength = def.maximum;
|
|
48
|
+
if (def?.check === 'greater_than' && def.inclusive)
|
|
49
|
+
bounds.minValue = def.value;
|
|
50
|
+
if (def?.check === 'less_than' && def.inclusive)
|
|
51
|
+
bounds.maxValue = def.value;
|
|
52
|
+
}
|
|
53
|
+
return bounds;
|
|
54
|
+
}
|
|
55
|
+
export function zodArbitrary(schema) {
|
|
56
|
+
const type = schemaType(schema);
|
|
57
|
+
const def = schemaDef(schema);
|
|
58
|
+
if (type === 'optional')
|
|
59
|
+
return fc.option(zodArbitrary(def.innerType), { nil: undefined });
|
|
60
|
+
if (type === 'nullable')
|
|
61
|
+
return fc.option(zodArbitrary(def.innerType), { nil: null });
|
|
62
|
+
if (type === 'string') {
|
|
63
|
+
const { minLength = 0, maxLength = 32 } = boundsFor(schema);
|
|
64
|
+
return fc.string({ minLength, maxLength: Math.min(maxLength, 64) }).filter((value) => schema.safeParse(value).success);
|
|
65
|
+
}
|
|
66
|
+
if (type === 'number') {
|
|
67
|
+
const { minValue = -1000, maxValue = 1000 } = boundsFor(schema);
|
|
68
|
+
return fc.double({ min: minValue, max: maxValue, noNaN: true, noDefaultInfinity: true }).filter((value) => schema.safeParse(value).success);
|
|
69
|
+
}
|
|
70
|
+
if (type === 'boolean')
|
|
71
|
+
return fc.boolean();
|
|
72
|
+
if (type === 'enum')
|
|
73
|
+
return fc.constantFrom(...Object.values(def.entries));
|
|
74
|
+
if (type === 'array') {
|
|
75
|
+
const { minLength = 0, maxLength = 4 } = boundsFor(schema);
|
|
76
|
+
return fc.array(zodArbitrary(def.element), { minLength, maxLength: Math.min(maxLength, 8) }).filter((value) => schema.safeParse(value).success);
|
|
77
|
+
}
|
|
78
|
+
if (type === 'object') {
|
|
79
|
+
const shape = typeof def.shape === 'function' ? def.shape() : def.shape;
|
|
80
|
+
const shapeEntries = Object.entries(shape ?? {});
|
|
81
|
+
const entries = shapeEntries.map(([key, child]) => [key, zodArbitrary(child)]);
|
|
82
|
+
const requiredKeys = shapeEntries.filter(([, child]) => schemaType(child) !== 'optional').map(([key]) => key);
|
|
83
|
+
return fc.record(Object.fromEntries(entries), { requiredKeys })
|
|
84
|
+
.filter((value) => schema.safeParse(value).success);
|
|
85
|
+
}
|
|
86
|
+
throw new Error(`Unsupported Zod schema type: ${type ?? 'unknown'}`);
|
|
87
|
+
}
|
|
88
|
+
function getAtPath(value, path) {
|
|
89
|
+
return path.reduce((current, key) => current?.[key], value);
|
|
90
|
+
}
|
|
91
|
+
function setAtPath(value, path, replacement, omit = false) {
|
|
92
|
+
if (!path.length)
|
|
93
|
+
return replacement;
|
|
94
|
+
const clone = Array.isArray(value) ? [...value] : { ...value };
|
|
95
|
+
const [head, ...tail] = path;
|
|
96
|
+
if (!tail.length && omit)
|
|
97
|
+
delete clone[head];
|
|
98
|
+
else
|
|
99
|
+
clone[head] = setAtPath(value?.[head], tail, replacement, omit);
|
|
100
|
+
return clone;
|
|
101
|
+
}
|
|
102
|
+
function semanticCases(schema) {
|
|
103
|
+
const base = unwrap(schema);
|
|
104
|
+
const type = schemaType(base);
|
|
105
|
+
const bounds = boundsFor(base);
|
|
106
|
+
if (type === 'string') {
|
|
107
|
+
const cases = [['empty string', ''], ['single-space string', ' '], ['whitespace-only string', ' ']];
|
|
108
|
+
if (Number.isFinite(bounds.minLength))
|
|
109
|
+
cases.push(['minimum-length string', 'x'.repeat(bounds.minLength)]);
|
|
110
|
+
if (Number.isFinite(bounds.maxLength))
|
|
111
|
+
cases.push(['maximum-length string', 'x'.repeat(Math.min(bounds.maxLength, 128))]);
|
|
112
|
+
return cases;
|
|
113
|
+
}
|
|
114
|
+
if (type === 'number') {
|
|
115
|
+
const cases = [['zero', 0], ['NaN', Number.NaN]];
|
|
116
|
+
if (Number.isFinite(bounds.minValue))
|
|
117
|
+
cases.push(['minimum number', bounds.minValue]);
|
|
118
|
+
if (Number.isFinite(bounds.maxValue))
|
|
119
|
+
cases.push(['maximum number', bounds.maxValue]);
|
|
120
|
+
return cases;
|
|
121
|
+
}
|
|
122
|
+
if (type === 'boolean')
|
|
123
|
+
return [['false', false]];
|
|
124
|
+
if (type === 'array')
|
|
125
|
+
return [['empty array', []], ['array containing null', [null]], ['array containing undefined', [undefined]]];
|
|
126
|
+
return [];
|
|
127
|
+
}
|
|
128
|
+
function collectLeaves(schema, path = []) {
|
|
129
|
+
const def = schemaDef(schema);
|
|
130
|
+
const type = schemaType(schema);
|
|
131
|
+
if (type === 'optional' || type === 'nullable')
|
|
132
|
+
return collectLeaves(def.innerType, path).map((leaf) => ({ ...leaf, wrappers: [type, ...(leaf.wrappers ?? [])] }));
|
|
133
|
+
if (type === 'object') {
|
|
134
|
+
const shape = typeof def.shape === 'function' ? def.shape() : def.shape;
|
|
135
|
+
return Object.entries(shape ?? {}).flatMap(([key, child]) => collectLeaves(child, [...path, key]));
|
|
136
|
+
}
|
|
137
|
+
return [{ path, schema, wrappers: [] }];
|
|
138
|
+
}
|
|
139
|
+
function semanticConcern(leaf, label, accepted) {
|
|
140
|
+
if (!accepted)
|
|
141
|
+
return null;
|
|
142
|
+
if (schemaType(unwrap(leaf.schema)) === 'string' && /empty|space|whitespace/u.test(label)) {
|
|
143
|
+
return 'empty/whitespace strings are accepted';
|
|
144
|
+
}
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
export function amplifySchema(schema, sample) {
|
|
148
|
+
const cases = [];
|
|
149
|
+
for (const leaf of collectLeaves(schema)) {
|
|
150
|
+
for (const [label, replacement] of semanticCases(leaf.schema)) {
|
|
151
|
+
const value = setAtPath(sample, leaf.path, replacement);
|
|
152
|
+
const accepted = schema.safeParse(value).success;
|
|
153
|
+
cases.push({ path: leaf.path.join('.'), label, value: replacement, accepted, concern: semanticConcern(leaf, label, accepted) });
|
|
154
|
+
}
|
|
155
|
+
if (schemaType(unwrap(leaf.schema)) === 'array') {
|
|
156
|
+
const current = getAtPath(sample, leaf.path);
|
|
157
|
+
if (Array.isArray(current) && current.length) {
|
|
158
|
+
const arrayCases = [['array item removal', current.slice(1)], ['duplicate array item', [current[0], ...current]]];
|
|
159
|
+
if (current.length > 1)
|
|
160
|
+
arrayCases.push(['array reordering', [...current].reverse()]);
|
|
161
|
+
for (const [label, replacement] of arrayCases) {
|
|
162
|
+
const value = setAtPath(sample, leaf.path, replacement);
|
|
163
|
+
cases.push({ path: leaf.path.join('.'), label, value: replacement, accepted: schema.safeParse(value).success, concern: null });
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (leaf.path.length) {
|
|
168
|
+
for (const [label, replacement, omit] of [['null', null, false], ['undefined', undefined, false], ['omitted property', undefined, true]]) {
|
|
169
|
+
const value = setAtPath(sample, leaf.path, replacement, omit);
|
|
170
|
+
cases.push({ path: leaf.path.join('.'), label, value: replacement, accepted: schema.safeParse(value).success, concern: null });
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return cases;
|
|
175
|
+
}
|
|
176
|
+
export function fuzzSchema(schema, { seed = DEFAULT_SEED, numRuns = 50 } = {}) {
|
|
177
|
+
let arbitrary;
|
|
178
|
+
try {
|
|
179
|
+
arbitrary = zodArbitrary(schema);
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
return { status: 'unsupported', reason: error.message };
|
|
183
|
+
}
|
|
184
|
+
let lastCases = [];
|
|
185
|
+
const property = fc.property(arbitrary, (sample) => {
|
|
186
|
+
lastCases = amplifySchema(schema, sample);
|
|
187
|
+
return !lastCases.some((item) => item.concern);
|
|
188
|
+
});
|
|
189
|
+
const result = fc.check(property, { seed, numRuns });
|
|
190
|
+
if (!result.failed)
|
|
191
|
+
return { status: 'pass', seed, cases: lastCases };
|
|
192
|
+
const sample = result.counterexample?.[0];
|
|
193
|
+
const cases = sample === undefined ? lastCases : amplifySchema(schema, sample);
|
|
194
|
+
return {
|
|
195
|
+
status: 'fail',
|
|
196
|
+
seed,
|
|
197
|
+
path: result.counterexamplePath,
|
|
198
|
+
counterexample: cases.find((item) => item.concern),
|
|
199
|
+
cases,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
async function walk(dir) {
|
|
203
|
+
const found = [];
|
|
204
|
+
let entries = [];
|
|
205
|
+
try {
|
|
206
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
return found;
|
|
210
|
+
}
|
|
211
|
+
for (const entry of entries) {
|
|
212
|
+
if (entry.name === 'node_modules' || entry.name.startsWith('.'))
|
|
213
|
+
continue;
|
|
214
|
+
const path = join(dir, entry.name);
|
|
215
|
+
if (entry.isDirectory())
|
|
216
|
+
found.push(...await walk(path));
|
|
217
|
+
else if (DEFAULT_SUFFIXES.some((suffix) => entry.name.endsWith(suffix)))
|
|
218
|
+
found.push(path);
|
|
219
|
+
}
|
|
220
|
+
return found;
|
|
221
|
+
}
|
|
222
|
+
async function readConfig(projectRoot) {
|
|
223
|
+
await ensureMetadataNamespace(projectRoot);
|
|
224
|
+
try {
|
|
225
|
+
const text = await readFile(join(projectRoot, CONFIG_PATH), 'utf8');
|
|
226
|
+
const config = JSON.parse(text);
|
|
227
|
+
if (config.schemaVersion !== 1 || !Array.isArray(config.targets))
|
|
228
|
+
throw new Error(`${CONFIG_PATH} must use schemaVersion 1 and a targets array`);
|
|
229
|
+
return config;
|
|
230
|
+
}
|
|
231
|
+
catch (error) {
|
|
232
|
+
if (error?.code === 'ENOENT')
|
|
233
|
+
return null;
|
|
234
|
+
throw error;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
export async function discoverFuzzTargets(projectRoot) {
|
|
238
|
+
const config = await readConfig(projectRoot);
|
|
239
|
+
if (config)
|
|
240
|
+
return config.targets.map((target) => ({ module: resolve(projectRoot, target.module), exports: target.exports ?? null }));
|
|
241
|
+
const files = [...await walk(join(projectRoot, 'src')), ...await walk(join(projectRoot, 'supabase', 'functions'))];
|
|
242
|
+
return files.map((module) => ({ module, exports: null }));
|
|
243
|
+
}
|
|
244
|
+
function resolveProjectSpecifier(specifier, parentURL, projectRoot) {
|
|
245
|
+
let base;
|
|
246
|
+
if (specifier.startsWith('@/'))
|
|
247
|
+
base = resolve(projectRoot, 'src', specifier.slice(2));
|
|
248
|
+
else if ((specifier.startsWith('./') || specifier.startsWith('../')) && parentURL?.startsWith('file:')) {
|
|
249
|
+
base = resolve(dirname(fileURLToPath(parentURL)), specifier);
|
|
250
|
+
}
|
|
251
|
+
else
|
|
252
|
+
return null;
|
|
253
|
+
const candidates = [base, ...['.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs'].map((ext) => `${base}${ext}`),
|
|
254
|
+
...['.ts', '.tsx', '.js', '.jsx'].map((ext) => join(base, `index${ext}`))];
|
|
255
|
+
return candidates.find((candidate) => existsSync(candidate)) ?? null;
|
|
256
|
+
}
|
|
257
|
+
async function loadTarget(target, projectRoot) {
|
|
258
|
+
const hooks = registerHooks({
|
|
259
|
+
resolve(specifier, context, nextResolve) {
|
|
260
|
+
const resolved = resolveProjectSpecifier(specifier, context.parentURL, projectRoot);
|
|
261
|
+
return resolved ? nextResolve(pathToFileURL(resolved).href, context) : nextResolve(specifier, context);
|
|
262
|
+
},
|
|
263
|
+
});
|
|
264
|
+
try {
|
|
265
|
+
const loaded = await import(__rewriteRelativeImportExtension(pathToFileURL(target.module).href));
|
|
266
|
+
return Object.entries(loaded)
|
|
267
|
+
.filter(([name, value]) => (!target.exports || target.exports.includes(name)) && isSchema(value))
|
|
268
|
+
.map(([name, schema]) => ({ name, schema }));
|
|
269
|
+
}
|
|
270
|
+
finally {
|
|
271
|
+
hooks.deregister();
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
function formatValue(value) {
|
|
275
|
+
if (value === undefined)
|
|
276
|
+
return 'undefined';
|
|
277
|
+
if (typeof value === 'number' && Number.isNaN(value))
|
|
278
|
+
return 'NaN';
|
|
279
|
+
return JSON.stringify(value);
|
|
280
|
+
}
|
|
281
|
+
export async function runFuzz(projectRoot, { seed = DEFAULT_SEED } = {}) {
|
|
282
|
+
const targets = await discoverFuzzTargets(projectRoot);
|
|
283
|
+
const results = [];
|
|
284
|
+
for (const target of targets) {
|
|
285
|
+
let schemas;
|
|
286
|
+
try {
|
|
287
|
+
schemas = await loadTarget(target, projectRoot);
|
|
288
|
+
}
|
|
289
|
+
catch (error) {
|
|
290
|
+
results.push({ status: 'unsupported', module: target.module, name: '*', reason: `unable to load module: ${error.message}` });
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
if (!schemas.length) {
|
|
294
|
+
results.push({ status: 'unsupported', module: target.module, name: '*', reason: 'no exported Zod schema found' });
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
for (const { name, schema } of schemas)
|
|
298
|
+
results.push({ module: target.module, name, ...fuzzSchema(schema, { seed }) });
|
|
299
|
+
}
|
|
300
|
+
return { seed, targets, results, failed: results.filter((item) => item.status === 'fail'), unsupported: results.filter((item) => item.status === 'unsupported') };
|
|
301
|
+
}
|
|
302
|
+
export function createFuzzCommandHandler(projectRoot = process.cwd()) {
|
|
303
|
+
return async (args, io) => {
|
|
304
|
+
if (args.length) {
|
|
305
|
+
io.stderr.write('Usage: specdest fuzz\n');
|
|
306
|
+
return 2;
|
|
307
|
+
}
|
|
308
|
+
const result = await runFuzz(projectRoot);
|
|
309
|
+
for (const item of result.failed) {
|
|
310
|
+
const counterexample = item.counterexample;
|
|
311
|
+
const label = `${item.name}${counterexample?.path ? `.${counterexample.path}` : ''}`;
|
|
312
|
+
io.stderr.write(`FAIL ${label}\n\n`);
|
|
313
|
+
io.stderr.write(`Unexpected behavior for amplified value:\n${formatValue(counterexample?.value)}\n\n`);
|
|
314
|
+
io.stderr.write(`Related semantic concern:\n${counterexample?.concern ?? 'fuzz property failed'}\n\n`);
|
|
315
|
+
io.stderr.write(`Reproduce: seed=${item.seed}${item.path ? ` path=${item.path}` : ''}\n`);
|
|
316
|
+
io.stderr.write(`Module: ${relative(projectRoot, item.module)}\n\n`);
|
|
317
|
+
}
|
|
318
|
+
for (const item of result.unsupported)
|
|
319
|
+
io.stderr.write(`SKIP ${relative(projectRoot, item.module)} ${item.name}: ${item.reason}\n`);
|
|
320
|
+
if (!result.results.length)
|
|
321
|
+
io.stdout.write('Fuzz: no contract targets found.\n');
|
|
322
|
+
else if (!result.failed.length)
|
|
323
|
+
io.stdout.write(`Fuzz: ${result.results.length - result.unsupported.length} passed; ${result.unsupported.length} skipped.\n`);
|
|
324
|
+
return result.failed.length ? 1 : 0;
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
export const FUZZ_DEFAULT_SEED = DEFAULT_SEED;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export function buildGitCatalog({ modulesRoot, repositoryRevision }: {
|
|
2
|
+
modulesRoot: any;
|
|
3
|
+
repositoryRevision?: string | undefined;
|
|
4
|
+
}): Promise<{
|
|
5
|
+
schemaVersion: number;
|
|
6
|
+
package: {
|
|
7
|
+
name: string;
|
|
8
|
+
version: string;
|
|
9
|
+
};
|
|
10
|
+
entries: {
|
|
11
|
+
projectProfiles?: {
|
|
12
|
+
[k: string]: {
|
|
13
|
+
ruleIds: any[];
|
|
14
|
+
};
|
|
15
|
+
} | undefined;
|
|
16
|
+
notApplicableRuleIds?: any[] | undefined;
|
|
17
|
+
composedOf?: string[] | undefined;
|
|
18
|
+
verification?: any;
|
|
19
|
+
ai?: any;
|
|
20
|
+
requiredCapabilities?: string[] | undefined;
|
|
21
|
+
requiredPackages?: string[] | undefined;
|
|
22
|
+
applicableStacks?: any[] | undefined;
|
|
23
|
+
enforcement?: string | undefined;
|
|
24
|
+
legacyInstalledIds?: any[] | undefined;
|
|
25
|
+
order: number;
|
|
26
|
+
source: string;
|
|
27
|
+
revision: string;
|
|
28
|
+
contentHash: string;
|
|
29
|
+
labels?: string[] | undefined;
|
|
30
|
+
scope?: string | undefined;
|
|
31
|
+
category?: string | undefined;
|
|
32
|
+
summary?: any;
|
|
33
|
+
id: any;
|
|
34
|
+
kind: any;
|
|
35
|
+
version: string;
|
|
36
|
+
name: any;
|
|
37
|
+
content: any;
|
|
38
|
+
}[];
|
|
39
|
+
}>;
|