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.
- package/.agents/notes/AGENTS.md +84 -0
- package/.agents/notes/decisions/2026-11-28-long-term-goals-ride-existing-tasks.md +36 -0
- package/.agents/notes/decisions/2026-11-29-building-a-workdir-means-cloning-the-template.md +25 -0
- package/.agents/notes/decisions/2026-11-29-find-workdir-cwd-first-then-registered.md +32 -0
- package/.agents/notes/decisions/2026-11-30-bot-inbound-uses-websocket-and-a-responder-command.md +48 -0
- package/.agents/notes/decisions/2026-11-30-cost-entry-writes-into-empty-cells-directly.md +25 -0
- package/.agents/notes/decisions/2026-11-30-dependencies-are-allowed-user-installs-missing-ones.md +30 -0
- package/.agents/notes/decisions/2026-11-30-markdown-is-the-record-html-is-only-a-view.md +25 -0
- package/.agents/notes/decisions/2026-11-30-message-channels-are-preset-in-the-skill.md +34 -0
- package/.agents/notes/decisions/2026-11-30-no-workdir-offers-to-create-one-by-default.md +34 -0
- package/.agents/notes/decisions/2026-11-30-paging-requires-coverage-only-when-enabled.md +27 -0
- package/.agents/notes/decisions/2026-11-30-pending-decisions-asked-in-a-chat-and-logged-briefly.md +39 -0
- package/.agents/notes/decisions/2026-11-30-push-via-personal-robot-webhook.md +33 -0
- package/.agents/notes/decisions/2026-11-30-scheduling-moves-to-a-local-service.md +37 -0
- package/.agents/notes/decisions/2026-11-30-skill-ships-with-its-template-in-a-zip.md +30 -0
- package/.agents/notes/decisions/2026-11-30-skill-stays-generic-user-provides-cli-or-mcp.md +25 -0
- package/.agents/notes/decisions/2026-12-01-everything-is-js-behind-work-rhythm.md +45 -0
- package/.agents/notes/pitfalls/2026-11-29-do-not-hand-roll-git-log.md +22 -0
- package/.agents/notes/pitfalls/2026-11-29-empty-or-partial-collector-output.md +23 -0
- package/.agents/notes/pitfalls/2026-11-29-skill-description-is-truncated.md +21 -0
- package/.agents/notes/pitfalls/2026-11-29-tool-shell-is-zsh.md +24 -0
- package/.agents/notes/pitfalls/2026-11-30-compare-timestamps-as-digits.md +22 -0
- package/.agents/notes/pitfalls/2026-11-30-message-search-lags-after-sending.md +18 -0
- package/.agents/notes/pitfalls/2026-11-30-rewriting-a-file-with-empty-output-after-tool-failure.md +20 -0
- package/.agents/notes/pitfalls/2026-11-30-secret-scan-false-positive-on-hyphenated-file-names.md +18 -0
- package/.agents/notes/pitfalls/2026-11-30-selftest-must-not-inherit-local-config.md +29 -0
- package/.agents/notes/pitfalls/2026-11-30-yzj-cli-list-limit-is-20.md +25 -0
- package/.agents/notes/pitfalls/2026-11-30-zsh-no-word-splitting-special-names.md +23 -0
- package/.agents/notes/pitfalls/2026-12-01-node-test-hooks-cannot-register-after-callbacks.md +22 -0
- package/.agents/notes/pitfalls/2026-12-01-spawning-node-test-inside-a-test-runs-nothing.md +18 -0
- package/.agents/skills/work-rhythm/SKILL.md +277 -0
- package/00-config/profile.example.yaml +54 -0
- package/00-config/sources.yaml +115 -0
- package/AGENTS.md +31 -0
- package/README.md +105 -2
- package/collectors/approval-yzj.js +114 -0
- package/collectors/calendar-yzj.js +48 -0
- package/collectors/file-activity.js +131 -0
- package/collectors/git-activity.js +105 -0
- package/collectors/im-yzj.js +151 -0
- package/collectors/linky-issues.js +88 -0
- package/collectors/local.env.example +52 -0
- package/docs/channels.md +95 -0
- package/docs/formats.md +285 -0
- package/docs/im-adapters.md +79 -0
- package/docs/scheduled-tasks.md +115 -0
- package/docs/sources.md +104 -0
- package/package.json +37 -4
- package/scripts/cli.js +98 -0
- package/scripts/cmd/check.js +297 -0
- package/scripts/cmd/collect.js +25 -0
- package/scripts/cmd/decisions.js +236 -0
- package/scripts/cmd/goals.js +145 -0
- package/scripts/cmd/init.js +53 -0
- package/scripts/cmd/listen.js +91 -0
- package/scripts/cmd/notify.js +42 -0
- package/scripts/cmd/pack.js +105 -0
- package/scripts/cmd/rhythm.js +102 -0
- package/scripts/cmd/selftest.js +23 -0
- package/scripts/cmd/sync.js +73 -0
- package/scripts/cmd/today.js +29 -0
- package/scripts/cmd/workdir.js +81 -0
- package/scripts/lib/agent.js +270 -0
- package/scripts/lib/bot.js +376 -0
- package/scripts/lib/collector.js +74 -0
- package/scripts/lib/config.js +250 -0
- package/scripts/lib/launchd.js +82 -0
- package/scripts/lib/log.js +22 -0
- package/scripts/lib/markdown.js +208 -0
- package/scripts/lib/notify.js +43 -0
- package/scripts/lib/rhythm.js +508 -0
- package/scripts/lib/robot.js +64 -0
- package/scripts/lib/run.js +151 -0
- package/scripts/lib/template.js +144 -0
- package/scripts/lib/workdir.js +89 -0
- package/scripts/test/check.test.js +217 -0
- package/scripts/test/collectors.test.js +574 -0
- package/scripts/test/config.test.js +119 -0
- package/scripts/test/decisions.test.js +129 -0
- package/scripts/test/fake-webhook.js +31 -0
- package/scripts/test/goals.test.js +109 -0
- package/scripts/test/harness.js +279 -0
- package/scripts/test/init.test.js +125 -0
- package/scripts/test/listen.test.js +362 -0
- package/scripts/test/notify.test.js +176 -0
- package/scripts/test/pack.test.js +141 -0
- package/scripts/test/rhythm.test.js +324 -0
- package/scripts/test/stubs.js +140 -0
- package/scripts/test/today.test.js +73 -0
- package/scripts/test/workdir.test.js +152 -0
- package/scripts/work-rhythm.js +18 -0
- package/tasks/alerts-morning.md +24 -0
- package/tasks/carryover.md +16 -0
- package/tasks/daily-plan.md +29 -0
- package/tasks/evening-review.md +27 -0
- package/tasks/linky-cost-check.md +13 -0
- package/tasks/linky-cost-fill.md +20 -0
- package/tasks/noon-checkin.md +15 -0
- package/tasks/weekly-rhythm.md +18 -0
- package/templates/03-carryover/CARRYOVER.md +14 -0
- package/templates/03-carryover/DECISIONS.md +10 -0
- package/templates/04-patterns/WORK_RHYTHM.md +16 -0
|
@@ -0,0 +1,141 @@
|
|
|
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 { spawnSync } = require('node:child_process');
|
|
7
|
+
const { PACKAGE_ROOT, NAME, VERSION, makeSandbox, baseEnv, cleanSources, gitRepo, run: rawRun, read, exists, write } = require('./harness');
|
|
8
|
+
const { which } = require('../lib/run');
|
|
9
|
+
|
|
10
|
+
const npm = () => which('npm');
|
|
11
|
+
// 把包按 package.json 的 files 清单复制到一个临时目录(npm 总会带上的 package.json 也算),再往里塞几个不该被带出去的东西
|
|
12
|
+
function stagePackage(box) {
|
|
13
|
+
const stage = path.join(box.root, 'pkg');
|
|
14
|
+
const pkg = JSON.parse(read(path.join(PACKAGE_ROOT, 'package.json')));
|
|
15
|
+
// files 里可以带 * 这种通配(如 collectors/*.js),按包根下的文件名展开
|
|
16
|
+
const expand = (entry) => {
|
|
17
|
+
if (!entry.includes('*')) return [entry];
|
|
18
|
+
const dir = path.dirname(entry);
|
|
19
|
+
const pattern = new RegExp(`^${path.basename(entry).replace(/\./g, '\\.').replace(/\*/g, '.*')}$`);
|
|
20
|
+
return fs.readdirSync(path.join(PACKAGE_ROOT, dir)).filter((n) => pattern.test(n)).map((n) => path.posix.join(dir, n));
|
|
21
|
+
};
|
|
22
|
+
for (const entry of ['package.json', ...pkg.files.flatMap(expand)]) {
|
|
23
|
+
const from = path.join(PACKAGE_ROOT, entry);
|
|
24
|
+
const to = path.join(stage, entry);
|
|
25
|
+
fs.mkdirSync(path.dirname(to), { recursive: true });
|
|
26
|
+
fs.cpSync(from, to, { recursive: true });
|
|
27
|
+
}
|
|
28
|
+
write(path.join(stage, 'collectors', 'local.env'), 'YZJ_MY_OPEN_ID="secret"\n');
|
|
29
|
+
write(path.join(stage, '01-inbox', 'x', 'raw.json'), '{}');
|
|
30
|
+
write(path.join(stage, '00-config', 'profile.yaml'), 'setup:\n status: done\n');
|
|
31
|
+
write(path.join(stage, '02-daily', '2026-01', '2026-01-01.md'), '# 2026-01-01\n');
|
|
32
|
+
write(path.join(stage, '03-carryover', 'DECISIONS.md'), '| 2026-01-01 | 我的决定 |\n');
|
|
33
|
+
return stage;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function packJson(stage, cwd, env) {
|
|
37
|
+
const r = spawnSync('npm', ['pack', stage, '--json', '--ignore-scripts'], { cwd, env, encoding: 'utf8', timeout: 120000 });
|
|
38
|
+
assert.equal(r.status, 0, r.stderr);
|
|
39
|
+
return JSON.parse(r.stdout)[0];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
test('npm pack:包里带的是脚本、采集器、文档、skill 和空白模板;不带记录、本机配置、原始数据,也不带填好的 profile', (t) => {
|
|
43
|
+
if (!npm()) return t.skip('本机没有 npm');
|
|
44
|
+
const box = makeSandbox(t);
|
|
45
|
+
const stage = stagePackage(box);
|
|
46
|
+
const out = path.join(box.root, 'out');
|
|
47
|
+
fs.mkdirSync(out);
|
|
48
|
+
const packed = packJson(stage, out, baseEnv(box.stubEnv));
|
|
49
|
+
const files = packed.files.map((f) => f.path);
|
|
50
|
+
assert.equal(packed.filename, `${NAME.replace(/^@/, '').replace('/', '-')}-${VERSION}.tgz`);
|
|
51
|
+
for (const need of [
|
|
52
|
+
'package.json', 'scripts/work-rhythm.js', 'scripts/cli.js', 'scripts/cmd/init.js', 'scripts/lib/template.js', 'scripts/test/harness.js',
|
|
53
|
+
'collectors/git-activity.js', 'collectors/im-yzj.js', 'collectors/local.env.example',
|
|
54
|
+
'docs/formats.md', 'docs/channels.md', 'tasks/daily-plan.md', '.agents/skills/work-rhythm/SKILL.md', '.agents/notes/AGENTS.md',
|
|
55
|
+
'00-config/profile.example.yaml', '00-config/sources.yaml', 'templates/03-carryover/DECISIONS.md', 'templates/03-carryover/CARRYOVER.md', 'templates/04-patterns/WORK_RHYTHM.md',
|
|
56
|
+
'AGENTS.md', 'README.md',
|
|
57
|
+
]) assert.ok(files.includes(need), `包里没有 ${need}`);
|
|
58
|
+
for (const banned of ['collectors/local.env', '01-inbox/x/raw.json', '00-config/profile.yaml', '02-daily/2026-01/2026-01-01.md', '03-carryover/DECISIONS.md', '04-patterns/WORK_RHYTHM.md', '.gitignore', '.npmignore']) {
|
|
59
|
+
assert.ok(!files.includes(banned), `包里不该有 ${banned}`);
|
|
60
|
+
}
|
|
61
|
+
assert.ok(files.every((f) => !f.startsWith('01-inbox/') && !f.startsWith('02-daily/') && !f.startsWith('03-carryover/') && !f.startsWith('04-patterns/') && !f.startsWith('05-goals/')));
|
|
62
|
+
assert.ok(exists(path.join(out, packed.filename)), 'tgz 要落在指定目录');
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('从打出来的包安装之后,work-rhythm init / check / today / sync 都能用(这就是别人装上之后走的路)', (t) => {
|
|
66
|
+
if (!npm()) return t.skip('本机没有 npm');
|
|
67
|
+
const box = makeSandbox(t);
|
|
68
|
+
const stage = stagePackage(box);
|
|
69
|
+
const out = path.join(box.root, 'out');
|
|
70
|
+
fs.mkdirSync(out);
|
|
71
|
+
const packed = packJson(stage, out, baseEnv(box.stubEnv));
|
|
72
|
+
const tgz = path.join(out, packed.filename);
|
|
73
|
+
// 离线装到一个临时位置(没有第三方依赖,不用联网)
|
|
74
|
+
const prefix = path.join(box.root, 'install');
|
|
75
|
+
const install = spawnSync('npm', ['install', '--prefix', prefix, '--no-audit', '--no-fund', '--no-package-lock', '--ignore-scripts', '--offline', tgz], { cwd: box.root, env: baseEnv(box.stubEnv), encoding: 'utf8', timeout: 120000 });
|
|
76
|
+
assert.equal(install.status, 0, install.stderr);
|
|
77
|
+
const bin = path.join(prefix, 'node_modules', '.bin', 'work-rhythm');
|
|
78
|
+
assert.ok(exists(bin), '装完没有 work-rhythm 这个命令');
|
|
79
|
+
const cfg = path.join(box.root, 'wr-config.yaml');
|
|
80
|
+
const env = baseEnv({ ...box.stubEnv, WORK_RHYTHM_CONFIG: cfg });
|
|
81
|
+
const wr = (args, cwd) => {
|
|
82
|
+
const r = spawnSync(bin, args, { cwd, env, encoding: 'utf8', timeout: 120000 });
|
|
83
|
+
return { code: r.status, stdout: r.stdout || '', stderr: r.stderr || '', out: `${r.stdout || ''}${r.stderr || ''}` };
|
|
84
|
+
};
|
|
85
|
+
assert.equal(wr(['--version'], box.root).stdout, `${VERSION}\n`);
|
|
86
|
+
assert.equal(wr(['init', './demo'], box.root).code, 0);
|
|
87
|
+
const demo = path.join(box.root, 'demo');
|
|
88
|
+
assert.equal(read(cfg), `workdir: ${fs.realpathSync(demo)}\n`);
|
|
89
|
+
assert.ok(exists(path.join(demo, '00-config', 'profile.yaml')));
|
|
90
|
+
const chk = wr(['check'], demo);
|
|
91
|
+
assert.equal(chk.code, 0, chk.out);
|
|
92
|
+
assert.match(chk.stdout, /== 6\/6 笔记格式 ==\n全部通过\n/);
|
|
93
|
+
assert.match(wr(['today'], demo).stdout, /^DATE=\d{4}-\d{2}-\d{2}\n/);
|
|
94
|
+
// 采集:内置采集器在包里
|
|
95
|
+
const ws = path.join(box.root, 'ws');
|
|
96
|
+
gitRepo(path.join(ws, 'demo'));
|
|
97
|
+
write(path.join(demo, '00-config', 'sources.yaml'), cleanSources());
|
|
98
|
+
write(path.join(demo, 'collectors', 'local.env'), `GIT_ACTIVITY_WORKSPACES="${ws}"\nGIT_ACTIVITY_AUTHORS="selftest@example.com"\nFILE_ACTIVITY_WORKSPACES="${ws}"\n`);
|
|
99
|
+
const sync = wr(['sync'], demo);
|
|
100
|
+
assert.equal(sync.code, 0, sync.out);
|
|
101
|
+
assert.match(sync.stdout, /ok git-activity\n/);
|
|
102
|
+
// 装出来的包里带着用例,自测也能跑
|
|
103
|
+
const self = wr(['selftest', '--test-name-pattern=today:周五是工作日'], demo);
|
|
104
|
+
assert.equal(self.code, 0, self.out);
|
|
105
|
+
assert.match(self.out, /today:周五是工作日,每日文件按月分目录,四行输出的顺序固定/);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test('pack:出 skill 加 npm 包的目录(有 zip 就再压一份),说明里写了怎么装', (t) => {
|
|
109
|
+
if (!npm()) return t.skip('本机没有 npm');
|
|
110
|
+
const box = makeSandbox(t);
|
|
111
|
+
box.workdir();
|
|
112
|
+
const out = path.join(box.root, 'outbox');
|
|
113
|
+
const r = rawRun(['pack', '--out', out], { cwd: box.work, env: baseEnv(box.stubEnv) });
|
|
114
|
+
assert.equal(r.code, 0, r.out);
|
|
115
|
+
const dir = path.join(out, `work-rhythm-skill-${VERSION}`);
|
|
116
|
+
assert.equal(exists(dir), true, r.out);
|
|
117
|
+
assert.equal(read(path.join(dir, 'SKILL.md')), read(path.join(PACKAGE_ROOT, '.agents', 'skills', 'work-rhythm', 'SKILL.md')));
|
|
118
|
+
assert.match(read(path.join(dir, 'README.md')), new RegExp(`npm install -g ./${NAME.replace(/^@/, '').replace('/', '-')}-${VERSION.replace(/\\./g, '\\.')}\\.tgz`));
|
|
119
|
+
assert.match(read(path.join(dir, 'VERSION')), new RegExp(`^${VERSION.replace(/\\./g, '\\.')}`));
|
|
120
|
+
assert.ok(exists(path.join(dir, `${NAME.replace(/^@/, '').replace('/', '-')}-${VERSION}.tgz`)), 'npm 包要在里面');
|
|
121
|
+
if (which('zip')) {
|
|
122
|
+
assert.ok(exists(`${dir}.zip`), '本机有 zip 就该有 zip');
|
|
123
|
+
assert.equal(r.stdout.trim(), `${dir}.zip`);
|
|
124
|
+
} else {
|
|
125
|
+
assert.equal(r.stdout.trim(), dir);
|
|
126
|
+
}
|
|
127
|
+
// 不带 --out 时放在工作目录的 01-inbox/outbox/(不入库)
|
|
128
|
+
const d = rawRun(['pack'], { cwd: box.work, env: baseEnv(box.stubEnv) });
|
|
129
|
+
assert.equal(d.code, 0, d.out);
|
|
130
|
+
assert.ok(exists(path.join(box.work, '01-inbox', 'outbox', `work-rhythm-skill-${VERSION}`, 'VERSION')));
|
|
131
|
+
assert.equal(rawRun(['pack', '--nope'], { cwd: box.work }).code, 2);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test('selftest:只跑匹配的用例,退出码跟着结果走', (t) => {
|
|
135
|
+
const box = makeSandbox(t);
|
|
136
|
+
const ok = rawRun(['selftest', '--test-name-pattern=today:周五是工作日'], { cwd: box.root });
|
|
137
|
+
assert.equal(ok.code, 0, ok.out);
|
|
138
|
+
assert.match(ok.out, /✔ today:周五是工作日,每日文件按月分目录,四行输出的顺序固定/);
|
|
139
|
+
assert.doesNotMatch(ok.out, /notify:/);
|
|
140
|
+
assert.equal(rawRun(['selftest', '什么'], { cwd: box.root }).code, 2);
|
|
141
|
+
});
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { test, describe, before, after } = 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, webhook, freePort, hookContext, read, write, setKey, exists, gitRepo } = require('./harness');
|
|
7
|
+
|
|
8
|
+
const pad = (n) => String(n).padStart(2, '0');
|
|
9
|
+
const TODAY = () => {
|
|
10
|
+
const d = new Date();
|
|
11
|
+
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
|
|
12
|
+
};
|
|
13
|
+
const AGENT = (box) => path.join(box.bin, 'fake-agent');
|
|
14
|
+
// 不推送的用例里钥匙串的假地址指向 1 端口:fetch 直接拒绝,不会真的往外发。要推送的用例换成假 webhook 的地址
|
|
15
|
+
const DUMMY_URL = 'http://127.0.0.1:1/gateway/robot/webhook/send?yzjtype=0&yzjtoken=stubtoken0123456789abcd';
|
|
16
|
+
const ENVS = (box, url = DUMMY_URL) => ({ ...box.stubs(), STUB_KEYCHAIN: 'ok', STUB_ROBOT_URL: url, YZJ_MY_OPEN_ID: 'me-0001', YZJ_BOT_AGENT: AGENT(box) });
|
|
17
|
+
|
|
18
|
+
// 搭工作目录:消息渠道是 yzj-robot,agent 是假的 fake-agent。set 改 profile.yaml 里缩进两格的键(schedule 的任务、rhythm 的设置)
|
|
19
|
+
function setup(t) {
|
|
20
|
+
const box = makeSandbox(t);
|
|
21
|
+
box.workdir();
|
|
22
|
+
const profile = `${box.work}/00-config/profile.yaml`;
|
|
23
|
+
write(profile, setKey(read(profile), 'notify', '"yzj-robot"'));
|
|
24
|
+
const set = (key, value) => write(profile, setKey(read(profile), key, value));
|
|
25
|
+
const s = { box, profile, set, env: ENVS(box) };
|
|
26
|
+
// 不传 env 就用 s.env(用例可以改它)
|
|
27
|
+
s.rhythm = (args, env) => box.run(['rhythm', ...args], { env: env || s.env });
|
|
28
|
+
return s;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// ---------------------------------------------------------------- 排期
|
|
32
|
+
describe('rhythm due', () => {
|
|
33
|
+
let s;
|
|
34
|
+
let clean;
|
|
35
|
+
before(() => {
|
|
36
|
+
clean = hookContext();
|
|
37
|
+
s = setup(clean);
|
|
38
|
+
s.set('daily-plan', '"08:50"');
|
|
39
|
+
s.set('weekly-rhythm', '"Fri 16:30"');
|
|
40
|
+
s.set('carryover', '""');
|
|
41
|
+
s.set('sync', '"工作日 09:00-18:00 每 60 分钟"');
|
|
42
|
+
});
|
|
43
|
+
after(() => clean.run());
|
|
44
|
+
|
|
45
|
+
test('按 schedule 和 rhythm.sync 列出每天的时间点,不上班的日子不跑', () => {
|
|
46
|
+
const out = s.rhythm(['due', '--at', '2026-12-04 07:00', '--days', '2']).stdout;
|
|
47
|
+
assert.match(out, /2026-12-04 周五\n/);
|
|
48
|
+
assert.match(out, / 08:50 daily-plan\n/);
|
|
49
|
+
assert.match(out, / 16:30 weekly-rhythm\n/);
|
|
50
|
+
assert.match(out, / 09:00 sync\n/);
|
|
51
|
+
assert.equal(out.split('\n').filter((l) => l.endsWith(' sync')).length, 10);
|
|
52
|
+
assert.match(out, /2026-12-05 周六(不上班) 没有要跑的\n/);
|
|
53
|
+
// carryover 留空、缺指令文件的任务都不列
|
|
54
|
+
assert.ok(!out.includes('carryover'));
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('时间写错了的任务指出来,别的照常列出', () => {
|
|
58
|
+
s.set('sync', '""');
|
|
59
|
+
s.set('weekly-rhythm', '"Som 10:00"');
|
|
60
|
+
const r = s.rhythm(['due', '--at', '2026-12-04', '--days', '1']);
|
|
61
|
+
assert.equal(r.code, 0);
|
|
62
|
+
assert.match(r.stdout, /!! weekly-rhythm(不认识的星期:Som 10:00)/);
|
|
63
|
+
assert.match(r.stdout, / 08:50 daily-plan\n/);
|
|
64
|
+
s.set('weekly-rhythm', '"Fri 16:30"');
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('不写 --at 就从现在起;参数不对退出码为 2', () => {
|
|
68
|
+
assert.equal(s.rhythm(['due']).code, 0);
|
|
69
|
+
assert.equal(s.rhythm(['due', '--at', '不是时间']).code, 2);
|
|
70
|
+
assert.equal(s.rhythm(['due', '什么']).code, 2);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// ---------------------------------------------------------------- 页面
|
|
75
|
+
test('rhythm render:标题、列表、勾选框、表格、引用、行内标记、代码块都渲染,javascript: 链接不放行', (t) => {
|
|
76
|
+
const s = setup(t);
|
|
77
|
+
const rel = `02-daily/${TODAY().slice(0, 7)}/${TODAY()}.md`;
|
|
78
|
+
write(`${s.box.work}/${rel}`, `# ${TODAY()} 周一
|
|
79
|
+
|
|
80
|
+
## 计划
|
|
81
|
+
|
|
82
|
+
- **今日重点**:
|
|
83
|
+
1. 写完导出接口文档 —— 建议 09:00–10:00
|
|
84
|
+
2. [ ] 一件没做的
|
|
85
|
+
- [x] 已定的一件
|
|
86
|
+
|
|
87
|
+
## 事件流
|
|
88
|
+
|
|
89
|
+
| 时间 | 类型 | 事项 |
|
|
90
|
+
|------|------|------|
|
|
91
|
+
| 09:05 | 沟通 | 同步进度 |
|
|
92
|
+
|
|
93
|
+
## 备注
|
|
94
|
+
|
|
95
|
+
> 引用一行 \`code\` *斜体* [链接](/carryover) [坏链接](javascript:alert)
|
|
96
|
+
|
|
97
|
+
\`\`\`sh
|
|
98
|
+
echo hi
|
|
99
|
+
\`\`\`
|
|
100
|
+
`);
|
|
101
|
+
const out = s.rhythm(['render', rel]).stdout;
|
|
102
|
+
for (const need of ['<h2>计划</h2>', '<ol>', '<li><input type="checkbox" disabled> 一件没做的</li>', '<li><input type="checkbox" disabled checked> 已定的一件</li>', '<table>', '<strong>今日重点</strong>', '<blockquote>', '<em>斜体</em>', '<code>code</code>', '<a href="/carryover">链接</a>', '<pre><code>echo hi']) {
|
|
103
|
+
assert.ok(out.includes(need), `页面里没有 ${need}`);
|
|
104
|
+
}
|
|
105
|
+
assert.ok(!out.includes('<a href="javascript'), 'javascript: 链接不该放行');
|
|
106
|
+
assert.equal(s.rhythm(['render', 'no-such-file.md']).code, 2);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// ---------------------------------------------------------------- 任务
|
|
110
|
+
test('rhythm run:任务交给 YZJ_BOT_AGENT 指的命令,回复打出来;不带 --push 不推送', async (t) => {
|
|
111
|
+
const hook = await webhook(t);
|
|
112
|
+
const s = setup(t);
|
|
113
|
+
const r = s.rhythm(['run', 'daily-plan'], { ...s.env, STUB_ROBOT_URL: hook.url });
|
|
114
|
+
assert.equal(r.code, 0, r.out);
|
|
115
|
+
assert.match(r.stdout, /回复:/);
|
|
116
|
+
assert.deepEqual(hook.requests(), []);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test('rhythm run:agent 失败时退出码为 5;没有这个任务也是 5', async (t) => {
|
|
120
|
+
const s = setup(t);
|
|
121
|
+
assert.equal(s.rhythm(['run', 'daily-plan'], { ...s.env, STUB_AGENT: 'fail' }).code, 5);
|
|
122
|
+
const r = s.rhythm(['run', 'no-such-task']);
|
|
123
|
+
assert.equal(r.code, 5);
|
|
124
|
+
assert.match(r.stdout, /没跑成:缺 tasks\/no-such-task.md/);
|
|
125
|
+
assert.equal(s.rhythm(['run']).code, 2);
|
|
126
|
+
assert.equal(s.rhythm(['run', 'daily-plan', '--nope']).code, 2);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test('rhythm doctor:配置齐了时退出码为 0;时间写了却缺指令文件的任务报出来', (t) => {
|
|
130
|
+
const s = setup(t);
|
|
131
|
+
assert.equal(s.rhythm(['doctor']).code, 0, s.rhythm(['doctor']).out);
|
|
132
|
+
fs.renameSync(`${s.box.work}/tasks/weekly-rhythm.md`, `${s.box.root}/weekly-rhythm.md`);
|
|
133
|
+
const r = s.rhythm(['doctor']);
|
|
134
|
+
assert.equal(r.code, 1);
|
|
135
|
+
assert.match(r.stdout, /任务 weekly-rhythm 的时间写了「Fri 16:30」,但缺 tasks\/weekly-rhythm.md,没法跑/);
|
|
136
|
+
// 推送开着却没配消息渠道、agent 没配,都报出来
|
|
137
|
+
s.set('notify', '""');
|
|
138
|
+
assert.match(s.rhythm(['doctor']).stdout, /rhythm.push 开着,但 preferences.notify 没配消息渠道/);
|
|
139
|
+
assert.match(s.rhythm(['doctor'], { ...s.env, YZJ_BOT_AGENT: '' }).stdout, /没配 YZJ_BOT_AGENT/);
|
|
140
|
+
// 端口不在范围里
|
|
141
|
+
s.set('port', '80');
|
|
142
|
+
assert.match(s.rhythm(['doctor']).stdout, /rhythm.port 要是 1024 到 65535 之间的整数(现在是 80)/);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test('rhythm start:配置有问题时不起,退出码为 3,缺指令文件不算问题', async (t) => {
|
|
146
|
+
const s = setup(t);
|
|
147
|
+
const port = await freePort();
|
|
148
|
+
s.set('port', String(port));
|
|
149
|
+
fs.renameSync(`${s.box.work}/tasks/weekly-rhythm.md`, `${s.box.root}/weekly-rhythm.md`);
|
|
150
|
+
s.set('notify', '""');
|
|
151
|
+
const r = s.rhythm(['start'], { ...s.env, YZJ_BOT_AGENT: '' });
|
|
152
|
+
assert.equal(r.code, 3);
|
|
153
|
+
assert.match(r.stderr, /配置有问题,先跑 work-rhythm rhythm doctor:\n没配 YZJ_BOT_AGENT/);
|
|
154
|
+
assert.match(r.stderr, /rhythm.push 开着/);
|
|
155
|
+
assert.ok(!r.stderr.includes('weekly-rhythm'), '缺指令文件的任务到点跳过就行,不该拦着不起');
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// ---------------------------------------------------------------- 服务
|
|
159
|
+
describe('rhythm start:服务在跑的时候', () => {
|
|
160
|
+
let s;
|
|
161
|
+
let hook;
|
|
162
|
+
let port;
|
|
163
|
+
let proc;
|
|
164
|
+
let clean;
|
|
165
|
+
const stop = async () => {
|
|
166
|
+
s.rhythm(['stop']);
|
|
167
|
+
await proc.exited;
|
|
168
|
+
};
|
|
169
|
+
before(async () => {
|
|
170
|
+
clean = hookContext();
|
|
171
|
+
hook = await webhook(clean);
|
|
172
|
+
s = setup(clean);
|
|
173
|
+
s.set('daily-plan', '"08:50"');
|
|
174
|
+
port = await freePort();
|
|
175
|
+
s.set('port', String(port));
|
|
176
|
+
s.set('quiet', '[linky-cost-fill, linky-cost-check]');
|
|
177
|
+
s.env = { ...s.env, STUB_ROBOT_URL: hook.url };
|
|
178
|
+
write(`${s.box.work}/02-daily/${TODAY().slice(0, 7)}/${TODAY()}.md`, `# ${TODAY()} 周一\n\n## 计划\n\n- **今日重点**:写完导出接口文档 —— 建议 09:00–10:00\n`);
|
|
179
|
+
proc = s.box.spawn(['rhythm', 'start'], { env: s.env });
|
|
180
|
+
assert.ok(await proc.waitFor('页面:'), proc.text());
|
|
181
|
+
// 收尾按登记的反过来跑:先停服务,再删目录
|
|
182
|
+
clean.after(stop);
|
|
183
|
+
});
|
|
184
|
+
after(() => clean.run());
|
|
185
|
+
|
|
186
|
+
test('status 报出页面地址和接下来的任务', () => {
|
|
187
|
+
const r = s.rhythm(['status']);
|
|
188
|
+
assert.equal(r.code, 0, r.out);
|
|
189
|
+
assert.match(r.stdout, /在跑(pid \d+),页面 http:\/\/127\.0\.0\.1:\d+\n/);
|
|
190
|
+
assert.match(r.stdout, /接下来:\n[\d-]+ 周.-?\n 08:50 daily-plan\n/);
|
|
191
|
+
assert.equal(exists(`${s.box.work}/01-inbox/_rhythm/service.pid`), true);
|
|
192
|
+
assert.match(read(`${s.box.work}/01-inbox/_rhythm.log`), /服务开始 pid=\d+ 页面=http:\/\/127\.0\.0\.1:\d+/);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test('再 start 一次不重复起,退出码为 3', () => {
|
|
196
|
+
const r = s.rhythm(['start']);
|
|
197
|
+
assert.equal(r.code, 3);
|
|
198
|
+
assert.match(r.stderr, /服务已经在跑了/);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
test('run --push:agent 的总结经消息渠道发给我,后面附页面地址', () => {
|
|
202
|
+
hook.clear();
|
|
203
|
+
const r = s.rhythm(['run', 'daily-plan', '--push']);
|
|
204
|
+
assert.equal(r.code, 0, r.out);
|
|
205
|
+
// 假的 agent 把整段指令回出来,所以只看结尾
|
|
206
|
+
assert.match(r.stdout, /\n\n(已推送)\n$/);
|
|
207
|
+
const requests = hook.requests();
|
|
208
|
+
assert.equal(requests.length, 1);
|
|
209
|
+
const body = JSON.parse(requests[0].body);
|
|
210
|
+
assert.match(body.content, /回复:/);
|
|
211
|
+
assert.ok(body.content.includes(`→ 今天的文件:http://127.0.0.1:${port}/day/${TODAY()}`), body.content);
|
|
212
|
+
assert.deepEqual(body.notifyParams, [{ type: 'openIds', values: ['me-0001'] }]);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
test('run --push:quiet 里的任务不推送', () => {
|
|
216
|
+
hook.clear();
|
|
217
|
+
const r = s.rhythm(['run', 'linky-cost-fill', '--push']);
|
|
218
|
+
assert.equal(r.code, 0, r.out);
|
|
219
|
+
assert.ok(!r.stdout.includes('已推送'));
|
|
220
|
+
assert.deepEqual(hook.requests(), []);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
test('run --push:任务没跑完也告诉我原因', () => {
|
|
224
|
+
hook.clear();
|
|
225
|
+
const r = s.rhythm(['run', 'daily-plan', '--push'], { ...s.env, STUB_AGENT: 'fail' });
|
|
226
|
+
assert.equal(r.code, 5);
|
|
227
|
+
assert.match(hook.contents().at(-1), /「daily-plan」没跑完:退出码 1:agent 崩了/);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
test('网页:今日、遗留打得开;没有的日子、越出范围的地址、写请求都拒掉', async () => {
|
|
231
|
+
const base = `http://127.0.0.1:${port}`;
|
|
232
|
+
const home = await fetch(`${base}/`);
|
|
233
|
+
const html = await home.text();
|
|
234
|
+
assert.equal(home.status, 200);
|
|
235
|
+
assert.match(html, /写完导出接口文档/);
|
|
236
|
+
assert.match(html, /<a href="\/rhythm">规律<\/a>/);
|
|
237
|
+
assert.equal((await fetch(`${base}/carryover`)).status, 200);
|
|
238
|
+
assert.equal((await fetch(`${base}/day/1999-01-01`)).status, 404);
|
|
239
|
+
assert.equal((await fetch(`${base}/reports/..%2F..%2F00-config%2Fprofile.yaml`)).status, 404);
|
|
240
|
+
assert.equal((await fetch(`${base}/`, { method: 'POST' })).status, 405);
|
|
241
|
+
assert.equal((await fetch(`${base}/nope`)).status, 404);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
test('stop 之后 status 说没在跑', async () => {
|
|
245
|
+
const r = s.rhythm(['stop']);
|
|
246
|
+
assert.match(r.stdout, /已让它停下(pid \d+)/);
|
|
247
|
+
await proc.exited;
|
|
248
|
+
assert.match(s.rhythm(['status']).stdout, /^没在跑。/);
|
|
249
|
+
assert.equal(exists(`${s.box.work}/01-inbox/_rhythm/service.pid`), false);
|
|
250
|
+
assert.equal(s.rhythm(['stop']).stdout, '没在跑。\n');
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test('服务没在跑时照常推送,但不附打不开的地址', () => {
|
|
254
|
+
hook.clear();
|
|
255
|
+
s.rhythm(['run', 'daily-plan', '--push']);
|
|
256
|
+
const content = hook.contents().at(-1);
|
|
257
|
+
assert.match(content, /回复:/);
|
|
258
|
+
assert.ok(!content.includes('127.0.0.1'), content);
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
// ---------------------------------------------------------------- 采集
|
|
263
|
+
test('rhythm run sync:在同一个进程里跑一遍 sync', async (t) => {
|
|
264
|
+
const hook = await webhook(t);
|
|
265
|
+
const s = setup(t);
|
|
266
|
+
const ws = path.join(s.box.root, 'ws');
|
|
267
|
+
gitRepo(path.join(ws, 'demo'));
|
|
268
|
+
write(`${s.box.work}/collectors/local.env`, `GIT_ACTIVITY_WORKSPACES="${ws}"\nGIT_ACTIVITY_AUTHORS="selftest@example.com"\nFILE_ACTIVITY_WORKSPACES="${ws}"\n`);
|
|
269
|
+
const r = s.rhythm(['run', 'sync'], { ...s.env, STUB_ROBOT_URL: hook.url });
|
|
270
|
+
assert.equal(r.code, 0, r.out);
|
|
271
|
+
assert.match(r.stdout, /采集完成,用时 \d+ 秒。\n[\s\S]*ok git-activity[\s\S]*ok file-activity/);
|
|
272
|
+
assert.deepEqual(hook.requests(), []);
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
test('rhythm run sync:有源失败时退出码为 5,原因带出来', async (t) => {
|
|
276
|
+
const s = setup(t);
|
|
277
|
+
const file = `${s.box.work}/00-config/sources.yaml`;
|
|
278
|
+
write(file, `${read(file)}\n - id: nofile\n name: 自测用\n enabled: true\n collector: collectors/nope.sh\n`);
|
|
279
|
+
const r = s.rhythm(['run', 'sync']);
|
|
280
|
+
assert.equal(r.code, 5);
|
|
281
|
+
assert.match(r.stdout, /采集失败:退出码 1:[\s\S]*FAIL nofile(采集脚本不存在: collectors\/nope.sh)/);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
// ---------------------------------------------------------------- 调度
|
|
285
|
+
test('rhythm start:到点真的把任务交给 agent,总结推送给我,状态里记一笔', async (t) => {
|
|
286
|
+
const hook = await webhook(t);
|
|
287
|
+
const s = setup(t);
|
|
288
|
+
const now = new Date();
|
|
289
|
+
const day = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'][now.getDay()];
|
|
290
|
+
const at = (d) => `${pad(d.getHours())}:${pad(d.getMinutes())}`;
|
|
291
|
+
// 一个任务写在这一分钟,另一个写在下一分钟:刚好跨过整分钟也能点着
|
|
292
|
+
s.set('daily-plan', `"${day} ${at(now)}"`);
|
|
293
|
+
s.set('noon-checkin', `"${day} ${at(new Date(now.getTime() + 60000))}"`);
|
|
294
|
+
s.set('sync', '""');
|
|
295
|
+
s.set('quiet', '[]');
|
|
296
|
+
s.set('port', String(await freePort()));
|
|
297
|
+
const env = { ...s.env, STUB_ROBOT_URL: hook.url };
|
|
298
|
+
const proc = s.box.spawn(['rhythm', 'start', '--tick', '1'], { env });
|
|
299
|
+
t.after(async () => {
|
|
300
|
+
s.rhythm(['stop'], env);
|
|
301
|
+
await proc.exited;
|
|
302
|
+
});
|
|
303
|
+
assert.ok(await proc.waitFor('rc=0', 90000), proc.text());
|
|
304
|
+
const log = proc.text();
|
|
305
|
+
assert.match(log, /到点 (daily-plan|noon-checkin) \d\d:\d\d\n[\s\S]*开始跑 (daily-plan|noon-checkin) pid=\d+[\s\S]*跑完 (daily-plan|noon-checkin) rc=0/);
|
|
306
|
+
assert.ok(hook.contents().some((c) => c.includes('回复:')), '任务跑了却没推送');
|
|
307
|
+
const state = JSON.parse(read(`${s.box.work}/01-inbox/_rhythm/state.json`));
|
|
308
|
+
assert.ok(Object.values(state.runs).some((r) => r.ok === true), JSON.stringify(state.runs));
|
|
309
|
+
// 同一时间点只跑一次
|
|
310
|
+
await new Promise((resolve) => setTimeout(resolve, 1500));
|
|
311
|
+
assert.equal(Object.values(state.runs).length >= 1, true);
|
|
312
|
+
assert.ok(log.split('\n').filter((l) => l.includes('到点 ')).length <= 2, '同一个时间点跑了不止一次');
|
|
313
|
+
assert.match(read(`${s.box.work}/01-inbox/_rhythm.log`), /任务 (daily-plan|noon-checkin) 成功 用时 \d+s 已推送/);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
// ---------------------------------------------------------------- 用法
|
|
317
|
+
test('rhythm:不认识的子命令退出码为 2,--help 列出全部', (t) => {
|
|
318
|
+
const s = setup(t);
|
|
319
|
+
assert.equal(s.rhythm(['nope']).code, 2);
|
|
320
|
+
const r = s.box.run(['--help', 'rhythm']);
|
|
321
|
+
assert.equal(r.code, 0);
|
|
322
|
+
assert.match(r.stdout, /用法: work-rhythm rhythm <子命令>/);
|
|
323
|
+
for (const cmd of ['start', 'stop', 'status', 'due', 'run', 'serve', 'render', 'doctor', 'install', 'uninstall']) assert.match(r.stdout, new RegExp(`(?:\\n | \\| )${cmd}[ |\\[]`), `用法里没有 ${cmd}`);
|
|
324
|
+
});
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// 自测用的假命令行工具的源码。每个值是一段 node 程序,由 harness.stub(name, source) 写成可执行文件放进 PATH 最前面。
|
|
2
|
+
// 它们靠 STUB_* 环境变量决定行为,把调用记录写到 STUB_* 指的文件里
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
// 假的 yzj-cli:记录每次调用的时间;会话 ID 以 fail 结尾时模拟拉取失败;--limit 超过 20 和真的一样在发请求前就报参数错误
|
|
6
|
+
const yzjCli = `
|
|
7
|
+
const fs = require('node:fs');
|
|
8
|
+
const args = process.argv.slice(2);
|
|
9
|
+
const out = (s) => process.stdout.write(s + '\\n');
|
|
10
|
+
fs.appendFileSync(process.env.STUB_CALLS || '/dev/null', Math.floor(Date.now() / 1000) + '\\n');
|
|
11
|
+
const flags = {};
|
|
12
|
+
for (let i = 0; i < args.length; i += 1) if (args[i].startsWith('--')) flags[args[i]] = args[i + 1];
|
|
13
|
+
const head = args.slice(0, 2).join(' ');
|
|
14
|
+
if (head.startsWith('whoami')) {
|
|
15
|
+
if (process.env.STUB_WHOAMI === 'fail') { process.stderr.write('errorCode 10000400\\n'); process.exit(1); }
|
|
16
|
+
if ('--jq' in flags) out('me-0001');
|
|
17
|
+
else out(JSON.stringify({ data: { openId: 'me-0001' }, success: true }));
|
|
18
|
+
} else if (head === 'im message') {
|
|
19
|
+
const limit = Number(flags['--limit'] || 10);
|
|
20
|
+
if (limit > 20) { process.stderr.write(JSON.stringify({ error: { message: 'invalid value for --limit: must be between 1 and 20', type: 'validation' } }) + '\\n'); process.exit(2); }
|
|
21
|
+
const gid = flags['--group-id'] || '';
|
|
22
|
+
if (gid.endsWith('fail')) { process.stderr.write('errorCode 10000400\\n'); process.exit(1); }
|
|
23
|
+
const msg = (msgId, fromOpenId, content, sendTime) => ({ msgId, fromOpenId, content, sendTime });
|
|
24
|
+
if (flags['--type'] === 'old') {
|
|
25
|
+
if (flags['--msg-id'] === 'm2') {
|
|
26
|
+
out(JSON.stringify(gid.endsWith('deep')
|
|
27
|
+
? { success: true, data: { list: [msg('m1b', 'other-0002', 'still that day', '2026-11-27 08:30:00.000')], more: true } }
|
|
28
|
+
: { success: true, data: { list: [msg('m1', 'other-0002', 'earlier', '2026-11-26 23:50:00.000')], more: false } }));
|
|
29
|
+
} else out(JSON.stringify({ success: true, data: { list: [] } }));
|
|
30
|
+
} else {
|
|
31
|
+
out(JSON.stringify({ success: true, data: { list: [msg('m2', 'me-0001', 'hello', '2026-11-27 09:00:00.000'), msg('m3', 'other-0001', 'hi', '2026-11-27 09:30:00.000')], more: true } }));
|
|
32
|
+
}
|
|
33
|
+
} else if (head === 'approval flow') {
|
|
34
|
+
const page = flags['--page'] || '1';
|
|
35
|
+
if (process.env.STUB_YZJ_APPROVAL === 'failpage2' && page === '2') { process.stderr.write('errorCode 10000400\\n'); process.exit(1); }
|
|
36
|
+
const fresh = { appName: '智能审批', templateName: '云之家日常部署流程', createTime: Number(process.env.STUB_APPROVAL_NEW_MS), title: '不该留下的单据标题', creatorName: '不该留下的申请人' };
|
|
37
|
+
const old = { appName: '流程助手', templateName: '', createTime: Number(process.env.STUB_APPROVAL_OLD_MS), title: '不该留下的单据标题', creatorName: '不该留下的申请人' };
|
|
38
|
+
if (page === '1') out(JSON.stringify({ success: true, data: { count: 2, total: 3, list: [fresh, old] } }));
|
|
39
|
+
else out(JSON.stringify({ success: true, data: { count: 1, total: 3, list: [fresh] } }));
|
|
40
|
+
} else if (head.startsWith('calendar')) {
|
|
41
|
+
if (process.env.STUB_CALENDAR === 'fail') { process.stderr.write('errorCode 10000400\\n'); process.exit(1); }
|
|
42
|
+
out(JSON.stringify({ success: true, data: { list: [{ title: '需求评审' }] } }));
|
|
43
|
+
} else { process.stderr.write('stub yzj-cli: 不支持的命令\\n'); process.exit(2); }
|
|
44
|
+
`;
|
|
45
|
+
|
|
46
|
+
// 假的 linky:按 STUB_LINKY_MODE 模拟导出成功、不完整、未登录
|
|
47
|
+
const linky = `
|
|
48
|
+
const fs = require('node:fs');
|
|
49
|
+
const path = require('node:path');
|
|
50
|
+
const mode = process.env.STUB_LINKY_MODE || 'ok';
|
|
51
|
+
if (mode === 'loggedout') { process.stderr.write('error: token_invalid\\n'); process.exit(4); }
|
|
52
|
+
const out = process.argv[process.argv.indexOf('--out-dir') + 1];
|
|
53
|
+
fs.mkdirSync(out, { recursive: true });
|
|
54
|
+
const bug = [
|
|
55
|
+
{ id: 'i1', code: 'BG-2026-000101', title: '修复导出\\t换行', type: 'bug', status: { name: '待修复', category: 'initial' }, labels: { gf_priority: '高', tf_module: '导出(LJ0003-0016)' }, updated_at: '2026-11-27 10:00:00' },
|
|
56
|
+
{ id: 'i2', code: 'BG-2026-000102', title: '已经做完的缺陷', type: 'bug', status: { name: '已完成', category: 'done' }, labels: { gf_priority: '中' }, updated_at: '2026-11-27 11:00:00' },
|
|
57
|
+
];
|
|
58
|
+
const story = [
|
|
59
|
+
{ id: 'i3', code: 'US-2026-000201', title: '导出接口设计', type: 'user_story', status: { name: '处理中', category: 'in_progress' }, labels: { gf_priority: '紧急', tf_module: '会议(LJ0001-0009)' }, updated_at: '2026-11-28 08:00:00' },
|
|
60
|
+
];
|
|
61
|
+
fs.writeFileSync(path.join(out, 'bug.jsonl'), bug.map((o) => JSON.stringify(o)).join('\\n') + '\\n');
|
|
62
|
+
fs.writeFileSync(path.join(out, 'user_story.jsonl'), story.map((o) => JSON.stringify(o)).join('\\n') + '\\n');
|
|
63
|
+
const complete = mode !== 'incomplete';
|
|
64
|
+
fs.writeFileSync(path.join(out, 'meta.json'), JSON.stringify({ complete }));
|
|
65
|
+
process.stdout.write(JSON.stringify({ ok: true, complete }) + '\\n');
|
|
66
|
+
process.exit(mode === 'incomplete' ? 3 : 0);
|
|
67
|
+
`;
|
|
68
|
+
|
|
69
|
+
// 假的 security(钥匙串):STUB_KEYCHAIN=ok 时找得到,带 -w 就给出 STUB_ROBOT_URL 那个地址(通常是自测里假 webhook 的);否则和真的一样报找不到(退出码 44)
|
|
70
|
+
const security = `
|
|
71
|
+
if (process.env.STUB_KEYCHAIN === 'ok') {
|
|
72
|
+
if (process.argv.includes('-w')) process.stdout.write(process.env.STUB_ROBOT_URL + '\\n');
|
|
73
|
+
process.exit(0);
|
|
74
|
+
}
|
|
75
|
+
process.stderr.write('security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.\\n');
|
|
76
|
+
process.exit(44);
|
|
77
|
+
`;
|
|
78
|
+
|
|
79
|
+
// 假的本地 agent(YZJ_BOT_AGENT 写一条自定义命令那条路):记下被调用,把消息原样回出来。
|
|
80
|
+
// STUB_AGENT=slow 睡 5 秒,fail 退出 1,empty 什么都不输出,long 回 300 个字
|
|
81
|
+
const fakeAgent = `
|
|
82
|
+
const fs = require('node:fs');
|
|
83
|
+
fs.appendFileSync(process.env.STUB_AGENT_CALLS || '/dev/null', 'called\\n');
|
|
84
|
+
const mode = process.env.STUB_AGENT || 'ok';
|
|
85
|
+
const finish = (text) => { process.stdout.write(text + '\\n'); };
|
|
86
|
+
if (mode === 'slow') setTimeout(() => finish('回复:' + process.argv[2]), 5000);
|
|
87
|
+
else if (mode === 'fail') { process.stderr.write('agent 崩了\\n'); process.exit(1); }
|
|
88
|
+
else if (mode === 'empty') process.exit(0);
|
|
89
|
+
else if (mode === 'long') finish('长'.repeat(300));
|
|
90
|
+
else finish('回复:' + process.argv[2]);
|
|
91
|
+
`;
|
|
92
|
+
|
|
93
|
+
// 假的 ZCode 命令行(YZJ_BOT_AGENT=zcode 那条路):把这次的参数记成一行,照真 --json 的样子回一个带 sessionId 的 JSON
|
|
94
|
+
const fakeZcode = `
|
|
95
|
+
const fs = require('node:fs');
|
|
96
|
+
const args = process.argv.slice(2);
|
|
97
|
+
fs.writeFileSync(process.env.STUB_ZCODE_ARGS || '/dev/null', args.join(' ') + '\\n');
|
|
98
|
+
process.stdout.write(JSON.stringify({ sessionId: 'sess_fake', response: '回复:' + args[args.length - 1], usage: {} }) + '\\n');
|
|
99
|
+
`;
|
|
100
|
+
|
|
101
|
+
// 假的 DSH 自带 Node:记下调用时的环境,丢掉「--import <路径>」两个参数,用真正的 node 把剩下的交给入口脚本
|
|
102
|
+
const fakeDshNode = `
|
|
103
|
+
const fs = require('node:fs');
|
|
104
|
+
const { spawnSync } = require('node:child_process');
|
|
105
|
+
const args = process.argv.slice(2);
|
|
106
|
+
fs.writeFileSync(process.env.STUB_DSH_ENV || '/dev/null', 'run_as_node=' + (process.env.ELECTRON_RUN_AS_NODE || '') + ' dsh_home=' + (process.env.DSH_HOME || '') + '\\n');
|
|
107
|
+
const r = spawnSync(process.execPath, [args[2], ...args.slice(3)], { stdio: 'inherit' });
|
|
108
|
+
process.exit(r.status === null ? 1 : r.status);
|
|
109
|
+
`;
|
|
110
|
+
|
|
111
|
+
// 假的 mdfind(Spotlight):记下查询串,按 STUB_MDFIND 列出路径。STUB_MDFIND=empty 时什么都不返回(目录没被索引的那种)
|
|
112
|
+
const mdfind = `
|
|
113
|
+
const fs = require('node:fs');
|
|
114
|
+
const args = process.argv.slice(2);
|
|
115
|
+
fs.writeFileSync((process.env.STUB_MDFIND_LOG || '/dev/null') + '.query', args.join('\\n') + '\\n');
|
|
116
|
+
const mode = process.env.STUB_MDFIND || 'ok';
|
|
117
|
+
if (mode === 'empty') process.exit(0);
|
|
118
|
+
const since = /\\$time\\.iso\\((\\d{4}-\\d{2}-\\d{2})\\)/.exec(args[args.length - 1]);
|
|
119
|
+
if (!since) process.exit(1); // 查询串里没有 $time.iso(日期):真的 mdfind 对这种查询什么都查不到
|
|
120
|
+
process.stdout.write((process.env.STUB_MDFIND_PATHS || '').split('\\n').filter(Boolean).join('\\n') + '\\n');
|
|
121
|
+
`;
|
|
122
|
+
|
|
123
|
+
// 假的 mdls:STUB_MDLS=null 时回 (null)(目录没被索引),否则回一个日期
|
|
124
|
+
const mdls = `
|
|
125
|
+
process.stdout.write((process.env.STUB_MDLS || '2026-11-30 00:00:00 +0000') + '\\n');
|
|
126
|
+
`;
|
|
127
|
+
|
|
128
|
+
// 假的 DSH 命令行(YZJ_BOT_AGENT=dsh 那条路):记下参数和 --patch 指的覆盖文件里有哪些条目,
|
|
129
|
+
// 照真 --json 的样子回 session 和 final 两个事件。STUB_DSH_FINAL 不为空时,final 的结束原因用它
|
|
130
|
+
const fakeDsh = `
|
|
131
|
+
const fs = require('node:fs');
|
|
132
|
+
const args = process.argv.slice(2);
|
|
133
|
+
const patch = args[args.indexOf('--patch') + 1];
|
|
134
|
+
fs.writeFileSync(process.env.STUB_DSH_ARGS || '/dev/null', args.join(' ') + '\\n');
|
|
135
|
+
fs.writeFileSync((process.env.STUB_DSH_ARGS || '/dev/null') + '.patch', (fs.readFileSync(patch, 'utf8').match(/^- id: |^ disabled:/gm) || []).join('\\n') + '\\n');
|
|
136
|
+
process.stdout.write(JSON.stringify({ type: 'session', sessionId: 'session_fake', cwd: 'x' }) + '\\n');
|
|
137
|
+
process.stdout.write(JSON.stringify({ type: 'final', sessionId: 'session_fake', reason: { kind: process.env.STUB_DSH_FINAL || 'completed' }, text: '回复:' + args[args.length - 1] }) + '\\n');
|
|
138
|
+
`;
|
|
139
|
+
|
|
140
|
+
module.exports = { yzjCli, linky, security, fakeAgent, fakeZcode, fakeDshNode, fakeDsh, mdfind, mdls };
|