work-rhythm 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.
Files changed (102) hide show
  1. package/.agents/notes/AGENTS.md +84 -0
  2. package/.agents/notes/decisions/2026-11-28-long-term-goals-ride-existing-tasks.md +36 -0
  3. package/.agents/notes/decisions/2026-11-29-building-a-workdir-means-cloning-the-template.md +25 -0
  4. package/.agents/notes/decisions/2026-11-29-find-workdir-cwd-first-then-registered.md +32 -0
  5. package/.agents/notes/decisions/2026-11-30-bot-inbound-uses-websocket-and-a-responder-command.md +48 -0
  6. package/.agents/notes/decisions/2026-11-30-cost-entry-writes-into-empty-cells-directly.md +25 -0
  7. package/.agents/notes/decisions/2026-11-30-dependencies-are-allowed-user-installs-missing-ones.md +30 -0
  8. package/.agents/notes/decisions/2026-11-30-markdown-is-the-record-html-is-only-a-view.md +25 -0
  9. package/.agents/notes/decisions/2026-11-30-message-channels-are-preset-in-the-skill.md +34 -0
  10. package/.agents/notes/decisions/2026-11-30-no-workdir-offers-to-create-one-by-default.md +34 -0
  11. package/.agents/notes/decisions/2026-11-30-paging-requires-coverage-only-when-enabled.md +27 -0
  12. package/.agents/notes/decisions/2026-11-30-pending-decisions-asked-in-a-chat-and-logged-briefly.md +39 -0
  13. package/.agents/notes/decisions/2026-11-30-push-via-personal-robot-webhook.md +33 -0
  14. package/.agents/notes/decisions/2026-11-30-scheduling-moves-to-a-local-service.md +37 -0
  15. package/.agents/notes/decisions/2026-11-30-skill-ships-with-its-template-in-a-zip.md +30 -0
  16. package/.agents/notes/decisions/2026-11-30-skill-stays-generic-user-provides-cli-or-mcp.md +25 -0
  17. package/.agents/notes/decisions/2026-12-01-everything-is-js-behind-work-rhythm.md +45 -0
  18. package/.agents/notes/pitfalls/2026-11-29-do-not-hand-roll-git-log.md +22 -0
  19. package/.agents/notes/pitfalls/2026-11-29-empty-or-partial-collector-output.md +23 -0
  20. package/.agents/notes/pitfalls/2026-11-29-skill-description-is-truncated.md +21 -0
  21. package/.agents/notes/pitfalls/2026-11-29-tool-shell-is-zsh.md +24 -0
  22. package/.agents/notes/pitfalls/2026-11-30-compare-timestamps-as-digits.md +22 -0
  23. package/.agents/notes/pitfalls/2026-11-30-message-search-lags-after-sending.md +18 -0
  24. package/.agents/notes/pitfalls/2026-11-30-rewriting-a-file-with-empty-output-after-tool-failure.md +20 -0
  25. package/.agents/notes/pitfalls/2026-11-30-secret-scan-false-positive-on-hyphenated-file-names.md +18 -0
  26. package/.agents/notes/pitfalls/2026-11-30-selftest-must-not-inherit-local-config.md +29 -0
  27. package/.agents/notes/pitfalls/2026-11-30-yzj-cli-list-limit-is-20.md +25 -0
  28. package/.agents/notes/pitfalls/2026-11-30-zsh-no-word-splitting-special-names.md +23 -0
  29. package/.agents/notes/pitfalls/2026-12-01-node-test-hooks-cannot-register-after-callbacks.md +22 -0
  30. package/.agents/notes/pitfalls/2026-12-01-spawning-node-test-inside-a-test-runs-nothing.md +18 -0
  31. package/.agents/skills/work-rhythm/SKILL.md +277 -0
  32. package/00-config/profile.example.yaml +54 -0
  33. package/00-config/sources.yaml +115 -0
  34. package/AGENTS.md +31 -0
  35. package/README.md +105 -2
  36. package/collectors/approval-yzj.js +114 -0
  37. package/collectors/calendar-yzj.js +48 -0
  38. package/collectors/file-activity.js +131 -0
  39. package/collectors/git-activity.js +105 -0
  40. package/collectors/im-yzj.js +151 -0
  41. package/collectors/linky-issues.js +88 -0
  42. package/collectors/local.env.example +52 -0
  43. package/docs/channels.md +95 -0
  44. package/docs/formats.md +285 -0
  45. package/docs/im-adapters.md +79 -0
  46. package/docs/scheduled-tasks.md +115 -0
  47. package/docs/sources.md +104 -0
  48. package/package.json +37 -4
  49. package/scripts/cli.js +98 -0
  50. package/scripts/cmd/check.js +297 -0
  51. package/scripts/cmd/collect.js +25 -0
  52. package/scripts/cmd/decisions.js +236 -0
  53. package/scripts/cmd/goals.js +145 -0
  54. package/scripts/cmd/init.js +53 -0
  55. package/scripts/cmd/listen.js +91 -0
  56. package/scripts/cmd/notify.js +42 -0
  57. package/scripts/cmd/pack.js +105 -0
  58. package/scripts/cmd/rhythm.js +102 -0
  59. package/scripts/cmd/selftest.js +23 -0
  60. package/scripts/cmd/sync.js +73 -0
  61. package/scripts/cmd/today.js +29 -0
  62. package/scripts/cmd/workdir.js +81 -0
  63. package/scripts/lib/agent.js +270 -0
  64. package/scripts/lib/bot.js +376 -0
  65. package/scripts/lib/collector.js +74 -0
  66. package/scripts/lib/config.js +250 -0
  67. package/scripts/lib/launchd.js +82 -0
  68. package/scripts/lib/log.js +22 -0
  69. package/scripts/lib/markdown.js +208 -0
  70. package/scripts/lib/notify.js +43 -0
  71. package/scripts/lib/rhythm.js +508 -0
  72. package/scripts/lib/robot.js +64 -0
  73. package/scripts/lib/run.js +151 -0
  74. package/scripts/lib/template.js +144 -0
  75. package/scripts/lib/workdir.js +89 -0
  76. package/scripts/test/check.test.js +217 -0
  77. package/scripts/test/collectors.test.js +574 -0
  78. package/scripts/test/config.test.js +119 -0
  79. package/scripts/test/decisions.test.js +129 -0
  80. package/scripts/test/fake-webhook.js +31 -0
  81. package/scripts/test/goals.test.js +109 -0
  82. package/scripts/test/harness.js +279 -0
  83. package/scripts/test/init.test.js +125 -0
  84. package/scripts/test/listen.test.js +362 -0
  85. package/scripts/test/notify.test.js +176 -0
  86. package/scripts/test/pack.test.js +141 -0
  87. package/scripts/test/rhythm.test.js +324 -0
  88. package/scripts/test/stubs.js +140 -0
  89. package/scripts/test/today.test.js +73 -0
  90. package/scripts/test/workdir.test.js +152 -0
  91. package/scripts/work-rhythm.js +18 -0
  92. package/tasks/alerts-morning.md +24 -0
  93. package/tasks/carryover.md +16 -0
  94. package/tasks/daily-plan.md +29 -0
  95. package/tasks/evening-review.md +27 -0
  96. package/tasks/linky-cost-check.md +13 -0
  97. package/tasks/linky-cost-fill.md +20 -0
  98. package/tasks/noon-checkin.md +15 -0
  99. package/tasks/weekly-rhythm.md +18 -0
  100. package/templates/03-carryover/CARRYOVER.md +14 -0
  101. package/templates/03-carryover/DECISIONS.md +10 -0
  102. package/templates/04-patterns/WORK_RHYTHM.md +16 -0
