gatelight 0.0.0-stage → 0.17.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/.antigravity-plugin/plugin.json +9 -0
  2. package/.claude-plugin/plugin.json +11 -0
  3. package/.codex-plugin/plugin.json +18 -0
  4. package/.mcp.json +8 -0
  5. package/bin/modwall +8 -0
  6. package/bin/node.sh +14 -0
  7. package/bin/testcraft +8 -0
  8. package/bin/testpace +8 -0
  9. package/entry/command +44 -0
  10. package/hooks/antigravity.sh +24 -0
  11. package/hooks/codex.json +50 -0
  12. package/hooks/codex.sh +7 -0
  13. package/hooks/hooks.json +43 -0
  14. package/hooks/testpace.ts +25 -0
  15. package/hooks.json +16 -0
  16. package/mcp_config.json +8 -0
  17. package/package.json +21 -4
  18. package/setup.mjs +99 -0
  19. package/skills/merge/SKILL.md +42 -0
  20. package/skills/modular/SKILL.md +75 -0
  21. package/skills/setup/SKILL.md +101 -0
  22. package/skills/setup-modwall/SKILL.md +112 -0
  23. package/skills/setup-testcraft/SKILL.md +23 -0
  24. package/skills/setup-testcraft/checklist/dev-login.md +20 -0
  25. package/skills/write-tests/SKILL.md +153 -0
  26. package/src/base/agent.ts +100 -0
  27. package/src/base/baseline.ts +54 -0
  28. package/src/base/commandline.ts +40 -0
  29. package/src/base/commands.ts +94 -0
  30. package/src/base/confirm.ts +161 -0
  31. package/src/base/contract.ts +94 -0
  32. package/src/base/coverage.ts +46 -0
  33. package/src/base/entry.ts +90 -0
  34. package/src/base/excerpt.ts +46 -0
  35. package/src/base/execute.ts +70 -0
  36. package/src/base/exit.ts +6 -0
  37. package/src/base/format.ts +27 -0
  38. package/src/base/full.ts +160 -0
  39. package/src/base/gate.ts +251 -0
  40. package/src/base/git.ts +361 -0
  41. package/src/base/glob.ts +38 -0
  42. package/src/base/hook.ts +297 -0
  43. package/src/base/hookformat.ts +257 -0
  44. package/src/base/judging.ts +27 -0
  45. package/src/base/layermap.ts +69 -0
  46. package/src/base/module.ts +88 -0
  47. package/src/base/process-group.ts +101 -0
  48. package/src/base/records.ts +66 -0
  49. package/src/base/repo.ts +155 -0
  50. package/src/base/sessions.ts +55 -0
  51. package/src/base/shell.ts +176 -0
  52. package/src/base/stop.ts +100 -0
  53. package/src/base/store.ts +348 -0
  54. package/src/base/version.ts +24 -0
  55. package/src/cli.ts +186 -0
  56. package/src/craft/change.ts +24 -0
  57. package/src/craft/checklist.ts +157 -0
  58. package/src/craft/commands.ts +111 -0
  59. package/src/craft/config.ts +15 -0
  60. package/src/craft/full.ts +30 -0
  61. package/src/craft/index.ts +12 -0
  62. package/src/craft/session.ts +21 -0
  63. package/src/mcp.ts +79 -0
  64. package/src/modules.ts +10 -0
  65. package/src/modwall.ts +8 -0
  66. package/src/pace/commands.ts +281 -0
  67. package/src/pace/config.ts +103 -0
  68. package/src/pace/consent.ts +81 -0
  69. package/src/pace/focused.ts +208 -0
  70. package/src/pace/full.ts +38 -0
  71. package/src/pace/index.ts +16 -0
  72. package/src/pace/record.ts +84 -0
  73. package/src/pace/report.ts +335 -0
  74. package/src/pace/results.ts +243 -0
  75. package/src/pace/run/node-test-reporter.mjs +34 -0
  76. package/src/pace/run/node-test.ts +26 -0
  77. package/src/pace/run/playwright-reporter.mjs +48 -0
  78. package/src/pace/run/playwright.ts +34 -0
  79. package/src/pace/run/runner.ts +157 -0
  80. package/src/pace/run/targets.ts +130 -0
  81. package/src/pace/run/vitest-launcher.mjs +101 -0
  82. package/src/pace/run/vitest.ts +22 -0
  83. package/src/pace/session.ts +35 -0
  84. package/src/pace/testlist.ts +91 -0
  85. package/src/pace/tests.ts +22 -0
  86. package/src/pace/verify.ts +95 -0
  87. package/src/testcraft.ts +9 -0
  88. package/src/wall/check.ts +258 -0
  89. package/src/wall/commands.ts +279 -0
  90. package/src/wall/config.ts +394 -0
  91. package/src/wall/database.ts +136 -0
  92. package/src/wall/dbcheck.ts +389 -0
  93. package/src/wall/dbrules.ts +213 -0
  94. package/src/wall/edit.ts +64 -0
  95. package/src/wall/files.ts +35 -0
  96. package/src/wall/full.ts +30 -0
  97. package/src/wall/index.ts +15 -0
  98. package/src/wall/init.ts +149 -0
  99. package/src/wall/layermap.ts +124 -0
  100. package/src/wall/layout.ts +231 -0
  101. package/src/wall/loosen.ts +157 -0
  102. package/src/wall/miniprogram.ts +80 -0
  103. package/src/wall/project.ts +88 -0
  104. package/src/wall/rules.ts +400 -0
  105. package/src/wall/session.ts +198 -0
  106. package/src/wall/sql.ts +510 -0
  107. package/src/wall/store.ts +39 -0
  108. package/README.md +0 -3
