@kerf-js/cli 0.0.0-stage → 0.1.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/LICENSE +16 -0
- package/README.md +554 -2
- package/bin/kerf.mjs +15 -0
- package/docs/async.md +50 -0
- package/docs/browser.md +53 -0
- package/docs/config.md +127 -0
- package/docs/core.md +30 -0
- package/docs/deploy.md +108 -0
- package/docs/design.md +106 -0
- package/docs/entrypoints.md +83 -0
- package/docs/fonts.md +118 -0
- package/docs/icons.md +108 -0
- package/docs/lists.md +25 -0
- package/docs/meta.md +92 -0
- package/docs/plugins.md +166 -0
- package/docs/props.md +88 -0
- package/docs/reactivity.md +26 -0
- package/docs/rules.md +39 -0
- package/docs/rust.md +253 -0
- package/docs/start-short.md +10 -0
- package/docs/start.md +642 -0
- package/docs/state.md +122 -0
- package/docs/style.md +82 -0
- package/docs/surface.md +158 -0
- package/docs/testing.md +81 -0
- package/docs/theme.md +263 -0
- package/docs/ui-state.md +105 -0
- package/docs/verify.md +61 -0
- package/package.json +47 -3
- package/src/add/config.mjs +93 -0
- package/src/add/errors.mjs +2 -0
- package/src/add/setup.mjs +88 -0
- package/src/add-rust.mjs +178 -0
- package/src/add.mjs +392 -0
- package/src/agents-md.mjs +240 -0
- package/src/alias.mjs +70 -0
- package/src/audit/page.mjs +313 -0
- package/src/audit/report.mjs +69 -0
- package/src/audit/utils.mjs +5 -0
- package/src/audit.mjs +319 -0
- package/src/browser/cli.mjs +53 -0
- package/src/browser/client.mjs +299 -0
- package/src/browser/commands.mjs +258 -0
- package/src/capture.mjs +21 -0
- package/src/catalog/audit.mjs +129 -0
- package/src/catalog/auth.mjs +40 -0
- package/src/catalog/compiler.mjs +337 -0
- package/src/catalog/contracts.mjs +18 -0
- package/src/catalog/database.mjs +52 -0
- package/src/catalog/deploy.mjs +117 -0
- package/src/catalog/design.mjs +159 -0
- package/src/catalog/forms.mjs +25 -0
- package/src/catalog/hydration.mjs +29 -0
- package/src/catalog/icons.mjs +24 -0
- package/src/catalog/media.mjs +163 -0
- package/src/catalog/metadata.mjs +116 -0
- package/src/catalog/plugins.mjs +228 -0
- package/src/catalog/realtime.mjs +195 -0
- package/src/catalog/rust.mjs +75 -0
- package/src/catalog/server-functions.mjs +100 -0
- package/src/catalog/state.mjs +39 -0
- package/src/catalog/style-groups.mjs +54 -0
- package/src/catalog/tests.mjs +127 -0
- package/src/catalog/text.mjs +200 -0
- package/src/catalog/ui-state.mjs +42 -0
- package/src/catalog.mjs +103 -0
- package/src/check/runtime.mjs +280 -0
- package/src/check.mjs +321 -0
- package/src/cli/authoring.mjs +213 -0
- package/src/cli/project.mjs +302 -0
- package/src/cli.mjs +340 -0
- package/src/config.d.ts +1 -0
- package/src/config.mjs +5 -0
- package/src/contributions.mjs +119 -0
- package/src/db-layer.mjs +41 -0
- package/src/deploy/cdn.mjs +142 -0
- package/src/deploy/files.mjs +266 -0
- package/src/deploy/fit.mjs +166 -0
- package/src/deploy/hosts.mjs +126 -0
- package/src/deploy/index/cli.mjs +20 -0
- package/src/deploy/index/environment.mjs +69 -0
- package/src/deploy/index/steps.mjs +87 -0
- package/src/deploy/index.mjs +387 -0
- package/src/deploy/needs.mjs +149 -0
- package/src/design-checks/accessible-names.mjs +62 -0
- package/src/design-checks/contrast.mjs +43 -0
- package/src/design-checks/csp.mjs +60 -0
- package/src/design-checks/files.mjs +59 -0
- package/src/design-checks/heading-links.mjs +61 -0
- package/src/design-checks/interaction.mjs +65 -0
- package/src/design-checks/jsx.mjs +67 -0
- package/src/design-checks/layout-values.mjs +68 -0
- package/src/design-checks/native-controls.mjs +48 -0
- package/src/design-checks/outline.mjs +39 -0
- package/src/design-checks/reporter.mjs +37 -0
- package/src/design-checks/rules.mjs +274 -0
- package/src/design-checks/values.mjs +262 -0
- package/src/design-checks.mjs +49 -0
- package/src/diag.mjs +98 -0
- package/src/docs.mjs +228 -0
- package/src/doctor.mjs +42 -0
- package/src/files.mjs +60 -0
- package/src/fix.mjs +48 -0
- package/src/gate/after.mjs +40 -0
- package/src/gate/before.mjs +54 -0
- package/src/gate/classify.mjs +72 -0
- package/src/gate/mutation.mjs +209 -0
- package/src/gate/resolve-cases.mjs +22 -0
- package/src/gate/result.mjs +63 -0
- package/src/gate/run.mjs +75 -0
- package/src/gate.mjs +178 -0
- package/src/index.mjs +9 -0
- package/src/infer/box-props.cjs +140 -0
- package/src/infer/core-boxes.cjs +46 -0
- package/src/infer/derived-writes.cjs +95 -0
- package/src/infer/engine/checks.cjs +193 -0
- package/src/infer/engine/flow.cjs +293 -0
- package/src/infer/engine/report.cjs +158 -0
- package/src/infer/engine/utils.cjs +15 -0
- package/src/infer/engine.cjs +265 -0
- package/src/infer/facts/sites.cjs +114 -0
- package/src/infer/facts.cjs +345 -0
- package/src/infer/hosts.cjs +161 -0
- package/src/infer/lsp.mjs +240 -0
- package/src/infer/session.cjs +161 -0
- package/src/infer/shadow.cjs +54 -0
- package/src/infer/worker.cjs +145 -0
- package/src/jsx-scan.mjs +128 -0
- package/src/kerf.mjs +98 -0
- package/src/load-config/schema.mjs +300 -0
- package/src/load-config.mjs +302 -0
- package/src/map.mjs +177 -0
- package/src/mcp/browser-tools.mjs +155 -0
- package/src/mcp.mjs +273 -0
- package/src/meta-table.mjs +63 -0
- package/src/mutate.mjs +224 -0
- package/src/plugin-checks.mjs +118 -0
- package/src/plugins.mjs +258 -0
- package/src/project.mjs +310 -0
- package/src/props-checks.mjs +64 -0
- package/src/props-listener.mjs +56 -0
- package/src/react-checks.mjs +67 -0
- package/src/server-state-checks.mjs +34 -0
- package/src/share-checks.mjs +158 -0
- package/src/site.mjs +46 -0
- package/src/slots.mjs +181 -0
- package/src/start/build.mjs +116 -0
- package/src/start/routes.mjs +38 -0
- package/src/start.mjs +394 -0
- package/src/state-checks/core-state.mjs +223 -0
- package/src/state-checks/modules.mjs +17 -0
- package/src/state-checks/scan.mjs +232 -0
- package/src/state-checks/server-graph.mjs +65 -0
- package/src/state-checks.mjs +189 -0
- package/src/style/find.mjs +121 -0
- package/src/style.mjs +327 -0
- package/src/surface-checks.mjs +384 -0
- package/src/tailwind.mjs +23 -0
- package/src/tasks.mjs +261 -0
- package/src/test-rules/context.mjs +109 -0
- package/src/test-rules/file-rules.mjs +279 -0
- package/src/test-rules/rule-helpers.mjs +244 -0
- package/src/test-rules/syntax.mjs +223 -0
- package/src/test-rules/test-rules.mjs +127 -0
- package/src/testdiff.mjs +219 -0
- package/src/tests.mjs +120 -0
- package/src/text-checks.mjs +332 -0
- package/src/tokens.mjs +33 -0
- package/src/trust/scan.mjs +95 -0
- package/src/trust.mjs +394 -0
- package/src/workspace.mjs +182 -0
- package/ts-plugin/context.js +57 -0
- package/ts-plugin/index.js +17 -0
- package/ts-plugin/mapping.js +65 -0
- package/ts-plugin/package.json +1 -0
- package/ts-plugin/proxy.js +144 -0
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
2
|
+
import { join, relative } from 'node:path'
|
|
3
|
+
import { diagnostic } from '../diag.mjs'
|
|
4
|
+
import { apply, changedLines, mutants as makeMutants, sample, suggest } from '../mutate.mjs'
|
|
5
|
+
import { isTestFile } from '../tests.mjs'
|
|
6
|
+
import { copyProject, removeCopy, runVitest, writeFiles } from '../workspace.mjs'
|
|
7
|
+
|
|
8
|
+
export async function mutate({
|
|
9
|
+
options,
|
|
10
|
+
root,
|
|
11
|
+
source,
|
|
12
|
+
testFiles,
|
|
13
|
+
importsOf,
|
|
14
|
+
code,
|
|
15
|
+
posix,
|
|
16
|
+
isSupport,
|
|
17
|
+
maxMutants,
|
|
18
|
+
byKey,
|
|
19
|
+
result,
|
|
20
|
+
log,
|
|
21
|
+
concurrency,
|
|
22
|
+
budgetSec,
|
|
23
|
+
started,
|
|
24
|
+
after,
|
|
25
|
+
bin,
|
|
26
|
+
compileError,
|
|
27
|
+
allowed,
|
|
28
|
+
diagnostics,
|
|
29
|
+
}) {
|
|
30
|
+
// Mutation: the changed source lines (or, with no source change, the source files the tests import).
|
|
31
|
+
if (options.mutation !== false) {
|
|
32
|
+
const targets = mutationTargets({
|
|
33
|
+
root,
|
|
34
|
+
source,
|
|
35
|
+
testFiles,
|
|
36
|
+
importsOf,
|
|
37
|
+
code,
|
|
38
|
+
posix,
|
|
39
|
+
isSupport,
|
|
40
|
+
result,
|
|
41
|
+
})
|
|
42
|
+
const all = targets.flatMap((t) => makeMutants(t.text, t.rel, t.lines))
|
|
43
|
+
const list = sample(all, maxMutants)
|
|
44
|
+
result.mutants.generated = all.length
|
|
45
|
+
result.mutants.sampled = list.length < all.length
|
|
46
|
+
const passing = [...byKey.values()].filter((t) => t.after === 'passed')
|
|
47
|
+
const files = [...new Set(passing.map((t) => t.file))]
|
|
48
|
+
if (list.length && files.length) {
|
|
49
|
+
log(
|
|
50
|
+
`gate: ${list.length}${result.mutants.sampled ? ` of ${all.length}` : ''} mutants on ${targets.length} files, ${concurrency} at a time, ${budgetSec}s budget`,
|
|
51
|
+
)
|
|
52
|
+
const outcomes = await runMutants({
|
|
53
|
+
root,
|
|
54
|
+
options,
|
|
55
|
+
targets,
|
|
56
|
+
started,
|
|
57
|
+
budgetSec,
|
|
58
|
+
after,
|
|
59
|
+
list,
|
|
60
|
+
result,
|
|
61
|
+
files,
|
|
62
|
+
bin,
|
|
63
|
+
concurrency,
|
|
64
|
+
})
|
|
65
|
+
recordMutants({ outcomes, result, compileError, passing, allowed, diagnostics })
|
|
66
|
+
} else if (!list.length) result.notes.push('no mutants on the changed lines')
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function mutationTargets({ root, source, testFiles, importsOf, code, posix, isSupport, result }) {
|
|
71
|
+
// Only code the changed tests can reach (runtime imports): a mutant nothing runs survives by definition.
|
|
72
|
+
const reach = (limit) => {
|
|
73
|
+
const seen = new Set()
|
|
74
|
+
const walk = (abs, depth) => {
|
|
75
|
+
if (seen.has(abs) || depth > limit || !existsSync(abs)) return
|
|
76
|
+
seen.add(abs)
|
|
77
|
+
for (const p of importsOf(abs, readFileSync(abs, 'utf8'))) walk(p, depth + 1)
|
|
78
|
+
}
|
|
79
|
+
for (const f of testFiles) walk(join(root, f), 0)
|
|
80
|
+
return seen
|
|
81
|
+
}
|
|
82
|
+
const reachable = reach(12)
|
|
83
|
+
const unreached = source
|
|
84
|
+
.filter((c) => c.after != null && code(c.rel) && !reachable.has(join(root, c.rel)))
|
|
85
|
+
.map((c) => c.rel)
|
|
86
|
+
if (unreached.length)
|
|
87
|
+
result.notes.push(`not mutated (no changed test imports them): ${unreached.join(', ')}`)
|
|
88
|
+
let targets = source
|
|
89
|
+
.filter((c) => c.after != null && reachable.has(join(root, c.rel)))
|
|
90
|
+
.map((c) => ({ rel: c.rel, text: c.after, lines: changedLines(c.before, c.after) }))
|
|
91
|
+
if (!source.length) {
|
|
92
|
+
targets = [...reach(3)]
|
|
93
|
+
.map((abs) => posix(relative(root, abs)))
|
|
94
|
+
.filter(
|
|
95
|
+
(rel) =>
|
|
96
|
+
!isTestFile(rel) &&
|
|
97
|
+
code(rel) &&
|
|
98
|
+
!isSupport(rel) &&
|
|
99
|
+
!rel.startsWith('..') &&
|
|
100
|
+
!rel.includes('node_modules'),
|
|
101
|
+
)
|
|
102
|
+
.map((rel) => {
|
|
103
|
+
const text = readFileSync(join(root, rel), 'utf8')
|
|
104
|
+
return { rel, text, lines: changedLines(null, text) }
|
|
105
|
+
})
|
|
106
|
+
}
|
|
107
|
+
return targets
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async function runMutants({
|
|
111
|
+
root,
|
|
112
|
+
options,
|
|
113
|
+
targets,
|
|
114
|
+
started,
|
|
115
|
+
budgetSec,
|
|
116
|
+
after,
|
|
117
|
+
list,
|
|
118
|
+
result,
|
|
119
|
+
files,
|
|
120
|
+
bin,
|
|
121
|
+
concurrency,
|
|
122
|
+
}) {
|
|
123
|
+
const texts = Object.fromEntries(targets.map((t) => [t.rel, t.text]))
|
|
124
|
+
const deadline = started + budgetSec * 1000 + after.ms
|
|
125
|
+
const timeoutMs = Math.max(15_000, after.ms * 3)
|
|
126
|
+
const queue = [...list]
|
|
127
|
+
const outcomes = []
|
|
128
|
+
const worker = async (i) => {
|
|
129
|
+
const dir = copyProject(root, { exclude: options.exclude, id: `mutant-${i}` })
|
|
130
|
+
try {
|
|
131
|
+
for (let m; (m = queue.shift());) {
|
|
132
|
+
if (performance.now() > deadline) {
|
|
133
|
+
result.mutants.sampled = true
|
|
134
|
+
queue.length = 0
|
|
135
|
+
break
|
|
136
|
+
}
|
|
137
|
+
writeFiles(dir, { [m.file]: apply(texts[m.file], m) })
|
|
138
|
+
const res = await runVitest(dir, files, { bin, config: options.config, timeoutMs })
|
|
139
|
+
writeFiles(dir, { [m.file]: texts[m.file] })
|
|
140
|
+
outcomes.push({ m, res })
|
|
141
|
+
}
|
|
142
|
+
} finally {
|
|
143
|
+
removeCopy(dir)
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
await Promise.all(Array.from({ length: Math.min(concurrency, list.length) }, (_, i) => worker(i)))
|
|
147
|
+
return outcomes
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function recordMutants({ outcomes, result, compileError, passing, allowed, diagnostics }) {
|
|
151
|
+
for (const { m, res } of outcomes) {
|
|
152
|
+
result.mutants.run++
|
|
153
|
+
if (res.timedOut) {
|
|
154
|
+
result.mutants.timedOut++
|
|
155
|
+
result.mutants.killed++
|
|
156
|
+
continue
|
|
157
|
+
}
|
|
158
|
+
const loadOnly = res.tests.size === 0 && res.loadErrors.size > 0
|
|
159
|
+
if (loadOnly && [...res.loadErrors.values()].some(compileError)) {
|
|
160
|
+
result.mutants.invalid++
|
|
161
|
+
continue
|
|
162
|
+
}
|
|
163
|
+
let killed = false
|
|
164
|
+
for (const t of passing) {
|
|
165
|
+
const st = res.loadErrors.has(t.file)
|
|
166
|
+
? 'failed'
|
|
167
|
+
: res.tests.get(`${t.file}::${t.name}`)?.status
|
|
168
|
+
if (st && st !== 'passed') {
|
|
169
|
+
t.kills++
|
|
170
|
+
killed = true
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (killed) result.mutants.killed++
|
|
174
|
+
else {
|
|
175
|
+
result.mutants.survived++
|
|
176
|
+
result.survivors.push({
|
|
177
|
+
file: m.file,
|
|
178
|
+
line: m.line,
|
|
179
|
+
column: m.column,
|
|
180
|
+
op: m.op,
|
|
181
|
+
original: m.original.slice(0, 80),
|
|
182
|
+
replacement: m.replacement,
|
|
183
|
+
suggestion: suggest(m),
|
|
184
|
+
})
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const valid = result.mutants.run - result.mutants.invalid
|
|
188
|
+
if (valid > 0)
|
|
189
|
+
for (const t of passing) {
|
|
190
|
+
if (t.kills > 0 || t.before === 'passed') continue
|
|
191
|
+
if (allowed('T011', t.file, t.line)) {
|
|
192
|
+
result.notes.push(`T011 allowed on "${t.name}"`)
|
|
193
|
+
continue
|
|
194
|
+
}
|
|
195
|
+
const partial = result.mutants.sampled
|
|
196
|
+
diagnostics.push(
|
|
197
|
+
diagnostic({
|
|
198
|
+
code: 'T011',
|
|
199
|
+
severity: partial ? 'warning' : 'error',
|
|
200
|
+
file: t.file,
|
|
201
|
+
span: [t.line, 1, t.line, 1],
|
|
202
|
+
why: `"${t.name}" killed none of the ${valid} mutants of the changed lines${partial ? ` (a sample of ${result.mutants.generated})` : ''}: it would still pass if that code were wrong`,
|
|
203
|
+
help: result.survivors[0]
|
|
204
|
+
? `Assert what the changed code computes. For example: ${result.survivors[0].suggestion}`
|
|
205
|
+
: 'Assert what the changed code computes',
|
|
206
|
+
}),
|
|
207
|
+
)
|
|
208
|
+
}
|
|
209
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function resolver(testFiles, cases) {
|
|
2
|
+
const resolveCases = (res) => {
|
|
3
|
+
// Static names map 1:1; results nobody claimed belong to the file's dynamic (`.each`) cases.
|
|
4
|
+
const out = new Map()
|
|
5
|
+
for (const f of testFiles) {
|
|
6
|
+
const mine = cases.filter((c) => c.file === f)
|
|
7
|
+
const keys = [...res.tests.keys()].filter((k) => k.startsWith(`${f}::`))
|
|
8
|
+
for (const c of mine.filter((x) => !x.dynamic))
|
|
9
|
+
out.set(
|
|
10
|
+
`${f}::${c.name}`,
|
|
11
|
+
res.loadErrors.has(f)
|
|
12
|
+
? { status: 'failed', message: res.loadErrors.get(f), load: true }
|
|
13
|
+
: (res.tests.get(`${f}::${c.name}`) ?? { status: 'missing' }),
|
|
14
|
+
)
|
|
15
|
+
if (mine.some((c) => c.dynamic))
|
|
16
|
+
for (const k of keys) if (!out.has(k)) out.set(k, res.tests.get(k))
|
|
17
|
+
}
|
|
18
|
+
return out
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return resolveCases
|
|
22
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { sortDiagnostics, summarize } from '../diag.mjs'
|
|
2
|
+
|
|
3
|
+
export function makeResult({
|
|
4
|
+
root,
|
|
5
|
+
options,
|
|
6
|
+
tests,
|
|
7
|
+
support,
|
|
8
|
+
source,
|
|
9
|
+
diagnostics,
|
|
10
|
+
budgetSec,
|
|
11
|
+
started,
|
|
12
|
+
}) {
|
|
13
|
+
const result = {
|
|
14
|
+
version: 1,
|
|
15
|
+
ok: true,
|
|
16
|
+
root,
|
|
17
|
+
base: options.changes ? null : (options.base ?? 'HEAD'),
|
|
18
|
+
files: {
|
|
19
|
+
tests: tests.map((c) => c.rel),
|
|
20
|
+
support: support.map((c) => c.rel),
|
|
21
|
+
source: source.map((c) => c.rel),
|
|
22
|
+
},
|
|
23
|
+
tests: [],
|
|
24
|
+
mutants: {
|
|
25
|
+
generated: 0,
|
|
26
|
+
run: 0,
|
|
27
|
+
killed: 0,
|
|
28
|
+
survived: 0,
|
|
29
|
+
timedOut: 0,
|
|
30
|
+
invalid: 0,
|
|
31
|
+
sampled: false,
|
|
32
|
+
budgetSec,
|
|
33
|
+
},
|
|
34
|
+
survivors: [],
|
|
35
|
+
diagnostics,
|
|
36
|
+
summary: null,
|
|
37
|
+
notes: [],
|
|
38
|
+
ms: 0,
|
|
39
|
+
}
|
|
40
|
+
const finish = () => {
|
|
41
|
+
sortDiagnostics(diagnostics)
|
|
42
|
+
// Each test's verdict: it ran green on the change, failed before it (when there was a before), and no error
|
|
43
|
+
// diagnostic falls inside it.
|
|
44
|
+
for (const t of result.tests) {
|
|
45
|
+
t.codes = diagnostics
|
|
46
|
+
.filter(
|
|
47
|
+
(d) =>
|
|
48
|
+
d.severity === 'error' &&
|
|
49
|
+
d.file === t.file &&
|
|
50
|
+
d.span[0] >= t.line &&
|
|
51
|
+
d.span[0] <= t.end,
|
|
52
|
+
)
|
|
53
|
+
.map((d) => d.code)
|
|
54
|
+
t.accepted = t.after === 'passed' && t.before !== 'passed' && !t.codes.length
|
|
55
|
+
}
|
|
56
|
+
result.summary = { changedTests: result.tests.length, ...summarize(diagnostics) }
|
|
57
|
+
result.ok = result.summary.errors === 0 && result.tests.every((t) => t.accepted)
|
|
58
|
+
result.ms = Math.round(performance.now() - started)
|
|
59
|
+
return result
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return { result, finish }
|
|
63
|
+
}
|
package/src/gate/run.mjs
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { runVitest, vitestBin } from '../workspace.mjs'
|
|
2
|
+
import { afterRun } from './after.mjs'
|
|
3
|
+
import { beforeRun } from './before.mjs'
|
|
4
|
+
import { mutate } from './mutation.mjs'
|
|
5
|
+
|
|
6
|
+
export async function runGate({
|
|
7
|
+
root,
|
|
8
|
+
options,
|
|
9
|
+
cases,
|
|
10
|
+
testFiles,
|
|
11
|
+
log,
|
|
12
|
+
result,
|
|
13
|
+
source,
|
|
14
|
+
allowed,
|
|
15
|
+
diagnostics,
|
|
16
|
+
importsOf,
|
|
17
|
+
code,
|
|
18
|
+
posix,
|
|
19
|
+
isSupport,
|
|
20
|
+
maxMutants,
|
|
21
|
+
concurrency,
|
|
22
|
+
budgetSec,
|
|
23
|
+
started,
|
|
24
|
+
compileError,
|
|
25
|
+
}) {
|
|
26
|
+
const bin = vitestBin(root)
|
|
27
|
+
const run = (dir, files) =>
|
|
28
|
+
runVitest(dir, files, { bin, config: options.config, timeoutMs: 180_000 })
|
|
29
|
+
|
|
30
|
+
const { after, resolveCases, byKey } = await afterRun({
|
|
31
|
+
cases,
|
|
32
|
+
testFiles,
|
|
33
|
+
log,
|
|
34
|
+
root,
|
|
35
|
+
options,
|
|
36
|
+
run,
|
|
37
|
+
result,
|
|
38
|
+
})
|
|
39
|
+
await beforeRun({
|
|
40
|
+
source,
|
|
41
|
+
options,
|
|
42
|
+
log,
|
|
43
|
+
root,
|
|
44
|
+
run,
|
|
45
|
+
testFiles,
|
|
46
|
+
resolveCases,
|
|
47
|
+
byKey,
|
|
48
|
+
allowed,
|
|
49
|
+
result,
|
|
50
|
+
diagnostics,
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
await mutate({
|
|
54
|
+
options,
|
|
55
|
+
root,
|
|
56
|
+
source,
|
|
57
|
+
testFiles,
|
|
58
|
+
importsOf,
|
|
59
|
+
code,
|
|
60
|
+
posix,
|
|
61
|
+
isSupport,
|
|
62
|
+
maxMutants,
|
|
63
|
+
byKey,
|
|
64
|
+
result,
|
|
65
|
+
log,
|
|
66
|
+
concurrency,
|
|
67
|
+
budgetSec,
|
|
68
|
+
started,
|
|
69
|
+
after,
|
|
70
|
+
bin,
|
|
71
|
+
compileError,
|
|
72
|
+
allowed,
|
|
73
|
+
diagnostics,
|
|
74
|
+
})
|
|
75
|
+
}
|
package/src/gate.mjs
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// `kerf test --gate`: a new or changed test has to prove it tests something (PLAN §7b, research 05 §5.1, research 22).
|
|
2
|
+
//
|
|
3
|
+
// 1. Fail-before (T010): each new or changed test fails against the code before the change, and passes after it.
|
|
4
|
+
// 2. Mutation on changed lines (T011): each new test kills at least one mutant of the changed source lines.
|
|
5
|
+
// Surviving mutants come back with what an assertion that kills them would check.
|
|
6
|
+
// 3. No silent weakening (T020): an existing test deleted, skipped or loosened needs `// kerf-allow T020: <reason>`.
|
|
7
|
+
// Plus the static T-codes on the changed test files, with T005 (implementation queries) blocking for new tests.
|
|
8
|
+
//
|
|
9
|
+
// Tests run in scratch copies of the project (`.kerf/gate/`), never in the working tree. With no source change
|
|
10
|
+
// (tests added for existing code) there is no "before" to fail on: fail-before is skipped and the mutants come from
|
|
11
|
+
// the source files the new tests import.
|
|
12
|
+
|
|
13
|
+
import { execFileSync } from 'node:child_process'
|
|
14
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
15
|
+
import { availableParallelism } from 'node:os'
|
|
16
|
+
import { join, resolve } from 'node:path'
|
|
17
|
+
import { diagnostic } from './diag.mjs'
|
|
18
|
+
import { runGate } from './gate/run.mjs'
|
|
19
|
+
import { classify } from './gate/classify.mjs'
|
|
20
|
+
import { makeResult } from './gate/result.mjs'
|
|
21
|
+
import { loadConfig } from './load-config.mjs'
|
|
22
|
+
import { testCases } from './testdiff.mjs'
|
|
23
|
+
import { resolveImport } from './tests.mjs'
|
|
24
|
+
|
|
25
|
+
const code = (rel) => /\.[cm]?[jt]sx?$/.test(rel) && !/\.d\.[cm]?ts$/.test(rel)
|
|
26
|
+
const configFile = (rel) =>
|
|
27
|
+
/(^|\/)(vite|vitest|kerf|tsup|eslint|playwright)\.config\.[cm]?[jt]s$/.test(rel)
|
|
28
|
+
const posix = (p) => p.split('\\').join('/')
|
|
29
|
+
|
|
30
|
+
/** Changed files between `base` and the working tree, from git (paths relative to `root`). */
|
|
31
|
+
export function gitChanges(root, base = 'HEAD') {
|
|
32
|
+
const git = (...args) =>
|
|
33
|
+
execFileSync('git', ['-C', root, ...args], {
|
|
34
|
+
encoding: 'utf8',
|
|
35
|
+
maxBuffer: 1 << 28,
|
|
36
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
37
|
+
})
|
|
38
|
+
const list = [
|
|
39
|
+
...git('diff', '--name-only', '--relative', '--no-renames', base).split('\n'),
|
|
40
|
+
...git('ls-files', '--others', '--exclude-standard').split('\n'),
|
|
41
|
+
]
|
|
42
|
+
const rels = [...new Set(list.filter((r) => r && !r.startsWith('.kerf/')))]
|
|
43
|
+
return rels
|
|
44
|
+
.map((rel) => {
|
|
45
|
+
let before = null
|
|
46
|
+
try {
|
|
47
|
+
before = git('show', `${base}:./${rel}`)
|
|
48
|
+
} catch {}
|
|
49
|
+
const abs = join(root, rel)
|
|
50
|
+
return { rel, before, after: existsSync(abs) ? readFileSync(abs, 'utf8') : null }
|
|
51
|
+
})
|
|
52
|
+
.filter((c) => c.before !== c.after)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const importRe = /(?:from\s*|import\s*\(\s*|import\s+)['"]([^'"]+)['"]/g
|
|
56
|
+
const typeOnly = /^\s*(?:import|export)\s+type\b[^;'"]*from\s*['"][^'"]+['"];?/gm
|
|
57
|
+
/** Runtime imports only: `import type` brings no code a test could run. */
|
|
58
|
+
function importsOf(abs, text) {
|
|
59
|
+
const out = []
|
|
60
|
+
for (const m of text.replace(typeOnly, '').matchAll(importRe)) {
|
|
61
|
+
const p = resolveImport(abs, m[1])
|
|
62
|
+
if (p) out.push(p)
|
|
63
|
+
}
|
|
64
|
+
return out
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @param {string} root project root (the "after" state)
|
|
69
|
+
* @param {{
|
|
70
|
+
* base?: string, changes?: { rel: string, before: string | null, after: string | null }[],
|
|
71
|
+
* budgetSec?: number, maxMutants?: number, concurrency?: number, refactor?: boolean, mutation?: boolean,
|
|
72
|
+
* exclude?: (rel: string) => boolean, config?: string, log?: (line: string) => void,
|
|
73
|
+
* }} [options] `changes` replaces the git diff (the eval harness passes its own); `exclude` leaves paths out of the copies
|
|
74
|
+
*/
|
|
75
|
+
export async function gate(root, options = {}) {
|
|
76
|
+
const started = performance.now()
|
|
77
|
+
root = resolve(root)
|
|
78
|
+
const log = options.log ?? (() => {})
|
|
79
|
+
const { config } = await loadConfig(root).catch(() => ({ config: {} }))
|
|
80
|
+
const gateCfg = config.test?.gate ?? {}
|
|
81
|
+
const budgetSec = options.budgetSec ?? gateCfg.mutationBudgetSec ?? 60
|
|
82
|
+
const maxMutants = options.maxMutants ?? gateCfg.maxMutants ?? 40
|
|
83
|
+
const concurrency = Math.max(
|
|
84
|
+
1,
|
|
85
|
+
options.concurrency ?? Math.min(4, Math.floor(availableParallelism() / 3) || 1),
|
|
86
|
+
)
|
|
87
|
+
const changes = (options.changes ?? gitChanges(root, options.base ?? 'HEAD')).map((c) => ({
|
|
88
|
+
...c,
|
|
89
|
+
rel: posix(c.rel),
|
|
90
|
+
}))
|
|
91
|
+
|
|
92
|
+
const { tests, support, source, isSupport, diagnostics, allowed, cases, testFiles } = classify(
|
|
93
|
+
root,
|
|
94
|
+
changes,
|
|
95
|
+
code,
|
|
96
|
+
configFile,
|
|
97
|
+
importsOf,
|
|
98
|
+
)
|
|
99
|
+
const { result, finish } = makeResult({
|
|
100
|
+
root,
|
|
101
|
+
options,
|
|
102
|
+
tests,
|
|
103
|
+
support,
|
|
104
|
+
source,
|
|
105
|
+
diagnostics,
|
|
106
|
+
budgetSec,
|
|
107
|
+
started,
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
if (options.refactor) {
|
|
111
|
+
if (cases.length)
|
|
112
|
+
for (const c of cases)
|
|
113
|
+
diagnostics.push(
|
|
114
|
+
diagnostic({
|
|
115
|
+
code: 'T020',
|
|
116
|
+
file: c.file,
|
|
117
|
+
span: [c.line, 1, c.line, 1],
|
|
118
|
+
why: `--refactor promises behavior didn't change, but test "${c.name}" is new or edited`,
|
|
119
|
+
help: 'Drop --refactor, or leave the tests alone',
|
|
120
|
+
}),
|
|
121
|
+
)
|
|
122
|
+
if (!cases.length) {
|
|
123
|
+
result.notes.push('refactor: no test changed; run the whole suite to confirm behavior')
|
|
124
|
+
return finish()
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (!cases.length) {
|
|
128
|
+
result.notes.push('no new or changed tests')
|
|
129
|
+
return finish()
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
await runGate({
|
|
133
|
+
root,
|
|
134
|
+
options,
|
|
135
|
+
cases,
|
|
136
|
+
testFiles,
|
|
137
|
+
log,
|
|
138
|
+
result,
|
|
139
|
+
source,
|
|
140
|
+
allowed,
|
|
141
|
+
diagnostics,
|
|
142
|
+
importsOf,
|
|
143
|
+
code,
|
|
144
|
+
posix,
|
|
145
|
+
isSupport,
|
|
146
|
+
maxMutants,
|
|
147
|
+
concurrency,
|
|
148
|
+
budgetSec,
|
|
149
|
+
started,
|
|
150
|
+
compileError,
|
|
151
|
+
})
|
|
152
|
+
return finish()
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** A mutant the compiler or parser rejects never ran, so it counts as neither killed nor survived. */
|
|
156
|
+
const compileError = (msg) =>
|
|
157
|
+
/\bW\d{3}\b|SyntaxError|Transform failed|Parse error|Unexpected token|Expected/.test(msg)
|
|
158
|
+
|
|
159
|
+
/** Human summary for stderr. */
|
|
160
|
+
export function formatGate(r) {
|
|
161
|
+
const lines = []
|
|
162
|
+
for (const t of r.tests) {
|
|
163
|
+
const mark = t.accepted ? '✓' : '✗'
|
|
164
|
+
lines.push(
|
|
165
|
+
`${mark} ${t.file} › ${t.name} before: ${t.before ?? '—'}, after: ${t.after}, kills: ${t.kills}${t.codes?.length ? `, ${t.codes.join(' ')}` : ''}${t.after !== 'passed' && t.afterMessage ? `\n ${t.afterMessage}` : ''}`,
|
|
166
|
+
)
|
|
167
|
+
}
|
|
168
|
+
const m = r.mutants
|
|
169
|
+
if (m.run)
|
|
170
|
+
lines.push(
|
|
171
|
+
`mutants: ${m.killed}/${m.run - m.invalid} killed${m.timedOut ? ` (${m.timedOut} by timeout)` : ''}${m.invalid ? `, ${m.invalid} didn't compile` : ''}${m.sampled ? `, sampled from ${m.generated}` : ''}`,
|
|
172
|
+
)
|
|
173
|
+
for (const s of r.survivors.slice(0, 8)) lines.push(` survived: ${s.suggestion}`)
|
|
174
|
+
for (const n of r.notes) lines.push(`note: ${n}`)
|
|
175
|
+
return lines.join('\n')
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export { testCases }
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { agentsMd, writeAgentsMd } from './agents-md.mjs'
|
|
2
|
+
export { catalog, entry, probe } from './catalog.mjs'
|
|
3
|
+
export { check } from './check.mjs'
|
|
4
|
+
export { defineConfig } from './config.mjs'
|
|
5
|
+
export { docs, topics } from './docs.mjs'
|
|
6
|
+
export { loadConfig } from './load-config.mjs'
|
|
7
|
+
export { handle, serve, tools } from './mcp.mjs'
|
|
8
|
+
export { bindSlots, slotTable } from './slots.mjs'
|
|
9
|
+
export { tasks } from './tasks.mjs'
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// Map proven JSX box/value assignability errors. Type names alone are not evidence:
|
|
2
|
+
// value members must come from the resolved core State/Derived exports.
|
|
3
|
+
const { coreMembers, known } = require('./core-boxes.cjs')
|
|
4
|
+
const assignmentCodes = new Set([2322, 2739, 2740, 2741])
|
|
5
|
+
|
|
6
|
+
function boxKind(ts, checker, type, members) {
|
|
7
|
+
if (!known(ts, type)) return null
|
|
8
|
+
if (type.isUnion()) {
|
|
9
|
+
const nullable = ts.TypeFlags.Null | ts.TypeFlags.Undefined
|
|
10
|
+
const parts = type.types.filter((part) => !(part.flags & nullable))
|
|
11
|
+
const kinds = new Set(parts.map((part) => boxKind(ts, checker, part, members)))
|
|
12
|
+
return kinds.size === 1 ? [...kinds][0] : null
|
|
13
|
+
}
|
|
14
|
+
if (members.roots.has(type.getSymbol()) || members.roots.has(type.aliasSymbol)) return 'box'
|
|
15
|
+
const value = checker.getPropertyOfType(type, 'value')
|
|
16
|
+
const declarations = value?.getDeclarations() ?? []
|
|
17
|
+
const names = new Set(checker.getPropertiesOfType(type).map((property) => property.name))
|
|
18
|
+
// A projection of just .value is not evidence of the complete box contract.
|
|
19
|
+
return members.shapes.some(
|
|
20
|
+
(shape) =>
|
|
21
|
+
shape.names.length > 1 &&
|
|
22
|
+
shape.names.every((name) => names.has(name)) &&
|
|
23
|
+
declarations.some((declaration) => shape.declarations.has(declaration)),
|
|
24
|
+
)
|
|
25
|
+
? 'box'
|
|
26
|
+
: 'value'
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function attribute(ts, file, start) {
|
|
30
|
+
let node = ts.getTokenAtPosition(file, start)
|
|
31
|
+
while (node && !ts.isJsxAttribute(node)) node = node.parent
|
|
32
|
+
if (!node || !node.initializer || !ts.isJsxExpression(node.initializer)) return null
|
|
33
|
+
const expression = node.initializer.expression
|
|
34
|
+
const opening = node.parent.parent
|
|
35
|
+
if (!expression || !opening.tagName || start > expression.getStart(file)) return null
|
|
36
|
+
return { expression, tag: opening.tagName }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Scalar aliases and synthesized boxes stay opaque; no complete contract is proven there.
|
|
40
|
+
function originatingBox(ts, expression) {
|
|
41
|
+
while (
|
|
42
|
+
ts.isParenthesizedExpression(expression) ||
|
|
43
|
+
ts.isAsExpression(expression) ||
|
|
44
|
+
ts.isTypeAssertionExpression(expression) ||
|
|
45
|
+
ts.isNonNullExpression(expression) ||
|
|
46
|
+
ts.isSatisfiesExpression(expression)
|
|
47
|
+
)
|
|
48
|
+
expression = expression.expression
|
|
49
|
+
if (ts.isPropertyAccessExpression(expression) && expression.name.text === 'value')
|
|
50
|
+
return expression.expression
|
|
51
|
+
if (
|
|
52
|
+
ts.isElementAccessExpression(expression) &&
|
|
53
|
+
expression.argumentExpression &&
|
|
54
|
+
ts.isStringLiteralLike(expression.argumentExpression) &&
|
|
55
|
+
expression.argumentExpression.text === 'value'
|
|
56
|
+
)
|
|
57
|
+
return expression.expression
|
|
58
|
+
return null
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** A core diagnostic only when changing box/value direction explains the type error. */
|
|
62
|
+
function boxMismatch(ts, program, diagnostic) {
|
|
63
|
+
if (!assignmentCodes.has(diagnostic.code) || !diagnostic.file || diagnostic.start === undefined)
|
|
64
|
+
return null
|
|
65
|
+
const site = attribute(ts, diagnostic.file, diagnostic.start)
|
|
66
|
+
if (!site) return null
|
|
67
|
+
const checker = program.getTypeChecker()
|
|
68
|
+
const tag = checker.getTypeAtLocation(site.tag)
|
|
69
|
+
if (!tag.getCallSignatures().length && !tag.getConstructSignatures().length) return null
|
|
70
|
+
const actual = checker.getTypeAtLocation(site.expression)
|
|
71
|
+
const expected = checker.getContextualType(site.expression)
|
|
72
|
+
const members = coreMembers(ts, program, checker)
|
|
73
|
+
const given = boxKind(ts, checker, actual, members)
|
|
74
|
+
const wanted = boxKind(ts, checker, expected, members)
|
|
75
|
+
if (!given || !wanted || given === wanted || checker.isTypeAssignableTo(actual, expected))
|
|
76
|
+
return null
|
|
77
|
+
const box = checker.getNonNullableType(given === 'box' ? actual : expected)
|
|
78
|
+
const value = checker.getTypeOfPropertyOfType(box, 'value')
|
|
79
|
+
if (!known(ts, value)) return null
|
|
80
|
+
const from = given === 'box' ? value : actual
|
|
81
|
+
const to = given === 'box' ? expected : value
|
|
82
|
+
if (!checker.isTypeAssignableTo(from, to)) return null
|
|
83
|
+
if (given === 'value') {
|
|
84
|
+
const origin = originatingBox(ts, site.expression)
|
|
85
|
+
const original = origin && checker.getTypeAtLocation(origin)
|
|
86
|
+
if (
|
|
87
|
+
boxKind(ts, checker, original, members) !== 'box' ||
|
|
88
|
+
!checker.isTypeAssignableTo(original, expected)
|
|
89
|
+
)
|
|
90
|
+
return null
|
|
91
|
+
}
|
|
92
|
+
const text = site.expression.getText(diagnostic.file)
|
|
93
|
+
return {
|
|
94
|
+
code: 'W008',
|
|
95
|
+
severity: 'error',
|
|
96
|
+
why:
|
|
97
|
+
given === 'box'
|
|
98
|
+
? `This component prop expects a value, but \`${text}\` is a core box.`
|
|
99
|
+
: `This component prop expects a core box, but \`${text}\` is a value.`,
|
|
100
|
+
help:
|
|
101
|
+
given === 'box'
|
|
102
|
+
? `Read the box's \`.value\` before passing it to this prop; handle null or undefined first if needed.`
|
|
103
|
+
: 'Pass the State/Derived box itself, for example `count` instead of `count.value`.',
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function canMap(file, diagnostic) {
|
|
108
|
+
return diagnostic.code === 2540 || (file.endsWith('.tsx') && assignmentCodes.has(diagnostic.code))
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Keep native inference; lazily map core box prop and readonly errors with the existing JS host. */
|
|
112
|
+
function withBoxProps(host, createFallback) {
|
|
113
|
+
let fallback
|
|
114
|
+
return {
|
|
115
|
+
...host,
|
|
116
|
+
update(files, touched = []) {
|
|
117
|
+
host.update(files, touched)
|
|
118
|
+
fallback?.update(files, touched)
|
|
119
|
+
},
|
|
120
|
+
diagnostics(file) {
|
|
121
|
+
const list = host.diagnostics(file)
|
|
122
|
+
if (!list.some((diagnostic) => canMap(file, diagnostic))) return list
|
|
123
|
+
fallback ??= createFallback()
|
|
124
|
+
const mapped = fallback.diagnostics(file).filter((diagnostic) => diagnostic.kerf)
|
|
125
|
+
return list.map((diagnostic) => {
|
|
126
|
+
if (!canMap(file, diagnostic)) return diagnostic
|
|
127
|
+
const match = mapped.find(
|
|
128
|
+
(candidate) => candidate.start === diagnostic.start && candidate.code === diagnostic.code,
|
|
129
|
+
)
|
|
130
|
+
return match ? { ...diagnostic, kerf: match.kerf } : diagnostic
|
|
131
|
+
})
|
|
132
|
+
},
|
|
133
|
+
close() {
|
|
134
|
+
host.close()
|
|
135
|
+
fallback?.close()
|
|
136
|
+
},
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
module.exports = { boxMismatch, withBoxProps }
|