@@ -0,0 +1,574 @@
1
+ 'use strict';
2
+ const { test } = require('node:test');
3
+ const assert = require('node:assert/strict');
4
+ const fs = require('node:fs');
5
+ const path = require('node:path');
6
+ const { makeSandbox, read, exists, write, T, BIN, git, GIT_ENV, gitRepo } = require('./harness');
7
+
8
+ const TODAY = () => {
9
+ const d = new Date();
10
+ const pad = (n) => String(n).padStart(2, '0');
11
+ return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
12
+ };
13
+ // 一条只有 node 的 PATH:用来测「没装 git」这种缺依赖的情况
14
+ function onlyNode(box) {
15
+ const dir = path.join(box.root, 'only-node');
16
+ fs.mkdirSync(dir, { recursive: true });
17
+ fs.symlinkSync(process.execPath, path.join(dir, 'node'));
18
+ return dir;
19
+ }
20
+
21
+ // ---------------------------------------------------------------- git-activity
22
+ test('git-activity:导出今天的提交,表头和时间、仓库、哈希、说明四列', (t) => {
23
+ const box = makeSandbox(t);
24
+ box.workdir();
25
+ const ws = path.join(box.root, 'ws');
26
+ gitRepo(path.join(ws, 'demo'));
27
+ const out = path.join(box.root, 'out', 'git');
28
+ const r = box.run(['collect', 'git-activity', out, TODAY()], { env: { GIT_ACTIVITY_WORKSPACES: ws, GIT_ACTIVITY_AUTHORS: 'selftest@example.com' } });
29
+ assert.equal(r.code, 0, r.stderr);
30
+ const text = read(path.join(out, 'commits.tsv'));
31
+ assert.equal(text.startsWith('time\trepo\thash\tsubject\n'), true);
32
+ assert.equal(text.includes('导出接口联调修复'), true);
33
+ const row = text.trimEnd().split('\n')[1].split('\t');
34
+ assert.equal(row[1], 'demo');
35
+ assert.equal(row[3], '导出接口联调修复');
36
+ assert.match(r.stderr, new RegExp(`自 ${TODAY()} 起 1 条记录,涉及 1 个仓库`));
37
+ });
38
+
39
+ test('git-activity:.git 是文件的子模块也算进来;别的仓库的 worktree 不算', (t) => {
40
+ const box = makeSandbox(t);
41
+ box.workdir();
42
+ const ws = path.join(box.root, 'ws');
43
+ const outer = path.join(ws, 'outer');
44
+ gitRepo(path.join(ws, 'demo'));
45
+ const lib = gitRepo(path.join(box.root, 'gitdirs', 'lib'), { identity: false });
46
+ git(lib, ['-c', 'user.name=Selftest', '-c', 'user.email=selftest@example.com', '-c', 'commit.gpgsign=false', 'commit', '-q', '--allow-empty', '-m', '子模块里的提交', `--date=${new Date().toISOString()}`], GIT_ENV);
47
+ fs.mkdirSync(path.join(outer, 'lib'), { recursive: true });
48
+ fs.writeFileSync(path.join(outer, 'lib', '.git'), 'gitdir: ../../../gitdirs/lib/.git\n');
49
+ git(path.join(ws, 'demo'), ['worktree', 'add', '-q', path.join(outer, 'wt-demo'), '-b', 'selftest-wt'], GIT_ENV);
50
+ assert.equal(fs.existsSync(path.join(outer, 'wt-demo', '.git')), true);
51
+ const out = path.join(box.root, 'out', 'git-sub');
52
+ const r = box.run(['collect', 'git-activity', out, TODAY()], { env: { GIT_ACTIVITY_WORKSPACES: `${ws}|${outer}`, GIT_ACTIVITY_AUTHORS: 'selftest@example.com' } });
53
+ assert.equal(r.code, 0, r.stderr);
54
+ const text = read(path.join(out, 'commits.tsv'));
55
+ const lines = text.trimEnd().split('\n').slice(1);
56
+ assert.equal(lines.length, 2);
57
+ assert.equal(text.includes('子模块里的提交'), true);
58
+ assert.equal(text.includes('导出接口联调修复'), true);
59
+ assert.deepEqual(lines.map((l) => l.split('\t')[1]).sort(), ['demo', 'lib']);
60
+ });
61
+
62
+ test('git-activity:只扫 worktree 所在的那一层时,主仓库的提交不会再数一遍', (t) => {
63
+ const box = makeSandbox(t);
64
+ box.workdir();
65
+ const ws = path.join(box.root, 'ws');
66
+ const outer = path.join(ws, 'outer');
67
+ gitRepo(path.join(ws, 'demo'));
68
+ const lib = gitRepo(path.join(box.root, 'gitdirs', 'lib'), { identity: false });
69
+ git(lib, ['-c', 'user.name=Selftest', '-c', 'user.email=selftest@example.com', '-c', 'commit.gpgsign=false', 'commit', '-q', '--allow-empty', '-m', '子模块里的提交', `--date=${new Date().toISOString()}`], GIT_ENV);
70
+ fs.mkdirSync(path.join(outer, 'lib'), { recursive: true });
71
+ fs.writeFileSync(path.join(outer, 'lib', '.git'), 'gitdir: ../../../gitdirs/lib/.git\n');
72
+ git(path.join(ws, 'demo'), ['worktree', 'add', '-q', path.join(outer, 'wt-demo'), '-b', 'selftest-wt'], GIT_ENV);
73
+ const out = path.join(box.root, 'out', 'git-wt');
74
+ const r = box.run(['collect', 'git-activity', out, TODAY()], { env: { GIT_ACTIVITY_WORKSPACES: outer, GIT_ACTIVITY_AUTHORS: 'selftest@example.com' } });
75
+ assert.equal(r.code, 0, r.stderr);
76
+ const text = read(path.join(out, 'commits.tsv'));
77
+ assert.equal(text.trimEnd().split('\n').length, 2);
78
+ assert.equal(text.includes('子模块里的提交'), true);
79
+ assert.equal(text.includes('导出接口联调修复'), false);
80
+ });
81
+
82
+ test('git-activity:作者时间在起始日之前、提交时间在之后的旧提交,按仓库压成一行汇总', (t) => {
83
+ const box = makeSandbox(t);
84
+ box.workdir();
85
+ const ws = path.join(box.root, 'ws');
86
+ const repo = gitRepo(path.join(ws, 'demo'), { identity: false });
87
+ git(repo, ['-c', 'user.name=Selftest', '-c', 'user.email=selftest@example.com', '-c', 'commit.gpgsign=false', 'commit', '-q', '--allow-empty', '-m', '新鲜的提交', `--date=${new Date().toISOString()}`], GIT_ENV);
88
+ // 被 rebase 的旧提交:作者时间在 20 天前,提交时间是现在
89
+ const old = new Date(Date.now() - 20 * 86400000).toISOString();
90
+ git(repo, ['-c', 'user.name=Selftest', '-c', 'user.email=selftest@example.com', '-c', 'commit.gpgsign=false', 'commit', '-q', '--allow-empty', '-m', '很早以前写的'], { ...GIT_ENV, GIT_AUTHOR_DATE: old, GIT_COMMITTER_DATE: new Date().toISOString() });
91
+ const out = path.join(box.root, 'out', 'git-rebase');
92
+ const r = box.run(['collect', 'git-activity', out, TODAY()], { env: { GIT_ACTIVITY_WORKSPACES: ws, GIT_ACTIVITY_AUTHORS: 'selftest@example.com' } });
93
+ assert.equal(r.code, 0, r.stderr);
94
+ const text = read(path.join(out, 'commits.tsv'));
95
+ assert.equal(text.includes('新鲜的提交'), true);
96
+ assert.equal(text.includes('很早以前写的'), false);
97
+ assert.equal(text.includes('\tdemo\t-\t(rebase / cherry-pick:重新提交了 1 个更早的提交)'), true);
98
+ });
99
+
100
+ test('git-activity:认不出作者身份时退出码为 3;没装 git 时退出码为 4', (t) => {
101
+ const box = makeSandbox(t);
102
+ box.workdir();
103
+ const ws = path.join(box.root, 'ws');
104
+ gitRepo(path.join(ws, 'demo'));
105
+ const out = path.join(box.root, 'out', 'git-x');
106
+ const noAuthor = box.run(['collect', 'git-activity', out, TODAY()], { env: { GIT_ACTIVITY_WORKSPACES: ws, GIT_ACTIVITY_AUTHORS: '', GIT_CONFIG_GLOBAL: '/dev/null', GIT_CONFIG_SYSTEM: '/dev/null' } });
107
+ assert.equal(noAuthor.code, 3);
108
+ assert.match(noAuthor.stderr, /无法确定作者身份/);
109
+ const noGit = box.run(['collect', 'git-activity', out, TODAY()], { env: { PATH: onlyNode(box) } });
110
+ assert.equal(noGit.code, 4);
111
+ assert.match(noGit.stderr, /未安装 git/);
112
+ });
113
+
114
+ // ---------------------------------------------------------------- file-activity
115
+ test('file-activity:按项目汇总今天改动的文件', (t) => {
116
+ const box = makeSandbox(t);
117
+ box.workdir();
118
+ const ws = path.join(box.root, 'ws');
119
+ fs.mkdirSync(path.join(ws, 'demo', 'src'), { recursive: true });
120
+ fs.writeFileSync(path.join(ws, 'demo', 'src', 'a.js'), 'console.log');
121
+ const out = path.join(box.root, 'out', 'files');
122
+ const r = box.run(['collect', 'file-activity', out, TODAY()], { env: { FILE_ACTIVITY_WORKSPACES: ws } });
123
+ assert.equal(r.code, 0, r.stderr);
124
+ const text = read(path.join(out, 'summary.tsv'));
125
+ assert.equal(text.startsWith('project\tfiles\ttop_extensions\tnote\n'), true);
126
+ assert.equal(text.includes('demo'), true);
127
+ assert.match(text, /^ws\/demo\t1\t\.js 1\t$/m); // 项目 = 工作区的目录名 + 一级目录
128
+ });
129
+
130
+ test('file-activity:跳过版本库和依赖目录、忽略日志类文件;文件数异常多的项目带备注;工作区本身是仓库时整个算一个项目', (t) => {
131
+ const box = makeSandbox(t);
132
+ box.workdir();
133
+ const ws = path.join(box.root, 'ws');
134
+ // demo 是个普通项目:有 node_modules、.git、一个 .log 和一个真改的文件
135
+ fs.mkdirSync(path.join(ws, 'demo', 'node_modules', 'x'), { recursive: true });
136
+ fs.mkdirSync(path.join(ws, 'demo', '.git'), { recursive: true });
137
+ fs.writeFileSync(path.join(ws, 'demo', 'node_modules', 'x', 'y.js'), '');
138
+ fs.writeFileSync(path.join(ws, 'demo', '.git', 'HEAD'), '');
139
+ fs.writeFileSync(path.join(ws, 'demo', 'run.log'), '');
140
+ fs.writeFileSync(path.join(ws, 'demo', 'a.ts'), '');
141
+ fs.writeFileSync(path.join(ws, 'demo', 'b.ts'), '');
142
+ fs.writeFileSync(path.join(ws, 'demo', 'c.py'), '');
143
+ // big 是个改了 151 个文件的项目
144
+ fs.mkdirSync(path.join(ws, 'big'), { recursive: true });
145
+ for (let i = 0; i < 151; i += 1) fs.writeFileSync(path.join(ws, 'big', `f${i}.js`), '');
146
+ // whole 自己就是仓库:它的下一层不拆成项目
147
+ fs.mkdirSync(path.join(ws, 'whole', '.git'), { recursive: true });
148
+ fs.mkdirSync(path.join(ws, 'whole', 'src'), { recursive: true });
149
+ fs.writeFileSync(path.join(ws, 'whole', '.git', 'HEAD'), '');
150
+ fs.writeFileSync(path.join(ws, 'whole', 'src', 'm.js'), '');
151
+ const out = path.join(box.root, 'out', 'files2');
152
+ const r = box.run(['collect', 'file-activity', out, TODAY()], { env: { FILE_ACTIVITY_WORKSPACES: ws } });
153
+ assert.equal(r.code, 0, r.stderr);
154
+ const lines = read(path.join(out, 'summary.tsv')).split('\n').slice(1, -1);
155
+ assert.deepEqual(lines, [
156
+ `ws/big\t151\t.js 151\t文件数异常多,多半是运行或构建产物`,
157
+ `ws/demo\t3\t.ts 2 .py 1\t`,
158
+ `ws/whole\t1\t.js 1\t`,
159
+ ]);
160
+ const files = read(path.join(out, 'files.txt')).split('\n').filter(Boolean);
161
+ assert.equal(files.some((f) => f.includes('node_modules') || f.includes('/.git/') || f.endsWith('.log')), false);
162
+ assert.equal(files.length, 155);
163
+ });
164
+
165
+ // ---------------------------------------------------------------- calendar-yzj
166
+ test('calendar-yzj:拉取起始日起若干天的日程,结束时打印日期范围', (t) => {
167
+ const box = makeSandbox(t);
168
+ box.workdir();
169
+ const env = box.stubs();
170
+ const out = path.join(box.root, 'out', 'calendar');
171
+ const r = box.run(['collect', 'calendar-yzj', out, '2026-11-27', '4'], { env });
172
+ assert.equal(r.code, 0, r.stderr);
173
+ assert.equal(exists(path.join(out, 'events-2026-11-27.json')), true);
174
+ assert.equal(read(path.join(out, 'events-2026-11-27.json')), '{"success":true,"data":{"list":[{"title":"需求评审"}]}}\n');
175
+ assert.equal(r.stderr, 'calendar-yzj: 已拉取 2026-11-27 至 2026-11-30 的日程\n');
176
+ const one = box.run(['collect', 'calendar-yzj', out, '2026-11-27'], { env });
177
+ assert.match(one.stderr, /已拉取 2026-11-27 至 2026-11-27 的日程/);
178
+ });
179
+
180
+ test('calendar-yzj:拉取失败时退出码为 5,不留上一次的旧文件;天数不对退出码为 2;没装 yzj-cli 退出码为 4', (t) => {
181
+ const box = makeSandbox(t);
182
+ box.workdir();
183
+ const env = box.stubs();
184
+ const out = path.join(box.root, 'out', 'calendar-fail');
185
+ fs.mkdirSync(out, { recursive: true });
186
+ write(path.join(out, 'events-2026-11-27.json'), '{"stale":true}');
187
+ const r = box.run(['collect', 'calendar-yzj', out, '2026-11-27'], { env: { ...env, STUB_CALENDAR: 'fail' } });
188
+ assert.equal(r.code, 5);
189
+ assert.match(r.stderr, /拉取失败(未登录?试试 yzj-cli auth login): errorCode 10000400/);
190
+ assert.equal(exists(path.join(out, 'events-2026-11-27.json')), false);
191
+ assert.equal(box.run(['collect', 'calendar-yzj', out, '2026-11-27', 'x'], { env }).code, 2);
192
+ assert.equal(box.run(['collect', 'calendar-yzj', out, '2026-11-27', '0'], { env }).code, 2);
193
+ assert.equal(box.run(['collect', 'calendar-yzj', out, '2026-13-40'], { env }).code, 2);
194
+ assert.equal(box.run(['collect', 'calendar-yzj', out, '2026-11-27'], { env: { ...env, PATH: onlyNode(box) } }).code, 4);
195
+ });
196
+
197
+ // ---------------------------------------------------------------- im-yzj
198
+ const imOut = (box, source, date = TODAY()) => path.join(box.work, '01-inbox', source, date);
199
+
200
+ test('im-yzj:没配会话 ID 时退出码为 3,什么都不拉', (t) => {
201
+ const box = makeSandbox(t);
202
+ box.workdir();
203
+ const env = box.stubs();
204
+ const r = box.run(['collect', 'im-yzj', imOut(box, 'im-yzj'), TODAY()], { env: { ...env, YZJ_GROUP_IDS: '' } });
205
+ assert.equal(r.code, 3);
206
+ assert.equal(exists(env.STUB_CALLS), false);
207
+ });
208
+
209
+ test('im-yzj:拉取订阅的会话,调用之间至少隔 2 秒', async (t) => {
210
+ const box = makeSandbox(t);
211
+ box.workdir();
212
+ const env = box.stubs();
213
+ const started = Date.now();
214
+ const r = await new Promise((resolve) => {
215
+ // 用异步起进程,量实际用时
216
+ const { spawn } = require('node:child_process');
217
+ const child = spawn(process.execPath, [BIN, 'collect', 'im-yzj', imOut(box, 'im-yzj'), TODAY()], { cwd: box.work, env: { ...process.env, ...env, YZJ_GROUP_IDS: 'g1|g2' } });
218
+ let err = '';
219
+ child.stderr.on('data', (d) => { err += d; });
220
+ child.on('close', (code) => resolve({ code, stderr: err }));
221
+ });
222
+ const elapsed = (Date.now() - started) / 1000;
223
+ const calls = read(env.STUB_CALLS).split('\n').filter(Boolean).length;
224
+ assert.equal(r.code, 0, r.stderr);
225
+ assert.equal(exists(path.join(imOut(box, 'im-yzj'), 'messages-g1.json')), true);
226
+ assert.equal(exists(path.join(imOut(box, 'im-yzj'), 'messages-g2.json')), true);
227
+ assert.equal(calls, 3);
228
+ assert.equal(elapsed >= 2, true, `用时 ${elapsed.toFixed(1)} 秒`);
229
+ });
230
+
231
+ test('im-yzj:某个会话失败时退出码为 5,不留半份文件,其他会话照常', (t) => {
232
+ const box = makeSandbox(t);
233
+ box.workdir();
234
+ const env = box.stubs();
235
+ const r = box.run(['collect', 'im-yzj', imOut(box, 'im-yzj'), TODAY()], { env: { ...env, YZJ_CALL_INTERVAL: '0', YZJ_GROUP_IDS: 'g1|gfail' } });
236
+ assert.equal(r.code, 5);
237
+ assert.equal(exists(path.join(imOut(box, 'im-yzj'), 'messages-gfail.json')), false);
238
+ assert.equal(exists(path.join(imOut(box, 'im-yzj'), 'messages-g1.json')), true);
239
+ assert.match(r.stderr, /会话 gfail 拉取失败: errorCode 10000400/);
240
+ // 失败后下一个会话之前多等一会儿
241
+ const r2 = box.run(['collect', 'im-yzj', imOut(box, 'im-yzj'), TODAY()], { env: { ...env, YZJ_CALL_INTERVAL: '0', YZJ_GROUP_IDS: 'gfail|g1' } });
242
+ assert.equal(r2.code, 5);
243
+ });
244
+
245
+ test('im-yzj:作为 ops-yzj 源时只读它自己的会话列表', (t) => {
246
+ const box = makeSandbox(t);
247
+ box.workdir();
248
+ const env = box.stubs();
249
+ const r = box.run(['collect', 'im-yzj', imOut(box, 'ops-yzj'), TODAY()], { env: { ...env, YZJ_CALL_INTERVAL: '0', YZJ_GROUP_IDS: 'g1', YZJ_GROUP_IDS_OPS_YZJ: 'g9' } });
250
+ assert.equal(r.code, 0);
251
+ assert.equal(exists(path.join(imOut(box, 'ops-yzj'), 'messages-g9.json')), true);
252
+ assert.equal(exists(path.join(imOut(box, 'ops-yzj'), 'messages-g1.json')), false);
253
+ // 没配它的变量时不回退到 im-yzj 的会话
254
+ const r2 = box.run(['collect', 'im-yzj', imOut(box, 'ops-yzj'), TODAY()], { env: { ...env, YZJ_GROUP_IDS: 'g1' } });
255
+ assert.equal(r2.code, 3);
256
+ });
257
+
258
+ test('im-yzj:没装 yzj-cli 时退出码为 4;whoami 失败时退出码为 5', (t) => {
259
+ const box = makeSandbox(t);
260
+ box.workdir();
261
+ const env = box.stubs();
262
+ const r = box.run(['collect', 'im-yzj', imOut(box, 'im-yzj'), TODAY()], { env: { PATH: onlyNode(box), YZJ_GROUP_IDS: 'g1' } });
263
+ assert.equal(r.code, 4);
264
+ const w = box.run(['collect', 'im-yzj', imOut(box, 'im-yzj'), TODAY()], { env: { ...env, YZJ_GROUP_IDS: 'g1', STUB_WHOAMI: 'fail' } });
265
+ assert.equal(w.code, 5);
266
+ assert.match(w.stderr, /whoami 失败/);
267
+ assert.equal(exists(path.join(imOut(box, 'im-yzj'), 'messages-g1.json')), false);
268
+ });
269
+
270
+ test('im-yzj:--limit 超过 CLI 上限(20)时按失败退出,不留上一次的旧文件', (t) => {
271
+ const box = makeSandbox(t);
272
+ box.workdir();
273
+ const env = box.stubs();
274
+ write(path.join(imOut(box, 'im-yzj'), 'messages-g1.json'), '{"stale":true}');
275
+ const r = box.run(['collect', 'im-yzj', imOut(box, 'im-yzj'), TODAY()], { env: { ...env, YZJ_CALL_INTERVAL: '0', YZJ_MESSAGE_LIMIT: '30', YZJ_GROUP_IDS: 'g1' } });
276
+ assert.equal(r.code, 5);
277
+ assert.equal(exists(path.join(imOut(box, 'im-yzj'), 'messages-g1.json')), false);
278
+ });
279
+
280
+ test('im-yzj:配了页数就往前翻到起始日 0 点,几页合并后按时间排序,并记下覆盖的时间段', (t) => {
281
+ const box = makeSandbox(t);
282
+ box.workdir();
283
+ const env = box.stubs();
284
+ fs.rmSync(env.STUB_CALLS, { force: true });
285
+ const r = box.run(['collect', 'im-yzj', imOut(box, 'ops-yzj', '2026-11-27'), '2026-11-27'], { env: { ...env, YZJ_CALL_INTERVAL: '0', YZJ_MESSAGE_PAGES_OPS_YZJ: '5', YZJ_GROUP_IDS_OPS_YZJ: 'g9' } });
286
+ const calls = read(env.STUB_CALLS).split('\n').filter(Boolean).length;
287
+ const file = path.join(imOut(box, 'ops-yzj', '2026-11-27'), 'messages-g9.json');
288
+ const ids = JSON.parse(read(file)).data.list.map((m) => m.msgId).join(' ');
289
+ assert.equal(r.code, 0, r.stderr);
290
+ assert.equal(calls, 3);
291
+ assert.equal(ids, 'm1 m2 m3');
292
+ const doc = JSON.parse(read(file));
293
+ assert.equal(doc.data.coverage.from, '2026-11-26 23:50:00.000');
294
+ assert.equal(doc.data.coverage.to, '2026-11-27 09:30:00.000');
295
+ assert.equal(doc.data.groupId, 'g9');
296
+ assert.match(r.stderr, /已拉取 3 条(2 页),覆盖 2026-11-26 23:50:00\.000 至 2026-11-27 09:30:00\.000/);
297
+ });
298
+
299
+ test('im-yzj:配了多页但翻完还覆盖不到起始日 0 点时退出码为 5,不留半段', (t) => {
300
+ const box = makeSandbox(t);
301
+ box.workdir();
302
+ const env = box.stubs();
303
+ const r = box.run(['collect', 'im-yzj', imOut(box, 'ops-yzj', '2026-11-27'), '2026-11-27'], { env: { ...env, YZJ_CALL_INTERVAL: '0', YZJ_MESSAGE_PAGES_OPS_YZJ: '2', YZJ_GROUP_IDS_OPS_YZJ: 'gdeep' } });
304
+ assert.equal(r.code, 5);
305
+ assert.equal(exists(path.join(imOut(box, 'ops-yzj', '2026-11-27'), 'messages-gdeep.json')), false);
306
+ assert.match(r.stderr, /没覆盖到 2026-11-27 00:00/);
307
+ });
308
+
309
+ test('im-yzj:只配 1 页时不要求覆盖到起始日 0 点;页数不是正整数时退出码为 2', (t) => {
310
+ const box = makeSandbox(t);
311
+ box.workdir();
312
+ const env = box.stubs();
313
+ const r = box.run(['collect', 'im-yzj', imOut(box, 'ops-yzj', '2026-11-27'), '2026-11-27'], { env: { ...env, YZJ_CALL_INTERVAL: '0', YZJ_GROUP_IDS_OPS_YZJ: 'gdeep' } });
314
+ const n = JSON.parse(read(path.join(imOut(box, 'ops-yzj', '2026-11-27'), 'messages-gdeep.json'))).data.list.length;
315
+ assert.equal(r.code, 0);
316
+ assert.equal(n, 2);
317
+ const zero = box.run(['collect', 'im-yzj', imOut(box, 'ops-yzj'), TODAY()], { env: { ...env, YZJ_CALL_INTERVAL: '0', YZJ_MESSAGE_PAGES_OPS_YZJ: '0', YZJ_GROUP_IDS_OPS_YZJ: 'g9' } });
318
+ assert.equal(zero.code, 2);
319
+ assert.match(zero.stderr, /YZJ_MESSAGE_PAGES_OPS_YZJ 要是正整数(现在是 '0')/);
320
+ });
321
+
322
+ // ---------------------------------------------------------------- approval-yzj
323
+ test('approval-yzj:翻页取齐,按单据类型汇总待办数和新到数,只留类型和数量,输出目录只有一张表', (t) => {
324
+ const box = makeSandbox(t);
325
+ box.workdir();
326
+ const env = box.stubs();
327
+ const out = path.join(box.root, 'out', 'approval');
328
+ const r = box.run(['collect', 'approval-yzj', out, '2026-11-27'], { env: { ...env, TZ: 'Asia/Shanghai', YZJ_CALL_INTERVAL: '0' } });
329
+ const text = read(path.join(out, 'approvals.tsv'));
330
+ assert.equal(r.code, 0, r.stderr);
331
+ assert.equal(text.includes(`云之家日常部署流程${T}2${T}2${T}2026-11-27 18:00`), true);
332
+ assert.equal(text.includes(`流程助手${T}1${T}0${T}2026-11-16 13:30`), true);
333
+ assert.equal(text.includes('不该留下'), false);
334
+ assert.equal(fs.readdirSync(out).filter((f) => !f.startsWith('.')).length, 1);
335
+ assert.equal(text.startsWith('type\tpending\tnew\tnewest\n'), true);
336
+ // 待办数多的在前
337
+ assert.equal(text.trimEnd().split('\n')[1].startsWith('云之家日常部署流程'), true);
338
+ });
339
+
340
+ test('approval-yzj:翻页中途失败时退出码为 5,不出半份清单', (t) => {
341
+ const box = makeSandbox(t);
342
+ box.workdir();
343
+ const env = box.stubs();
344
+ const out = path.join(box.root, 'out', 'approval-partial');
345
+ const r = box.run(['collect', 'approval-yzj', out, '2026-11-27'], { env: { ...env, TZ: 'Asia/Shanghai', YZJ_CALL_INTERVAL: '0', STUB_YZJ_APPROVAL: 'failpage2' } });
346
+ assert.equal(r.code, 5);
347
+ assert.equal(exists(path.join(out, 'approvals.tsv')), false);
348
+ assert.match(r.stderr, /拉取失败/);
349
+ });
350
+
351
+ test('approval-yzj:非法日期退出码为 2;没装 yzj-cli 时退出码为 4', (t) => {
352
+ const box = makeSandbox(t);
353
+ box.workdir();
354
+ const env = box.stubs();
355
+ assert.equal(box.run(['collect', 'approval-yzj', path.join(box.root, 'out', 'approval-date'), '2026-13-40'], { env: { ...env, TZ: 'Asia/Shanghai' } }).code, 2);
356
+ assert.equal(box.run(['collect', 'approval-yzj', path.join(box.root, 'out', 'approval-nocli'), '2026-11-27'], { env: { PATH: onlyNode(box) } }).code, 4);
357
+ });
358
+
359
+ // ---------------------------------------------------------------- linky-issues
360
+ const rowsOf = (text) => text.trimEnd().split('\n').slice(1);
361
+
362
+ test('linky-issues:只留未完成的,去掉模块编号后缀,标题里的制表符换成空格,输出目录只有一张表', (t) => {
363
+ const box = makeSandbox(t);
364
+ box.workdir();
365
+ const env = box.stubs();
366
+ const out = path.join(box.root, 'out', 'linky');
367
+ const r = box.run(['collect', 'linky-issues', out, '2026-11-27'], { env });
368
+ const text = read(path.join(out, 'issues.tsv'));
369
+ assert.equal(r.code, 0, r.stderr);
370
+ assert.equal(rowsOf(text).length, 2);
371
+ assert.equal(text.includes(`会议${T}导出接口设计`), true);
372
+ assert.equal(text.includes(`导出${T}修复导出 换行`), true);
373
+ assert.equal(text.includes('已经做完的缺陷'), false);
374
+ assert.equal(fs.readdirSync(out).length, 1);
375
+ assert.equal(r.stderr, 'linky-issues: 未完成工作项 2 条,其中 2026-11-27 之后更新过 2 条\n');
376
+ });
377
+
378
+ test('linky-issues:按最近更新倒序', (t) => {
379
+ const box = makeSandbox(t);
380
+ box.workdir();
381
+ const env = box.stubs();
382
+ const out = path.join(box.root, 'out', 'linky');
383
+ box.run(['collect', 'linky-issues', out, '2026-11-27'], { env });
384
+ assert.equal(rowsOf(read(path.join(out, 'issues.tsv')))[0].split('\t')[1], 'US-2026-000201');
385
+ });
386
+
387
+ test('linky-issues:导出不完整、未登录时退出码为 5,不出半份清单', (t) => {
388
+ const box = makeSandbox(t);
389
+ box.workdir();
390
+ const env = box.stubs();
391
+ const incomplete = box.run(['collect', 'linky-issues', path.join(box.root, 'out', 'linky-incomplete'), '2026-11-27'], { env: { ...env, STUB_LINKY_MODE: 'incomplete' } });
392
+ assert.equal(incomplete.code, 5);
393
+ assert.equal(exists(path.join(box.root, 'out', 'linky-incomplete', 'issues.tsv')), false);
394
+ assert.match(incomplete.stderr, /导出不完整/);
395
+ const loggedout = box.run(['collect', 'linky-issues', path.join(box.root, 'out', 'linky-loggedout'), '2026-11-27'], { env: { ...env, STUB_LINKY_MODE: 'loggedout' } });
396
+ assert.equal(loggedout.code, 5);
397
+ assert.match(loggedout.stderr, /linky 未登录/);
398
+ });
399
+
400
+ test('linky-issues:没装 linky 时退出码为 4', (t) => {
401
+ const box = makeSandbox(t);
402
+ box.workdir();
403
+ const r = box.run(['collect', 'linky-issues', path.join(box.root, 'out', 'linky-nocli'), '2026-11-27'], { env: { PATH: onlyNode(box) } });
404
+ assert.equal(r.code, 4);
405
+ assert.match(r.stderr, /未安装 linky/);
406
+ });
407
+
408
+ // ---------------------------------------------------------------- sync
409
+ test('sync:依次运行已启用的源,原始数据落 01-inbox/<源>/<今天>,日志记每一次', (t) => {
410
+ const box = makeSandbox(t);
411
+ box.workdir();
412
+ const ws = path.join(box.root, 'ws');
413
+ gitRepo(path.join(ws, 'demo'));
414
+ fs.writeFileSync(path.join(ws, 'demo', 'a.js'), '');
415
+ write(path.join(box.work, 'collectors', 'local.env'), `GIT_ACTIVITY_WORKSPACES="${ws}"\nGIT_ACTIVITY_AUTHORS="selftest@example.com"\nFILE_ACTIVITY_WORKSPACES="${ws}"\n`);
416
+ const r = box.run(['sync']);
417
+ assert.equal(r.code, 0, r.out);
418
+ assert.equal(r.stdout.includes('ok git-activity'), true);
419
+ assert.equal(r.stdout.includes('ok file-activity'), true);
420
+ assert.equal(exists(path.join(box.work, '01-inbox', 'git-activity', TODAY(), 'commits.tsv')), true);
421
+ const log = read(path.join(box.work, '01-inbox', '_sync.log'));
422
+ assert.match(log, /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} sync 开始 since=\d{4}-\d{2}-\d{2}\n/);
423
+ assert.match(log, /\d{2}:\d{2}:\d{2} git-activity ok\n/);
424
+ assert.match(log, /git-activity: 自 \d{4}-\d{2}-\d{2} 起 1 条记录,涉及 1 个仓库/);
425
+ });
426
+
427
+ test('sync:指定源 id 时只跑那一个', (t) => {
428
+ const box = makeSandbox(t);
429
+ box.workdir();
430
+ const ws = path.join(box.root, 'ws');
431
+ gitRepo(path.join(ws, 'demo'));
432
+ fs.writeFileSync(path.join(ws, 'demo', 'a.js'), '');
433
+ write(path.join(box.work, 'collectors', 'local.env'), `GIT_ACTIVITY_WORKSPACES="${ws}"\nGIT_ACTIVITY_AUTHORS="selftest@example.com"\nFILE_ACTIVITY_WORKSPACES="${ws}"\n`);
434
+ const r = box.run(['sync', TODAY(), 'git-activity']);
435
+ assert.equal(r.code, 0);
436
+ assert.equal(r.stdout.includes('ok git-activity'), true);
437
+ assert.equal(r.stdout.includes('file-activity'), false);
438
+ const none = box.run(['sync', TODAY(), 'nope']);
439
+ assert.equal(none.code, 0);
440
+ assert.equal(none.stdout, 'sync: 没有可运行的源(看 00-config/sources.yaml 里的 enabled)\n');
441
+ });
442
+
443
+ test('sync:共用一个采集器的源,输出落在自己的源 id 目录下', (t) => {
444
+ const box = makeSandbox(t);
445
+ box.workdir();
446
+ const env = box.stubs();
447
+ fs.appendFileSync(path.join(box.work, '00-config', 'sources.yaml'), '\n - id: ops-yzj\n name: 自测用\n enabled: true\n collector: im-yzj\n cadence: on-demand\n extract: |\n 仅用于自测。\n');
448
+ const r = box.run(['sync', TODAY(), 'ops-yzj'], { env: { ...env, YZJ_CALL_INTERVAL: '0', YZJ_GROUP_IDS: 'g1', YZJ_GROUP_IDS_OPS_YZJ: 'g9' } });
449
+ assert.equal(r.code, 0, r.out);
450
+ assert.equal(r.stdout.includes('ok ops-yzj'), true);
451
+ assert.equal(r.stdout.includes('im-yzj'), false);
452
+ assert.equal(exists(path.join(box.work, '01-inbox', 'ops-yzj', TODAY(), 'messages-g9.json')), true);
453
+ assert.equal(exists(path.join(box.work, '01-inbox', 'im-yzj')), false);
454
+ });
455
+
456
+ test('sync:源的 id 行带行内注释时,仍按源 id 落目录和筛选', (t) => {
457
+ const box = makeSandbox(t);
458
+ box.workdir();
459
+ fs.appendFileSync(path.join(box.work, '00-config', 'sources.yaml'), '\n - id: noted # 这一行带了行内注释\n name: 自测用\n enabled: true\n collector: collectors/echo.js\n cadence: on-demand\n extract: |\n 仅用于自测。\n');
460
+ write(path.join(box.work, 'collectors', 'echo.js'), "'use strict';\nconst fs = require('node:fs');\nfs.mkdirSync(process.argv[2], { recursive: true });\nfs.writeFileSync(process.argv[2] + '/done.txt', 'ok');\n");
461
+ const r = box.run(['sync', TODAY(), 'noted']);
462
+ assert.equal(r.code, 0, r.out);
463
+ assert.equal(r.stdout.includes('ok noted'), true);
464
+ assert.equal(r.stdout.includes('git-activity'), false);
465
+ assert.equal(exists(path.join(box.work, '01-inbox', 'noted', TODAY(), 'done.txt')), true);
466
+ assert.equal(exists(path.join(box.work, '01-inbox', '这一行带了行内注释')), false);
467
+ });
468
+
469
+ test('sync:采集脚本不一定是 js:有可执行权限、带 shebang 的直接运行', (t) => {
470
+ const box = makeSandbox(t);
471
+ box.workdir();
472
+ fs.appendFileSync(path.join(box.work, '00-config', 'sources.yaml'), '\n - id: shell\n name: 自测用\n enabled: true\n collector: collectors/direct.sh\n cadence: on-demand\n extract: |\n 仅用于自测。\n');
473
+ const file = path.join(box.work, 'collectors', 'direct.sh');
474
+ write(file, '#!/usr/bin/env bash\nmkdir -p "$1"\necho sh > "$1/done.txt"\n');
475
+ fs.chmodSync(file, 0o755);
476
+ const r = box.run(['sync', TODAY(), 'shell']);
477
+ assert.equal(r.code, 0, r.out);
478
+ assert.equal(read(path.join(box.work, '01-inbox', 'shell', TODAY(), 'done.txt')), 'sh\n');
479
+ });
480
+
481
+ test('sync:失败的源和缺脚本的源各记一次失败,退出码等于失败数', (t) => {
482
+ const box = makeSandbox(t);
483
+ box.workdir();
484
+ fs.appendFileSync(path.join(box.work, '00-config', 'sources.yaml'), [
485
+ '',
486
+ ' - id: broken',
487
+ ' name: 会失败的源',
488
+ ' enabled: true',
489
+ ' collector: collectors/broken.js',
490
+ ' cadence: on-demand',
491
+ ' extract: |',
492
+ ' 仅用于自测。',
493
+ '',
494
+ ' - id: nofile',
495
+ ' name: 缺采集脚本的源',
496
+ ' enabled: true',
497
+ ' collector: collectors/nope.js',
498
+ ' cadence: on-demand',
499
+ ' extract: |',
500
+ ' 仅用于自测。',
501
+ '',
502
+ ].join('\n'));
503
+ write(path.join(box.work, 'collectors', 'broken.js'), "process.stderr.write('broken: 远端失败\\n');\nprocess.exit(5);\n");
504
+ const r = box.run(['sync', TODAY(), 'broken', 'nofile']);
505
+ assert.equal(r.code, 2, r.out);
506
+ assert.equal(r.stdout.includes('FAIL broken'), true);
507
+ assert.equal(r.stdout.includes('FAIL nofile(采集脚本不存在: collectors/nope.js)'), true);
508
+ assert.match(read(path.join(box.work, '01-inbox', '_sync.log')), /\d{2}:\d{2}:\d{2} broken 失败\n/);
509
+ assert.match(read(path.join(box.work, '01-inbox', '_sync.log')), /broken: 远端失败/);
510
+ });
511
+
512
+ // ---------------------------------------------------------------- file-activity 的 Spotlight 路径
513
+ // 真机上踩过:查询串里的 $time 前面多了一个反斜杠,Spotlight 一条都不返回,又不报错,结果少了四分之三(2026-12-01)
514
+ test('file-activity:被 Spotlight 索引的目录用它的结果,并和最近 30 分钟的逐层扫描合并去重', (t) => {
515
+ const box = makeSandbox(t);
516
+ box.workdir();
517
+ const env = box.stubs();
518
+ const ws = path.join(box.root, 'ws');
519
+ // 索引里有的两个文件;一个刚改、还没进索引的;一个在被跳过的目录里
520
+ const indexed = [
521
+ path.join(ws, 'demo', 'old.js'),
522
+ path.join(ws, 'demo', 'README.md'),
523
+ path.join(ws, 'demo', 'node_modules', 'x', 'dep.js'),
524
+ ];
525
+ fs.mkdirSync(path.join(ws, 'demo', 'src'), { recursive: true });
526
+ fs.mkdirSync(path.join(ws, 'demo', 'node_modules', 'x'), { recursive: true });
527
+ for (const f of indexed) write(f, '');
528
+ fs.utimesSync(path.join(ws, 'demo', 'old.js'), new Date(Date.now() - 3600000), new Date(Date.now() - 3600000));
529
+ write(path.join(ws, 'demo', 'src', 'fresh.js'), ''); // 刚改的
530
+ const out = path.join(box.root, 'out', 'spot');
531
+ const r = box.run(['collect', 'file-activity', out, TODAY()], { env: { ...env, FILE_ACTIVITY_WORKSPACES: ws, STUB_MDFIND_PATHS: indexed.join('\n') } });
532
+ assert.equal(r.code, 0, r.stderr);
533
+ const query = read(`${env.STUB_MDFIND_LOG}.query`);
534
+ assert.equal(query.includes('$time.iso('), true);
535
+ assert.equal(query.includes('\\$time'), false);
536
+ const lines = read(path.join(out, 'summary.tsv')).split('\n').slice(1, -1);
537
+ assert.deepEqual(lines, [`ws/demo\t3\t.js 2 .md 1\t`]);
538
+ assert.match(r.stderr, /Spotlight 1 个目录,其中 0 个没返回结果/);
539
+ });
540
+
541
+ test('file-activity:Spotlight 什么都没返回时(目录没被索引的典型表现)整个改用逐层扫描,不把空结果当没有改动', (t) => {
542
+ const box = makeSandbox(t);
543
+ box.workdir();
544
+ const env = box.stubs();
545
+ const ws = path.join(box.root, 'ws');
546
+ fs.mkdirSync(path.join(ws, 'demo', 'src'), { recursive: true });
547
+ write(path.join(ws, 'demo', 'src', 'a.js'), '');
548
+ // 改动时间在一小时前:不在「最近 30 分钟」里,只有从起始日扫才找得到。刚过零点时一小时前是昨天,所以起始日用昨天
549
+ const anHourAgo = new Date(Date.now() - 3600000);
550
+ fs.utimesSync(path.join(ws, 'demo', 'src', 'a.js'), anHourAgo, anHourAgo);
551
+ const yesterday = `${anHourAgo.getFullYear()}-${String(anHourAgo.getMonth() + 1).padStart(2, '0')}-${String(anHourAgo.getDate()).padStart(2, '0')}`;
552
+ const out = path.join(box.root, 'out', 'spot-empty');
553
+ const r = box.run(['collect', 'file-activity', out, yesterday], { env: { ...env, FILE_ACTIVITY_WORKSPACES: ws, STUB_MDFIND: 'empty' } });
554
+ assert.equal(r.code, 0, r.stderr);
555
+ assert.equal(read(path.join(out, 'summary.tsv')).includes('ws/demo\t1\t.js 1'), true);
556
+ assert.match(r.stderr, /Spotlight 1 个目录,其中 1 个没返回结果、已改用逐层扫描复核/);
557
+ });
558
+
559
+ test('file-activity:本机没有 Spotlight(或目录没被索引)时直接逐层扫描', (t) => {
560
+ const box = makeSandbox(t);
561
+ box.workdir();
562
+ const env = box.stubs();
563
+ const ws = path.join(box.root, 'ws');
564
+ fs.mkdirSync(path.join(ws, 'demo'), { recursive: true });
565
+ write(path.join(ws, 'demo', 'a.js'), '');
566
+ const out = path.join(box.root, 'out', 'spot-null');
567
+ const unindexed = box.run(['collect', 'file-activity', out, TODAY()], { env: { ...env, FILE_ACTIVITY_WORKSPACES: ws, STUB_MDLS: '(null)' } });
568
+ assert.equal(unindexed.code, 0);
569
+ assert.match(unindexed.stderr, /逐层扫描 1 个目录/);
570
+ const noTool = box.run(['collect', 'file-activity', path.join(box.root, 'out', 'spot-none'), TODAY()], { env: { PATH: onlyNode(box), FILE_ACTIVITY_WORKSPACES: ws } });
571
+ assert.equal(noTool.code, 0);
572
+ assert.equal(exists(path.join(box.root, 'out', 'spot-none', 'summary.tsv')), true);
573
+ assert.equal(exists(`${env.STUB_MDFIND_LOG}.query`), false);
574
+ });