@@ -0,0 +1,48 @@
1
+ // testpace run playwright 的计数报告器:经 PW_TEST_REPORTER 加在项目自己的报告器后面(不换掉它们),
2
+ // 跑完把每个跑过或被跳过的测试往 TESTPACE_REPORT 写一行 {file, line, ancestors, name, status, retried}(见 targets.ts 的 Ran),testpace 按给的文件和行核对。
3
+ // 由项目的 Playwright 加载,写成 .mjs,不靠 node 直接运行 TypeScript。
4
+ //
5
+ // 配置写错时 Playwright 不起报告器,不写报告,testpace 不核对;全局准备(globalSetup)出错时选好了测试、一个没跑,照样写。
6
+ // 只算选中的测试(没被行选中的 Playwright 不放进来)。一个结果都没有的是没跑到(提前停下、依赖的准备项目没过);
7
+ // 被跳过但本来要跑的(serial 模式里前面的没过,后面的就不跑了)也是没跑到,不算声明了跳过。
8
+ // 多个 project 时同一个测试各算一次,名字前带上 project 的名字;重试的按最后一次算,重试后才过的记 retried。
9
+ import { writeFileSync } from 'node:fs'
10
+
11
+ export default class TestpaceCount {
12
+ // 不往屏幕上写:Playwright 见所有报告器都不写屏幕时会自己补一个,加了它不改变项目原来的输出
13
+ printsToStdio() {
14
+ return false
15
+ }
16
+
17
+ onBegin(_config, suite) {
18
+ this.suite = suite
19
+ }
20
+
21
+ onEnd() {
22
+ const lines = []
23
+ for (const test of this.suite.allTests()) {
24
+ const last = test.results.at(-1)
25
+ if (!last) continue
26
+ let status
27
+ if (last.status === 'skipped') {
28
+ if (test.expectedStatus !== 'skipped') continue
29
+ status = 'skipped'
30
+ } else {
31
+ status = last.status === test.expectedStatus ? 'passed' : 'failed'
32
+ }
33
+ const ancestors = []
34
+ const titles = [test.title]
35
+ let parent = test.parent
36
+ for (; parent?.type === 'describe'; parent = parent.parent) {
37
+ ancestors.push(parent.location.line)
38
+ titles.unshift(parent.title)
39
+ }
40
+ // 往上是文件,再往上是 project
41
+ const project = parent?.parent?.type === 'project' ? parent.parent.title : ''
42
+ const name = `${project ? `[${project}] ` : ''}${titles.join(' > ')}`
43
+ const retried = status === 'passed' && test.outcome() === 'flaky'
44
+ lines.push(JSON.stringify({ file: test.location.file, line: test.location.line, ancestors, name, status, ...(retried ? { retried } : {}) }))
45
+ }
46
+ writeFileSync(process.env.TESTPACE_REPORT, lines.map(line => `${line}\n`).join(''))
47
+ }
48
+ }
@@ -0,0 +1,34 @@
1
+ // testpace run playwright:用项目自己装的 Playwright(从 --cwd 找 @playwright/test)跑给定的测试文件,守 testpace 对 files 的约定。
2
+ // 项目配置里的报告器(比如断言检查)照常起作用:计数报告器经 PW_TEST_REPORTER 加在它们后面,不用命令行的 --reporter(会把配置里的换掉)。
3
+ // Playwright 把文件参数当正则,路径里的 ( + . 这些先转义。按行给的交给它按行跑(同一个文件也给了整个文件时整个跑,见 selectors)。
4
+ // Playwright 只在一个测试都没选到时才失败:给的行上没有测试、文件里没有测试、文件被配置排除了,只要别的给的文件或行有测试就照样通过。
5
+ // 这些靠逐个文件、逐行核对查出来。它按名字片段(正则)找文件,`a.spec.ts:4` 也会去 `a.spec.tsx` 找第 4 行:核对只认给的那个文件,
6
+ // 别的文件跑到的测试不算数,只说一句。依赖的准备项目(setup)里的测试也算成没给、也跑了的文件。
7
+
8
+ import { fileURLToPath } from 'node:url'
9
+ import { type Framework, projectBin } from './runner.ts'
10
+ import { selectors } from './targets.ts'
11
+
12
+ const REPORTER = fileURLToPath(new URL('./playwright-reporter.mjs', import.meta.url))
13
+
14
+ const escape = (path: string): string => path.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
15
+
16
+ export const playwright: Framework = {
17
+ usage: 'testpace run playwright [--cwd 目录] [--playwright-arg 参数]... 文件[:行号[:列号]]...',
18
+ argOption: '--playwright-arg',
19
+ byLine: true,
20
+ command(targets, args, cwd, report) {
21
+ const bin = projectBin(cwd, '@playwright/test', 'playwright')
22
+ if (typeof bin === 'string') return bin
23
+ return {
24
+ program: 'node',
25
+ args: [
26
+ bin.path,
27
+ 'test',
28
+ ...args,
29
+ ...selectors(targets, target => escape(target.path)),
30
+ ],
31
+ env: { PW_TEST_REPORTER: REPORTER, TESTPACE_REPORT: report },
32
+ }
33
+ },
34
+ }
@@ -0,0 +1,157 @@
1
+ // testpace run 各框架共用的跑法:解析选项和测试文件、起框架、跑完逐个文件核对、说明、退出码。
2
+ // 各框架只管怎么起(命令和参数,报告写到哪),各一个文件(node-test.ts、vitest.ts、playwright.ts);报告都写成 targets.ts 的 Ran。
3
+
4
+ import { appendFileSync, mkdtempSync, readFileSync, rmSync, statSync } from 'node:fs'
5
+ import { createRequire } from 'node:module'
6
+ import { tmpdir } from 'node:os'
7
+ import { dirname, join, relative, resolve } from 'node:path'
8
+ import { RUN_ENV } from '../../base/execute.ts'
9
+ import { FRAMEWORK_KILL_AFTER_MS, type GroupRun, spawnGroup } from '../../base/process-group.ts'
10
+ import { RESULTS_ENV } from '../results.ts'
11
+ import { parseTargets, readRanLines, type Target, tally } from './targets.ts'
12
+
13
+ export interface Framework {
14
+ /** 用法里的一行,比如 testpace run node-test [--cwd 目录] [--node-arg 参数]... 文件[:行号[:列号]]... */
15
+ usage: string
16
+ /** 原样交给框架的参数用的选项名,比如 --node-arg。 */
17
+ argOption: string
18
+ /** 能按行跑。不能的,按行给的整个文件跑,runner 说一句。 */
19
+ byLine: boolean
20
+ /**
21
+ * 怎么起:程序、参数,要另加的环境变量。cwd 是框架的工作目录,args 是用户交给框架的参数。
22
+ * report 是给框架写报告的临时文件:框架选好测试、跑完,把每个跑过或被跳过的测试写一行 Ran(见 targets.ts);
23
+ * 选测试之前就停了(参数不认、配置写错)不写。
24
+ * 起不了(比如项目里没装这个框架)时返回说明。
25
+ */
26
+ command(targets: Target[], args: string[], cwd: string, report: string): Command | string
27
+ }
28
+
29
+ export interface Command {
30
+ program: string
31
+ args: string[]
32
+ env?: Record<string, string>
33
+ }
34
+
35
+ /** 项目在 cwd 装的包里的命令(package.json 的 bin)的路径;找不到这个包时返回说明。框架用项目自己装的那份,testpace 不带框架。 */
36
+ export function projectBin(cwd: string, pkg: string, bin: string): { path: string } | string {
37
+ let manifest: string
38
+ try {
39
+ manifest = createRequire(join(cwd, 'package.json')).resolve(`${pkg}/package.json`)
40
+ } catch {
41
+ return `在 ${cwd} 找不到项目装的 ${pkg}:在装了它的目录下跑,或用 --cwd 指过去`
42
+ }
43
+ const bins = (JSON.parse(readFileSync(manifest, 'utf8')) as { bin: Record<string, string> }).bin
44
+ return { path: join(dirname(manifest), bins[bin]!) }
45
+ }
46
+
47
+ /** 退出码:0 通过;1 测试没过或有文件没跑到测试(框架自己的非零退出码照传);2 用法问题、文件或目录不存在、起不了框架;130 被中断。 */
48
+ export async function runFramework(framework: Framework, argv: string[]): Promise<number> {
49
+ const options = parseOptions(argv, framework.argOption)
50
+ if (typeof options === 'string') {
51
+ console.error(`${options}\n用法:${framework.usage}`)
52
+ return 2
53
+ }
54
+ const cwd = resolve(options.cwd)
55
+ if (!statSync(cwd, { throwIfNoEntry: false })?.isDirectory()) {
56
+ console.error(`[testpace] --cwd 给的目录不存在:${options.cwd}`)
57
+ return 2
58
+ }
59
+ const { targets, missing } = parseTargets(options.files, process.cwd())
60
+ if (missing.length > 0) {
61
+ console.error(`[testpace] 找不到测试文件:${missing.join('、')}`)
62
+ return 2
63
+ }
64
+
65
+ const located = targets.flatMap(target => target.located.map(({ text }) => text))
66
+ if (!framework.byLine && located.length > 0) console.log(`[testpace] 这个框架不能按行跑,这些整个文件跑:${located.join('、')}`)
67
+
68
+ const dir = mkdtempSync(join(tmpdir(), 'testpace-run-'))
69
+ try {
70
+ const report = join(dir, 'report')
71
+ const command = framework.command(targets, options.args, cwd, report)
72
+ if (typeof command === 'string') {
73
+ console.error(`[testpace] ${command}`)
74
+ return 2
75
+ }
76
+ const run = await spawnFramework(command, cwd)
77
+ if (run.interrupted) return 130
78
+ if (run.error) {
79
+ console.error(`[testpace] 没能起 ${command.program}:${run.error.code === 'ENOENT' ? 'PATH 上找不到它' : run.error.message}`)
80
+ return 2
81
+ }
82
+ // 被信号终止(不是 testpace 转过去的)算没过
83
+ const code = run.exitCode ?? 1
84
+
85
+ const ran = readRanLines(report)
86
+ // 没写报告:框架在选测试之前就停了,原因它已经说过,没有可核对的。退出码是 0 却没报告,什么都没验证到,不算通过
87
+ if (ran === undefined) {
88
+ if (code !== 0) return code
89
+ console.error('[testpace] 框架退出了,却没写出跑了哪些测试,什么都没验证到')
90
+ return 1
91
+ }
92
+ // 由 testpace 的 files、focused、full 起的:每个测试的结果交给它,认时好时坏和几轮没过(见 results.ts)
93
+ const results = process.env[RESULTS_ENV]
94
+ if (results) appendFileSync(results, ran.map(test => `${JSON.stringify(test)}\n`).join(''))
95
+ // 框架没过也照样核对:没跑到测试的文件和行一并说出来
96
+ const { notRun, allSkipped, others, failed } = tally(targets, ran)
97
+ if (others.length > 0) console.log(`[testpace] 框架还跑了这些没给的文件(名字片段匹配到的,或给的测试依赖的准备项目):${others.map(file => relative(process.cwd(), file)).join('、')}`)
98
+ if (allSkipped.length > 0) console.log(`[testpace] 这些给的文件或行上的测试这次全被跳过,什么都没验证:${allSkipped.join('、')}`)
99
+ if (notRun.length > 0) {
100
+ console.error(`[testpace] 这些给的文件或行上一个测试都没跑到(没加载起来、里面没有测试、给的行上没有测试,或被框架的配置排除了):${notRun.join('、')}`)
101
+ if (failed) console.error('[testpace] 这次已有测试没过,框架可能没跑到它们就停下了(比如 bail、max-failures、依赖的准备项目没过):先修没过的测试,再看这几个')
102
+ }
103
+ if (code !== 0) return code
104
+ return notRun.length > 0 ? 1 : 0
105
+ } finally {
106
+ rmSync(dir, { recursive: true, force: true })
107
+ }
108
+ }
109
+
110
+ interface Options {
111
+ cwd: string
112
+ args: string[]
113
+ files: string[]
114
+ }
115
+
116
+ /** 选项在前,文件在后;`--` 之后都当文件。出错时返回说明。 */
117
+ function parseOptions(argv: string[], argOption: string): Options | string {
118
+ const options: Options = { cwd: '.', args: [], files: [] }
119
+ for (let i = 0; i < argv.length; i += 1) {
120
+ const arg = argv[i]!
121
+ if (arg === '--') {
122
+ options.files.push(...argv.slice(i + 1))
123
+ break
124
+ }
125
+ const [name, inline] = arg.startsWith('--') && arg.includes('=') ? [arg.slice(0, arg.indexOf('=')), arg.slice(arg.indexOf('=') + 1)] : [arg, undefined]
126
+ if (name === '--cwd' || name === argOption) {
127
+ const value = inline ?? argv[++i]
128
+ if (value === undefined || value === '') return `${name} 后面要跟一个值`
129
+ if (name === '--cwd') options.cwd = value
130
+ else options.args.push(value)
131
+ } else if (arg.startsWith('-')) {
132
+ return `不认识的选项 "${arg}"`
133
+ } else {
134
+ options.files.push(arg)
135
+ }
136
+ }
137
+ if (options.files.length === 0) return '没有给测试文件'
138
+ return options
139
+ }
140
+
141
+ /**
142
+ * 起框架,输出直通,不接输入(testpace 跑测试不回答框架的提问,比如 Vitest 问要不要装缺的包;框架在自己的进程组里,
143
+ * 从终端读输入会被系统停住)。testpace 被中断时(不管收到的是哪个信号)给框架的整个进程组发 SIGINT:框架当成 Ctrl-C 停下,
144
+ * 它起的跑测试的子进程也一起收到,不靠框架转告(Vitest 收到信号只管自己退出,跑测试的子进程要等手上的测试跑完才走)。
145
+ */
146
+ function spawnFramework({ program, args, env }: Command, cwd: string): Promise<GroupRun> {
147
+ // 交结果的位置只给这一层:测试自己再起的 testpace run(比如测 testpace 的测试)不往里写。
148
+ // 整组给检查的运行 id 也不给测试:检查直接跑项目的测试命令时(比如 testcraft check 在副本里跑),测试不是那一段检查
149
+ const { [RESULTS_ENV]: _, [RUN_ENV]: _run, ...inherited } = process.env
150
+ return spawnGroup(program, args, {
151
+ cwd,
152
+ env: { ...inherited, ...env },
153
+ stdio: ['ignore', 'inherit', 'inherit'],
154
+ stopSignal: 'SIGINT',
155
+ killAfterMs: FRAMEWORK_KILL_AFTER_MS,
156
+ })
157
+ }
@@ -0,0 +1,130 @@
1
+ // testpace run 各框架共用:解析交给 files 的测试文件,跑完逐个文件核对有没有跑到测试。
2
+
3
+ import { existsSync, readFileSync, realpathSync, statSync } from 'node:fs'
4
+ import { resolve } from 'node:path'
5
+ import { LOCATED, testFile } from '../testlist.ts'
6
+
7
+ export interface Target {
8
+ /** 照给的写法,去掉了行号、列号。 */
9
+ file: string
10
+ /** 真实的绝对路径(解开符号链接),和框架报上来的路径对得上。 */
11
+ path: string
12
+ /** 带行号给的:原样的写法和行号(去掉了列号)。 */
13
+ located: Array<{ text: string; line: number }>
14
+ /** 有一次没带行号:整个文件跑。 */
15
+ whole: boolean
16
+ }
17
+
18
+ /**
19
+ * 解析 文件[:行号[:列号]]:路径按 base(仓库根目录)算,同一个文件只留一份;
20
+ * 不存在、不是文件的放进 missing(框架拿到找不到的路径时常常悄悄跳过,要先查)。
21
+ */
22
+ export function parseTargets(args: string[], base: string): { targets: Target[]; missing: string[] } {
23
+ const byPath = new Map<string, Target>()
24
+ const missing: string[] = []
25
+ for (const arg of args) {
26
+ const file = testFile(arg)
27
+ const absolute = resolve(base, file)
28
+ if (!isFile(absolute)) {
29
+ if (!missing.includes(file)) missing.push(file)
30
+ continue
31
+ }
32
+ const path = realpathSync(absolute)
33
+ let target = byPath.get(path)
34
+ if (!target) {
35
+ target = { file, path, located: [], whole: false }
36
+ byPath.set(path, target)
37
+ }
38
+ const location = LOCATED.exec(arg)?.[0]
39
+ if (location === undefined) {
40
+ target.whole = true
41
+ } else {
42
+ target.located.push({ text: arg, line: Number(location.split(':')[1]) })
43
+ }
44
+ }
45
+ return { targets: [...byPath.values()], missing }
46
+ }
47
+
48
+ /**
49
+ * 交给能按行跑的框架的参数:整个文件给路径,按行给 路径:行号;同一个文件也给了整个文件时只给路径,整个跑
50
+ * (框架见到行号就只跑那几行)。path 是这个框架认的路径写法。
51
+ */
52
+ export function selectors(targets: Target[], path: (target: Target) => string = target => target.path): string[] {
53
+ return targets.flatMap(target => (target.whole ? [path(target)] : target.located.map(({ line }) => `${path(target)}:${line}`)))
54
+ }
55
+
56
+ function isFile(path: string): boolean {
57
+ try {
58
+ return statSync(path).isFile()
59
+ } catch {
60
+ return false
61
+ }
62
+ }
63
+
64
+ /**
65
+ * 一个测试的记录,三个框架的计数报告器都写成这样:哪个文件(真实的绝对路径)、声明在哪一行、外层各级分组(describe)声明在哪几行、名字、结果。
66
+ * 只记跑过或被跳过的;没跑到的(框架提前停下、准备项目没过)不记。
67
+ * 没有行号的(Vitest 没打开 includeTaskLocation 时)只能按整个文件核对。
68
+ */
69
+ export interface Ran {
70
+ file: string
71
+ line?: number
72
+ ancestors: number[]
73
+ /** 测试名,带上外层各级分组的名字,用 > 连起来(Vitest、Playwright 的 project 有名字时前面还有 [project 名])。认同一个测试靠它和文件。 */
74
+ name: string
75
+ status: 'passed' | 'failed' | 'skipped'
76
+ /** 框架重试之后才过。 */
77
+ retried?: boolean
78
+ }
79
+
80
+ /** 读计数报告器写的报告:每行一条 Ran。没写报告(框架在选测试之前就停了)时返回 undefined。 */
81
+ export function readRanLines(report: string): Ran[] | undefined {
82
+ if (!existsSync(report)) return undefined
83
+ return readFileSync(report, 'utf8').split('\n').filter(Boolean).map(line => JSON.parse(line) as Ran)
84
+ }
85
+
86
+ export interface Tally {
87
+ /** 一个测试都没跑到的:整个文件给的写文件,按行给的写原样的 文件:行号。 */
88
+ notRun: string[]
89
+ /** 选中的测试全被跳过的,写法同上。 */
90
+ allSkipped: string[]
91
+ /** 框架还跑了的没给的文件(按名字片段匹配到的,或给的测试依赖的准备项目)。 */
92
+ others: string[]
93
+ /** 有测试没过:没跑到的可能是框架提前停下了。 */
94
+ failed: boolean
95
+ }
96
+
97
+ /**
98
+ * 逐个核对给的文件和 文件:行号:整个文件给的看这个文件的测试,按行给的看这一行选中的测试
99
+ * (测试自己声明在这一行,或它外层哪一级分组声明在这一行;三个框架按行跑都是这么选的)。
100
+ * 一个测试都没跑到的算失败;全被跳过的不算失败,但要说。同一个文件也给了整个文件时,按行给的不另核对。
101
+ */
102
+ export function tally(targets: Target[], ran: Ran[]): Tally {
103
+ const byFile = new Map<string, Ran[]>(targets.map(target => [target.path, []]))
104
+ const others = new Set<string>()
105
+ for (const test of ran) {
106
+ const tests = byFile.get(test.file)
107
+ if (tests) tests.push(test)
108
+ else others.add(test.file)
109
+ }
110
+ const notRun: string[] = []
111
+ const allSkipped: string[] = []
112
+ const check = (name: string, tests: Ran[]): void => {
113
+ if (tests.length === 0) notRun.push(name)
114
+ else if (tests.every(test => test.status === 'skipped')) allSkipped.push(name)
115
+ }
116
+ for (const target of targets) {
117
+ const tests = byFile.get(target.path)!
118
+ if (target.whole) {
119
+ check(target.file, tests)
120
+ continue
121
+ }
122
+ const seen = new Set<number>()
123
+ for (const { text, line } of target.located) {
124
+ if (seen.has(line)) continue // 同一行带列号、不带列号各给了一次
125
+ seen.add(line)
126
+ check(text, tests.filter(test => test.line === line || test.ancestors.includes(line)))
127
+ }
128
+ }
129
+ return { notRun, allSkipped, others: [...others], failed: ran.some(test => test.status === 'failed') }
130
+ }
@@ -0,0 +1,101 @@
1
+ // testpace run vitest 起 Vitest 用的启动脚本:用项目的 node、在 --cwd 下运行,经 Vitest 的 Node 接口照 `vitest run` 的做法跑,
2
+ // 跑完把每个跑过或被跳过的测试写进报告(见 targets.ts 的 Ran),testpace 按给的文件和行核对。
3
+ // 不用命令行,是因为命令行加报告器(--reporter)会把项目配置里的报告器整个换掉;这里不加报告器,项目的报告器、
4
+ // 屏幕输出都和直接跑 `vitest run` 一样,结果跑完从它的状态(vitest.state)里读。
5
+ //
6
+ // 用法:node vitest-launcher.mjs <报告文件> <vitest 的 bin> -- <交给 vitest run 的参数>...
7
+ // 照 Vitest 命令行(vitest run → start)做:解析参数,再交给它导出的 startVitest(命令行也是调它:准备环境、查要装的包、跑、
8
+ // 报启动错误、收尾),只在它返回之后、退出之前写报告。4.1 到 5.x 都一样;不同的地方注明。
9
+ // 选测试之前就停了(参数不认、不支持、配置写错)不写报告,testpace 见没有报告就不核对。
10
+ import { writeFileSync } from 'node:fs'
11
+ import { createRequire } from 'node:module'
12
+ import { join } from 'node:path'
13
+ import { pathToFileURL } from 'node:url'
14
+
15
+ const [report, bin, separator, ...args] = process.argv.slice(2)
16
+ if (separator !== '--') throw new Error('用法:node vitest-launcher.mjs <报告文件> <vitest 的 bin> -- <参数>...')
17
+
18
+ /** testpace 只跑一次就结束:这些会另起分支(看着改动重跑、开界面、合并报告、只列测试、清缓存)或不跑测试,不交给它。 */
19
+ const UNSUPPORTED = ['watch', 'ui', 'standalone', 'mergeReports', 'listTags', 'clearCache', 'help', 'version']
20
+
21
+ await main()
22
+
23
+ async function main() {
24
+ const node = await import(pathToFileURL(createRequire(join(process.cwd(), 'package.json')).resolve('vitest/node')).href)
25
+
26
+ // 配置文件里看 process.argv 的,看到的和直接跑 vitest run 一样
27
+ process.argv = [process.argv[0], bin, 'run', ...args]
28
+ const argv = ['vitest', 'run', ...args]
29
+
30
+ // 不认识的参数:命令行会报错退出,parseCLI 却照收不误。5.0 起 Vitest 导出 createCLI,照命令行那样查;4.1 没导出,查不了
31
+ if (node.createCLI) {
32
+ const cli = node.createCLI()
33
+ try {
34
+ cli.parse(['node', bin, ...argv.slice(1)], { run: false })
35
+ cli.matchedCommand?.checkUnknownOptions()
36
+ cli.matchedCommand?.checkOptionValue()
37
+ cli.matchedCommand?.checkRequiredArgs()
38
+ } catch (error) {
39
+ console.error(`[testpace] Vitest 不认这些参数:${error.message}`)
40
+ process.exitCode = 2
41
+ return
42
+ }
43
+ }
44
+
45
+ const { filter, options } = node.parseCLI(argv)
46
+ const unsupported = UNSUPPORTED.filter(name => options[name] !== undefined && options[name] !== false)
47
+ if (unsupported.length > 0) {
48
+ console.error(`[testpace] testpace run vitest 不支持这些参数:${unsupported.map(name => `--${name}`).join('、')}`)
49
+ process.exitCode = 2
50
+ return
51
+ }
52
+
53
+ // 命令行的 normalizeCliOptions(Vitest 没导出)。命令行还把过滤参数的路径统一成 / 分隔,testpace 只在类 Unix 系统上跑,本来就是
54
+ if (options.exclude) {
55
+ options.cliExclude = [options.exclude].flat()
56
+ delete options.exclude
57
+ }
58
+ if (filter.some(arg => arg.includes(':'))) options.includeTaskLocation ??= true
59
+ if (typeof options.typecheck?.only === 'boolean') options.typecheck.enabled ??= true
60
+
61
+ // 命令行的 start。startVitest 5.x 把第一个参数(mode)去掉了,但还认 4.1 的写法
62
+ try {
63
+ const vitest = await node.startVitest('test', filter, options)
64
+ // 它返回时已经收尾(close),跑的结果还在 vitest.state 里
65
+ writeFileSync(report, collect(vitest))
66
+ await vitest.exit()
67
+ } catch (error) {
68
+ console.error('\n Startup Error \n')
69
+ console.error(error)
70
+ console.error('\n\n')
71
+ process.exitCode ??= 1
72
+ process.exit()
73
+ }
74
+ }
75
+
76
+ /**
77
+ * 每个跑过或被跳过的测试一行。文件出错、又一个测试都没真跑的(没加载起来、给的行上没有测试),它报上来的测试只是没被选中,不算;
78
+ * 还是 pending 的(bail 之后没跑到)也不算。按行跑时没被选中的测试也报成跳过,靠行号由 testpace 筛掉。
79
+ */
80
+ function collect(vitest) {
81
+ const lines = []
82
+ for (const module of vitest.state.getTestModules()) {
83
+ const tests = [...module.children.allTests()].map(test => ({ test, state: test.result().state }))
84
+ const ran = tests.some(({ state }) => state === 'passed' || state === 'failed')
85
+ if (module.errors().length > 0 && !ran) continue
86
+ for (const { test, state } of tests) {
87
+ if (state === 'pending') continue
88
+ const ancestors = []
89
+ for (let parent = test.parent; parent?.type === 'suite'; parent = parent.parent) {
90
+ if (parent.location) ancestors.push(parent.location.line)
91
+ }
92
+ // 重试后才过的记 retried(diagnostic 只在跑过的测试上有)
93
+ const retried = state === 'passed' && test.diagnostic()?.flaky === true
94
+ // 同一个文件跑在几个 project 里时各算一个测试:名字前面带上 project 名(和 Playwright 一样)
95
+ const project = test.project.name
96
+ const name = `${project ? `[${project}] ` : ''}${test.fullName}`
97
+ lines.push(`${JSON.stringify({ file: module.moduleId, line: test.location?.line, ancestors, name, status: state, ...(retried ? { retried } : {}) })}\n`)
98
+ }
99
+ }
100
+ return lines.join('')
101
+ }
@@ -0,0 +1,22 @@
1
+ // testpace run vitest:用项目自己装的 Vitest(从 --cwd 找)跑给定的测试文件,守 testpace 对 files 的约定。
2
+ // 经插件的启动脚本(vitest-launcher.mjs)用 Vitest 的 Node 接口照 `vitest run` 跑:项目配置里的报告器、屏幕输出照旧,
3
+ // 跑完把每个测试的记录写进报告文件,逐个文件、逐行核对。按行给的交给 Vitest 按行跑(同一个文件也给了整个文件时整个跑,见 selectors)。
4
+ // Vitest 自己就把这几种算失败:文件里没有测试、给的行上没有测试。这里补上它放过的:给的文件被配置排除了
5
+ // (它悄悄不跑、照样通过),选中的测试全被跳过(说一句)。它把文件参数当名字片段,可能顺带跑别的文件,也说一句。
6
+
7
+ import { fileURLToPath } from 'node:url'
8
+ import { type Framework, projectBin } from './runner.ts'
9
+ import { selectors } from './targets.ts'
10
+
11
+ const LAUNCHER = fileURLToPath(new URL('./vitest-launcher.mjs', import.meta.url))
12
+
13
+ export const vitest: Framework = {
14
+ usage: 'testpace run vitest [--cwd 目录] [--vitest-arg 参数]... 文件[:行号[:列号]]...',
15
+ argOption: '--vitest-arg',
16
+ byLine: true,
17
+ command(targets, args, cwd, report) {
18
+ const bin = projectBin(cwd, 'vitest', 'vitest')
19
+ if (typeof bin === 'string') return bin
20
+ return { program: 'node', args: [LAUNCHER, report, bin.path, '--', ...args, ...selectors(targets)] }
21
+ },
22
+ }
@@ -0,0 +1,35 @@
1
+ // "跑测试"说给 agent 的话,经底座的钩子送出去(base/module.ts 的 brief、alerts、beforeCommand):
2
+ // 测试节奏、名单上的时好时坏测试、直接跑 full 命令时的提示。认出的时好时坏测试在测试结果里点名,收尾时不再拦。
3
+
4
+ import { basename } from 'node:path'
5
+ import { openRepo, type Repo } from '../base/repo.ts'
6
+ import { invocations, runsCommand } from '../base/shell.ts'
7
+ import { loadConfig } from './config.ts'
8
+ import { flakyBrief } from './results.ts'
9
+ import { flakyTests } from './testlist.ts'
10
+
11
+ /** 测试节奏。repo 是话里指这个仓库的写法。落地的规矩由底座接在后面说。 */
12
+ export function rhythm(repo: string): string[] {
13
+ return [
14
+ `${repo}用 testpace 管测试节奏(testpace.json):`,
15
+ '1. 每个任务在自己的分支上做,最好是自己的 worktree,不和别的会话挤在同一个目录里。',
16
+ '2. 每做完一轮、向用户汇报之前跑一次 testpace focused:fast 检查,加上这次改到的测试和最相关的几个(最多再跑 8 个文件)。一轮只跑一次;小改动(文字、样式、注释)不用跑。在分支上不跑整组,其余的留给合并时的整组兜底。',
17
+ '3. 开发中有测试没过,只看不追。先分清是哪一种:因为用户这次改了需求才不过的,不管它,不改测试,留到合并时一起改;和这次需求无关的(这次的改动弄坏了别的功能),当场改这次自己写的代码,用 testpace files <文件:行号> 只重跑那几个测试(位置看失败输出)。不为了通过重跑,不改测试凑通过。fast 没过的照常当场修。',
18
+ '4. 做完一个任务不要自己合回默认分支:告诉用户做完了,等用户说合。用户说合("合""可以合了""提 PR"),就照 gatelight:merge 技能合这个分支:合到一个临时的合并分支上跑一次 testpace full,通过了再落地。一个分支合一次,不用攒。整组要跑几分钟,能把命令放到后台的就放后台、接着做别的,不要干等;跑完用 testpace status 看结果。',
19
+ ]
20
+ }
21
+
22
+ /** 名单上有时好时坏的测试时提醒一句。 */
23
+ export function alerts(repo: Repo): string[] {
24
+ const flaky = flakyBrief(flakyTests(repo.store))
25
+ return flaky ? [flaky] : []
26
+ }
27
+
28
+ /** 直接跑配置里的 full 不会留下记录,合并门不认;它也只是整组的一部分。 */
29
+ export function fullCommandHint(command: string, cwd: string): string | undefined {
30
+ const repo = openRepo(cwd)
31
+ const full = repo && loadConfig(repo.root).config?.full
32
+ const calls = invocations(command, cwd)
33
+ if (!full || calls.some(call => basename(call.argv[0] ?? '') === 'testpace') || !runsCommand(command, full, cwd)) return undefined
34
+ return '这是 testpace.json 里 full 的检查,只是整组的一部分(整组还要跑 tests 规则找到的全部测试)。直接跑不会留下通过记录,合并门不认;整组改用 testpace full,结果会按当前代码记录。'
35
+ }
@@ -0,0 +1,91 @@
1
+ // 认一个测试,和按测试记的两份名单:时好时坏的测试、连着几轮没过的测试。
2
+ // 存在底座的记录目录里(.git/testpace/ 下的 flaky/、rounds/),读写都在这里,底座不认识这两份名单。
3
+
4
+ import { createHash } from 'node:crypto'
5
+ import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync } from 'node:fs'
6
+ import { join } from 'node:path'
7
+ import { type Store, worktreeKey, writeWhole } from '../base/store.ts'
8
+
9
+ /** files 的参数可以是 文件:行号,只跑那一行的测试;测试框架输出里常见的 文件:行号:列号 也算。 */
10
+ export const LOCATED = /:\d+(?::\d+)?$/
11
+
12
+ /** files 的一个参数对应的测试文件。 */
13
+ export function testFile(target: string): string {
14
+ return target.replace(LOCATED, '')
15
+ }
16
+
17
+ /** 一个测试:文件(相对仓库根目录)和带外层分组的名字认它;行号用来显示和重跑。 */
18
+ export interface TestId {
19
+ file: string
20
+ line?: number
21
+ name: string
22
+ }
23
+
24
+ /** 时好时坏名单上的一个测试。 */
25
+ export interface FlakyTest extends TestId {
26
+ /** 怎么认出来的:同一份代码上先没过后过了(或连跑时前面过了后面没过),还是框架重试后才过。 */
27
+ how: 'reran' | 'retried'
28
+ foundAt: number
29
+ tree: string
30
+ worktree: string
31
+ /** 没过那次的日志。重试后才过的就是通过那次的日志。 */
32
+ failLog: string
33
+ passLog: string
34
+ /** 认出它的 agent 会话。 */
35
+ session?: string
36
+ }
37
+
38
+ /** 一个测试在一个工作区里连着几轮没过:每轮的代码、日志、时间。 */
39
+ export interface Rounds extends TestId {
40
+ rounds: Array<{ tree: string; log: string; at: number }>
41
+ }
42
+
43
+ /** 名单、计数里认同一个测试用的键。 */
44
+ export function testKey(test: Pick<TestId, 'file' | 'name'>): string {
45
+ return `${test.file}\0${test.name}`
46
+ }
47
+
48
+ function flakyDir(store: Store): string {
49
+ return join(store.dir, 'flaky')
50
+ }
51
+
52
+ function flakyPath(store: Store, test: Pick<TestId, 'file' | 'name'>): string {
53
+ return join(flakyDir(store), `${createHash('sha1').update(testKey(test)).digest('hex').slice(0, 16)}.json`)
54
+ }
55
+
56
+ /** 时好时坏的名单,先认出的在前。一个测试一份文件:几个工作区同时跑完时各写各的,不会互相覆盖。 */
57
+ export function flakyTests(store: Store): FlakyTest[] {
58
+ if (!existsSync(flakyDir(store))) return []
59
+ const list: FlakyTest[] = []
60
+ for (const name of readdirSync(flakyDir(store)).filter(name => name.endsWith('.json'))) {
61
+ try {
62
+ list.push(JSON.parse(readFileSync(join(flakyDir(store), name), 'utf8')) as FlakyTest)
63
+ } catch {
64
+ // 写坏了的跳过
65
+ }
66
+ }
67
+ return list.sort((a, b) => a.foundAt - b.foundAt)
68
+ }
69
+
70
+ export function addFlaky(store: Store, test: FlakyTest): void {
71
+ mkdirSync(flakyDir(store), { recursive: true })
72
+ writeWhole(flakyPath(store, test), JSON.stringify(test))
73
+ }
74
+
75
+ export function removeFlaky(store: Store, test: Pick<TestId, 'file' | 'name'>): void {
76
+ rmSync(flakyPath(store, test), { force: true })
77
+ }
78
+
79
+ /** 这个工作区里连着没过的测试,按 testKey。同一个工作区一次只跑一轮,读写不会撞上。 */
80
+ export function readRounds(store: Store, worktree: string): Record<string, Rounds> {
81
+ try {
82
+ return JSON.parse(readFileSync(join(store.dir, 'rounds', `${worktreeKey(worktree)}.json`), 'utf8')) as Record<string, Rounds>
83
+ } catch {
84
+ return {}
85
+ }
86
+ }
87
+
88
+ export function saveRounds(store: Store, worktree: string, rounds: Record<string, Rounds>): void {
89
+ mkdirSync(join(store.dir, 'rounds'), { recursive: true })
90
+ writeWhole(join(store.dir, 'rounds', `${worktreeKey(worktree)}.json`), JSON.stringify(rounds))
91
+ }
@@ -0,0 +1,22 @@
1
+ // 整组跑的测试:按 testpace.json 的 tests 规则,在工作区里找出全部测试文件。
2
+ // 由 testpace 自己找、交给 files 跑,新加的测试不用谁记得加进整组,就会被跑到。
3
+
4
+ import { existsSync } from 'node:fs'
5
+ import { join } from 'node:path'
6
+ import { git } from '../base/git.ts'
7
+ import { matcher } from '../base/glob.ts'
8
+
9
+ /** 规则选中这个路径:匹配某条规则,且不匹配以 ! 开头的排除规则。 */
10
+ export function testMatcher(patterns: string[]): (path: string) => boolean {
11
+ const include = matcher(patterns.filter(pattern => !pattern.startsWith('!')))
12
+ const exclude = matcher(patterns.filter(pattern => pattern.startsWith('!')).map(pattern => pattern.slice(1)))
13
+ return path => include(path) && !exclude(path)
14
+ }
15
+
16
+ /** 工作区里规则选中的测试文件:已跟踪的和未跟踪、没被 .gitignore 忽略的,相对仓库根目录,按路径排序。 */
17
+ export function findTests(root: string, patterns: string[]): string[] {
18
+ const selected = testMatcher(patterns)
19
+ const paths = git(root, ['ls-files', '-z', '--cached', '--others', '--exclude-standard']).split('\0')
20
+ // 已删除但还在索引里的文件不算
21
+ return [...new Set(paths)].filter(path => path !== '' && selected(path) && existsSync(join(root, path))).sort()
22
+ }