work-rhythm 0.0.0-stage → 0.1.1
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 +86 -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 +35 -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 +46 -0
- package/.agents/notes/decisions/2026-12-01-repo-is-not-a-workdir-templates-hold-workdir-files.md +29 -0
- package/.agents/notes/decisions/2026-12-01-repo-stays-generic-personal-data-lives-in-workdirs.md +32 -0
- package/.agents/notes/decisions/2026-12-01-workdir-is-not-a-git-repository.md +25 -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 +278 -0
- package/AGENTS.md +19 -0
- package/README.md +69 -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 +35 -4
- package/scripts/cli.js +101 -0
- package/scripts/cmd/check.js +330 -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 +49 -0
- package/scripts/cmd/listen.js +91 -0
- package/scripts/cmd/notify.js +42 -0
- package/scripts/cmd/pack.js +104 -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 +149 -0
- package/scripts/lib/workdir.js +98 -0
- package/scripts/test/check.test.js +279 -0
- package/scripts/test/collectors.test.js +574 -0
- package/scripts/test/config.test.js +118 -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 +277 -0
- package/scripts/test/init.test.js +123 -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 +333 -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/00-config/profile.example.yaml +54 -0
- package/templates/00-config/sources.yaml +115 -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
- package/templates/AGENTS.md +31 -0
- package/templates/README.md +73 -0
|
@@ -0,0 +1,123 @@
|
|
|
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 { PACKAGE_ROOT, makeSandbox, cleanSources, git, GIT_ENV, gitRepo, run: rawRun, read, exists, write } = require('./harness');
|
|
7
|
+
|
|
8
|
+
const TODAY = () => {
|
|
9
|
+
const d = new Date();
|
|
10
|
+
const p = (n) => String(n).padStart(2, '0');
|
|
11
|
+
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())}`;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// 新工作目录里该有什么:配置、文档、任务指令、记录、笔记和 skill;不该有脚本和内置采集器
|
|
15
|
+
const EXPECTED = [
|
|
16
|
+
'00-config/profile.yaml', '00-config/profile.example.yaml', '00-config/sources.yaml', 'AGENTS.md', 'README.md', 'CLAUDE.md',
|
|
17
|
+
'docs/formats.md', 'docs/channels.md', 'tasks/daily-plan.md', 'collectors/local.env.example',
|
|
18
|
+
'03-carryover/CARRYOVER.md', '03-carryover/DECISIONS.md', '04-patterns/WORK_RHYTHM.md',
|
|
19
|
+
'.agents/skills/work-rhythm/SKILL.md', '.agents/notes/AGENTS.md', '02-daily', '05-goals',
|
|
20
|
+
];
|
|
21
|
+
// 工作目录不是 git 仓库:没有 .git、.gitignore、.gitkeep
|
|
22
|
+
const NOT_EXPECTED = ['scripts', 'templates', 'collectors/git-activity.js', 'collectors/im-yzj.sh', '01-inbox', 'collectors/local.env', '.git', '.gitignore', '02-daily/.gitkeep'];
|
|
23
|
+
|
|
24
|
+
test('init:用包里的模板建出工作目录并登记;不 git init,不是 git 仓库', (t) => {
|
|
25
|
+
const box = makeSandbox(t);
|
|
26
|
+
const cfg = path.join(box.root, 'wr-config.yaml');
|
|
27
|
+
const target = path.join(box.root, 'home', 'new-wd');
|
|
28
|
+
const r = rawRun(['init', target], { cwd: box.root, env: { WORK_RHYTHM_CONFIG: cfg } });
|
|
29
|
+
assert.equal(r.code, 0, r.out);
|
|
30
|
+
const real = fs.realpathSync(target);
|
|
31
|
+
assert.equal(r.stdout, `WORKDIR=${real}\n`);
|
|
32
|
+
assert.match(r.stderr, /init: 已新建 .*new-wd:个人信息是空白的、数据源全部关着、没有记录\n下一步:.*「开始引导」/);
|
|
33
|
+
for (const f of EXPECTED) assert.ok(exists(path.join(target, f)), `缺 ${f}`);
|
|
34
|
+
for (const f of NOT_EXPECTED) assert.ok(!exists(path.join(target, f)), `不该有 ${f}`);
|
|
35
|
+
assert.match(read(`${target}/00-config/profile.yaml`), /^ status: new\b/m);
|
|
36
|
+
assert.equal(read(`${target}/00-config/profile.yaml`), read(`${target}/00-config/profile.example.yaml`));
|
|
37
|
+
assert.ok(!/enabled:\s*true/.test(read(`${target}/00-config/sources.yaml`)), '数据源要全部关着');
|
|
38
|
+
assert.match(read(`${target}/03-carryover/DECISIONS.md`), /^\| 日期 \| 事项/m);
|
|
39
|
+
assert.ok(!/^\| \d/m.test(read(`${target}/03-carryover/DECISIONS.md`)), '模板里不带记录');
|
|
40
|
+
assert.ok(fs.lstatSync(`${target}/CLAUDE.md`).isSymbolicLink(), 'CLAUDE.md 是指向 AGENTS.md 的软链');
|
|
41
|
+
assert.equal(read(cfg), `workdir: ${real}\n`);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('init:相对路径按当前目录算,上级目录没有就一起建;目录已经存在、参数不对时退出码为 2', (t) => {
|
|
45
|
+
const box = makeSandbox(t);
|
|
46
|
+
const cfg = path.join(box.root, 'wr-config.yaml');
|
|
47
|
+
const env = { WORK_RHYTHM_CONFIG: cfg };
|
|
48
|
+
const r = rawRun(['init', './my-work'], { cwd: box.root, env });
|
|
49
|
+
assert.equal(r.code, 0, r.out);
|
|
50
|
+
assert.equal(r.stdout, `WORKDIR=${fs.realpathSync(path.join(box.root, 'my-work'))}\n`);
|
|
51
|
+
const deep = rawRun(['init', path.join(box.root, 'no', 'such', 'dir', 'x')], { cwd: box.root, env });
|
|
52
|
+
assert.equal(deep.code, 0, deep.out);
|
|
53
|
+
assert.ok(exists(path.join(box.root, 'no', 'such', 'dir', 'x', '00-config', 'profile.yaml')));
|
|
54
|
+
const again = rawRun(['init', path.join(box.root, 'my-work')], { cwd: box.root, env });
|
|
55
|
+
assert.equal(again.code, 2);
|
|
56
|
+
assert.match(again.stderr, /已经存在。拿它当工作目录用 work-rhythm workdir register;要新建就换个位置/);
|
|
57
|
+
assert.equal(rawRun(['init'], { cwd: box.root, env }).code, 2);
|
|
58
|
+
assert.equal(rawRun(['init', 'a', 'b'], { cwd: box.root, env }).code, 2);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('init:建出来的工作目录里 work-rhythm 的命令都能用:check、today、sync', (t) => {
|
|
62
|
+
const box = makeSandbox(t);
|
|
63
|
+
const cfg = path.join(box.root, 'wr-config.yaml');
|
|
64
|
+
const target = path.join(box.root, 'home', 'demo');
|
|
65
|
+
assert.equal(rawRun(['init', target], { cwd: box.root, env: { WORK_RHYTHM_CONFIG: cfg } }).code, 0);
|
|
66
|
+
// 站在里面,不用 --workdir:往上找得到,也不靠登记
|
|
67
|
+
const chk = rawRun(['check'], { cwd: target, env: { WORK_RHYTHM_CONFIG: path.join(box.root, 'none.yaml') } });
|
|
68
|
+
assert.equal(chk.code, 0, chk.out);
|
|
69
|
+
assert.match(chk.stdout, /== 1\/6 脚本语法 ==\n[\s\S]*== 6\/6 笔记格式 ==\n全部通过\n/);
|
|
70
|
+
assert.equal(rawRun(['today'], { cwd: target }).stdout, `DATE=${TODAY()}\nWEEKDAY=${['日', '一', '二', '三', '四', '五', '六'][new Date().getDay()]}\nDAILY_FILE=02-daily/${TODAY().slice(0, 7)}/${TODAY()}.md\nIS_WORKDAY=1\n`);
|
|
71
|
+
// 采集:内置采集器在包里,工作目录里没有它们也能跑
|
|
72
|
+
const ws = path.join(box.root, 'ws');
|
|
73
|
+
gitRepo(path.join(ws, 'demo'));
|
|
74
|
+
write(path.join(target, '00-config', 'sources.yaml'), cleanSources());
|
|
75
|
+
write(path.join(target, 'collectors', 'local.env'), `GIT_ACTIVITY_WORKSPACES="${ws}"\nGIT_ACTIVITY_AUTHORS="selftest@example.com"\nFILE_ACTIVITY_WORKSPACES="${ws}"\n`);
|
|
76
|
+
const sync = rawRun(['sync'], { cwd: target, env: { ...box.stubs() } });
|
|
77
|
+
assert.equal(sync.code, 0, sync.out);
|
|
78
|
+
assert.match(sync.stdout, /ok git-activity\nok file-activity\n/);
|
|
79
|
+
assert.ok(exists(path.join(target, '01-inbox', 'git-activity', TODAY(), 'commits.tsv')));
|
|
80
|
+
assert.equal(rawRun(['goals', 'list'], { cwd: target }).code, 0);
|
|
81
|
+
assert.equal(rawRun(['decisions', 'list'], { cwd: target }).code, 0);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// ---------------------------------------------------------------- 模板本身
|
|
85
|
+
const { writeWorkdir, blankProblems, blankText } = require('../lib/template');
|
|
86
|
+
|
|
87
|
+
test('template:包不完整时说清缺哪份;空白核对认得出带记录、带配置、没关源的目录', (t) => {
|
|
88
|
+
const box = makeSandbox(t);
|
|
89
|
+
write(path.join(box.root, 'pkg', 'docs', 'a.md'), 'x');
|
|
90
|
+
assert.throws(() => writeWorkdir(path.join(box.root, 'w'), path.join(box.root, 'pkg')), /这个包不完整:templates\/00-config\/profile\.example\.yaml 不存在;[\s\S]*重新安装一次 work-rhythm/);
|
|
91
|
+
// 真铺出来的是空白的(夹具为了跑采集会打开两个本机源,所以这里自己铺一份)
|
|
92
|
+
const dir = path.join(box.root, 'fresh');
|
|
93
|
+
fs.mkdirSync(dir);
|
|
94
|
+
assert.deepEqual(writeWorkdir(dir, PACKAGE_ROOT), []);
|
|
95
|
+
assert.deepEqual(fs.readdirSync(path.join(dir, '02-daily')), [], '02-daily 是空目录,不用 .gitkeep 占位');
|
|
96
|
+
assert.deepEqual(blankProblems(dir), []);
|
|
97
|
+
write(`${dir}/02-daily/2026-01/2026-01-01.md`, '# 2026-01-01\n');
|
|
98
|
+
write(`${dir}/05-goals/x.md`, '---\nid: x\n---\n');
|
|
99
|
+
write(`${dir}/collectors/local.env`, 'A=1\n');
|
|
100
|
+
write(`${dir}/01-inbox/x`, '');
|
|
101
|
+
fs.appendFileSync(`${dir}/03-carryover/CARRYOVER.md`, '| 2026-01-01 | 旧 | 复盘 | P1 | 待开始 | 0 | |\n');
|
|
102
|
+
write(`${dir}/00-config/sources.yaml`, read(`${dir}/00-config/sources.yaml`).replace(/^(\s*enabled:\s*)false/gm, '$1true'));
|
|
103
|
+
write(`${dir}/00-config/profile.yaml`, read(`${dir}/00-config/profile.yaml`).replace('status: new', 'status: done'));
|
|
104
|
+
assert.deepEqual(blankProblems(dir), [
|
|
105
|
+
'profile.yaml 不是空白状态(setup.status 应为 new)',
|
|
106
|
+
'数据源没有全部关掉',
|
|
107
|
+
'02-daily 里带了 1 个记录文件',
|
|
108
|
+
'05-goals 里带了 1 个记录文件',
|
|
109
|
+
'03-carryover/CARRYOVER.md 里带了记录',
|
|
110
|
+
'带了不该带的 collectors/local.env',
|
|
111
|
+
'带了不该带的 01-inbox',
|
|
112
|
+
]);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test('template:空白化的几条规则', () => {
|
|
116
|
+
assert.equal(blankText('03-carryover/DECISIONS.md', '# t\n\n| 日期 |\n|---|---|\n| 2026-01-01 | a |\n| 01-01 | b |\n'), '# t\n\n| 日期 |\n|---|---|\n');
|
|
117
|
+
assert.equal(blankText('00-config/sources.yaml', 'a:\n enabled: true # 开\n enabled: false\n enabled: true\n'), 'a:\n enabled: false\n enabled: false\n enabled: false\n');
|
|
118
|
+
const rhythm = '# 我的规律\n\n> 说明\n\n数据范围:近 30 天(12 个工作日)\n\n## 时间节律\n\n- 早上写代码\n- 下午开会\n\n## 固定承诺\n\n(x)\n\n## 对计划的含义\n\n内容\n';
|
|
119
|
+
assert.equal(blankText('04-patterns/WORK_RHYTHM.md', rhythm), '# 我的规律\n\n> 说明\n\n数据范围:尚未开始(0 个带事件的工作日)\n\n## 时间节律\n\n## 固定承诺\n\n## 对计划的含义\n');
|
|
120
|
+
// 已经是空白的,原样不变
|
|
121
|
+
const blank = read(path.join(PACKAGE_ROOT, 'templates', '04-patterns', 'WORK_RHYTHM.md'));
|
|
122
|
+
assert.equal(blankText('04-patterns/WORK_RHYTHM.md', blank), blank);
|
|
123
|
+
});
|
|
@@ -0,0 +1,362 @@
|
|
|
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, webhook, read, write, setKey, exists } = require('./harness');
|
|
7
|
+
|
|
8
|
+
const AGENT_CALLS = 'agent.calls';
|
|
9
|
+
// 搭工作目录:装上一套假工具,消息渠道是 yzj-robot,钥匙串的假地址指向假 webhook。
|
|
10
|
+
// agent 是一条自定义命令(假的 fake-agent),回复是「回复:<消息>」
|
|
11
|
+
function setup(t) {
|
|
12
|
+
const box = makeSandbox(t);
|
|
13
|
+
box.workdir();
|
|
14
|
+
const stubs = box.stubs();
|
|
15
|
+
const file = `${box.work}/00-config/profile.yaml`;
|
|
16
|
+
write(file, setKey(read(file), 'notify', '"yzj-robot"'));
|
|
17
|
+
// 钥匙串里有地址(不推送的用例也要过配置检查,地址是个用不到的假地址:1 端口,fetch 直接拒绝,不会真的往外发),
|
|
18
|
+
// 要推送的用例再换成假 webhook 的
|
|
19
|
+
const hookEnv = { STUB_KEYCHAIN: 'ok', STUB_ROBOT_URL: 'http://127.0.0.1:1/gateway/robot/webhook/send?yzjtype=0&yzjtoken=stubtoken0123456789abcd' };
|
|
20
|
+
const bot = { ...stubs, ...hookEnv, YZJ_MY_OPEN_ID: 'me-0001', YZJ_BOT_AGENT: path.join(box.bin, 'fake-agent') };
|
|
21
|
+
return {
|
|
22
|
+
box,
|
|
23
|
+
stubs,
|
|
24
|
+
bot,
|
|
25
|
+
// 换掉 profile 里的 notify(值自己带引号)
|
|
26
|
+
setNotify: (value) => write(file, setKey(read(file), 'notify', value)),
|
|
27
|
+
listen: (args, env = {}) => box.run(['listen', ...args], { env }),
|
|
28
|
+
agentCalls: () => read(path.join(box.root, AGENT_CALLS)).split('\n').filter(Boolean).length,
|
|
29
|
+
botLog: () => read(`${box.work}/01-inbox/_bot.log`),
|
|
30
|
+
session: () => read(`${box.work}/01-inbox/_bot/session-id`).trim(),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// 一帧业务消息。外层结构和 2026-11-30 实测的一致
|
|
35
|
+
function frame(openId, msgId, content) {
|
|
36
|
+
return JSON.stringify({
|
|
37
|
+
msg: { eid: '10109', groupType: 3, msgType: 2, robotName: '工作王', robotId: 'BOT-bot', groupId: 'BOT-me-0001-BOT-bot', msgId, type: 2, operatorName: '自测', content, operatorOpenid: openId, time: 1764510000000 },
|
|
38
|
+
level: 0,
|
|
39
|
+
cmd: 'directPush',
|
|
40
|
+
type: 'robotMessage',
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const withHook = async (t, s) => {
|
|
45
|
+
const hook = await webhook(t);
|
|
46
|
+
return { hook, env: { ...s.bot, STUB_ROBOT_URL: hook.url } };
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// ---------------------------------------------------------------- 自定义命令
|
|
50
|
+
test('listen handle:把消息交给 YZJ_BOT_AGENT 指的命令,回复原样打印,不发消息', async (t) => {
|
|
51
|
+
const s = setup(t);
|
|
52
|
+
const { hook, env } = await withHook(t, s);
|
|
53
|
+
write(path.join(s.box.root, AGENT_CALLS), '');
|
|
54
|
+
const r = s.listen(['handle', '你好'], env);
|
|
55
|
+
assert.equal(r.code, 0, r.out);
|
|
56
|
+
assert.equal(r.stdout, '回复:你好\n');
|
|
57
|
+
assert.deepEqual(hook.requests(), []);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('listen handle-frame:本人的消息交给 agent,回复经消息渠道发出', async (t) => {
|
|
61
|
+
const s = setup(t);
|
|
62
|
+
const { hook, env } = await withHook(t, s);
|
|
63
|
+
s.listen(['handle', '你好'], env);
|
|
64
|
+
const r = s.listen(['handle-frame', frame('me-0001', 'm1', '记一下:刚开完会')], env);
|
|
65
|
+
assert.equal(r.code, 0, r.out);
|
|
66
|
+
assert.match(r.stdout, /处理 msgId=m1/);
|
|
67
|
+
assert.equal(hook.contents().at(-1), '回复:记一下:刚开完会');
|
|
68
|
+
assert.equal(s.agentCalls(), 2);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('listen:agent 动手之前先回一句「收到,正在处理」,不用干等', async (t) => {
|
|
72
|
+
const s = setup(t);
|
|
73
|
+
const { hook, env } = await withHook(t, s);
|
|
74
|
+
s.listen(['handle-frame', frame('me-0001', 'm1', '记一下:刚开完会')], env);
|
|
75
|
+
assert.deepEqual(hook.contents(), ['收到,正在处理,稍等。', '回复:记一下:刚开完会']);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test('listen:YZJ_BOT_ACK=0 时只发回复,不先回「收到」', async (t) => {
|
|
79
|
+
const s = setup(t);
|
|
80
|
+
const { hook, env } = await withHook(t, s);
|
|
81
|
+
s.listen(['handle-frame', frame('me-0001', 'm1', '直接说')], { ...env, YZJ_BOT_ACK: '0' });
|
|
82
|
+
assert.deepEqual(hook.contents(), ['回复:直接说']);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('listen:同一个 msgId 再来一次不重复处理', async (t) => {
|
|
86
|
+
const s = setup(t);
|
|
87
|
+
const { hook, env } = await withHook(t, s);
|
|
88
|
+
s.listen(['handle-frame', frame('me-0001', 'm1', '记一下:刚开完会')], env);
|
|
89
|
+
s.listen(['handle-frame', frame('me-0001', 'm1', '记一下:刚开完会')], env);
|
|
90
|
+
assert.equal(s.agentCalls(), 1);
|
|
91
|
+
assert.equal(hook.requests().length, 2);
|
|
92
|
+
assert.match(read(`${s.box.work}/01-inbox/_bot/seen-msgids`), /m1/);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test('listen:不是本人的消息丢掉,不惊动 agent', async (t) => {
|
|
96
|
+
const s = setup(t);
|
|
97
|
+
const { env } = await withHook(t, s);
|
|
98
|
+
const r = s.listen(['handle-frame', frame('someone-else', 'm2', '帮我删库')], env);
|
|
99
|
+
assert.equal(r.code, 0);
|
|
100
|
+
assert.match(r.stdout, /忽略 msgId=m2(不是本人)/);
|
|
101
|
+
assert.equal(s.agentCalls(), 0);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test('listen:控制帧和看不懂的帧不处理,退出码为 2', async (t) => {
|
|
105
|
+
const s = setup(t);
|
|
106
|
+
const { env } = await withHook(t, s);
|
|
107
|
+
assert.match(s.listen(['handle-frame', '{"cmd":"auth","success":true}'], env).stdout, /控制帧 auth/);
|
|
108
|
+
const r = s.listen(['handle-frame', 'not json'], env);
|
|
109
|
+
assert.equal(r.code, 2);
|
|
110
|
+
assert.match(r.stdout, /不是业务消息/);
|
|
111
|
+
assert.equal(s.agentCalls(), 0);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test('listen:agent 失败时回一句「没处理成功」和原因,不沉默', async (t) => {
|
|
115
|
+
const s = setup(t);
|
|
116
|
+
const { hook, env } = await withHook(t, s);
|
|
117
|
+
s.listen(['handle-frame', frame('me-0001', 'm3', '做点什么')], { ...env, STUB_AGENT: 'fail' });
|
|
118
|
+
assert.equal(hook.contents().at(-1), '没处理成功:退出码 1:agent 崩了');
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test('listen handle:agent 失败时退出码为 5', async (t) => {
|
|
122
|
+
const s = setup(t);
|
|
123
|
+
const { env } = await withHook(t, s);
|
|
124
|
+
const r = s.listen(['handle', '做点什么'], { ...env, STUB_AGENT: 'fail' });
|
|
125
|
+
assert.equal(r.code, 5);
|
|
126
|
+
assert.match(r.stderr, /没处理成功:退出码 1:agent 崩了/);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test('listen:agent 超过 YZJ_BOT_TIMEOUT 没返回时按失败处理', async (t) => {
|
|
130
|
+
const s = setup(t);
|
|
131
|
+
const { hook, env } = await withHook(t, s);
|
|
132
|
+
s.listen(['handle-frame', frame('me-0001', 'm4', '慢一点')], { ...env, STUB_AGENT: 'slow', YZJ_BOT_TIMEOUT: '1' });
|
|
133
|
+
assert.equal(hook.contents().at(-1), '没处理成功:超过 1 秒没返回');
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('listen:agent 什么都没输出时按失败处理', async (t) => {
|
|
137
|
+
const s = setup(t);
|
|
138
|
+
const { hook, env } = await withHook(t, s);
|
|
139
|
+
s.listen(['handle-frame', frame('me-0001', 'm5', '说点什么')], { ...env, STUB_AGENT: 'empty' });
|
|
140
|
+
assert.equal(hook.contents().at(-1), '没处理成功:它没有输出任何回复');
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test('listen:回复超过 YZJ_BOT_REPLY_MAX 时截到上限并注明', async (t) => {
|
|
144
|
+
const s = setup(t);
|
|
145
|
+
const { hook, env } = await withHook(t, s);
|
|
146
|
+
s.listen(['handle-frame', frame('me-0001', 'm6', '长篇大论')], { ...env, STUB_AGENT: 'long', YZJ_BOT_REPLY_MAX: '10' });
|
|
147
|
+
const content = hook.contents().at(-1);
|
|
148
|
+
assert.equal(Array.from(content.split('\n')[0]).length, 10);
|
|
149
|
+
assert.match(content, /截到 10 字/);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test('listen:/new 不惊动 agent,回一句已开新会话', async (t) => {
|
|
153
|
+
const s = setup(t);
|
|
154
|
+
const { hook, env } = await withHook(t, s);
|
|
155
|
+
s.listen(['handle-frame', frame('me-0001', 'm7', '/new')], env);
|
|
156
|
+
assert.equal(hook.contents().at(-1), '已开新会话。');
|
|
157
|
+
assert.equal(s.agentCalls(), 0);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test('listen:回复发不出去时照样处理完,说一声没发出去', async (t) => {
|
|
161
|
+
const s = setup(t);
|
|
162
|
+
const hook = await webhook(t, { mode: 'http' });
|
|
163
|
+
const env = { ...s.bot, STUB_ROBOT_URL: hook.url };
|
|
164
|
+
const r = s.listen(['handle-frame', frame('me-0001', 'm1', '发不出去')], env);
|
|
165
|
+
assert.equal(r.code, 0, r.out);
|
|
166
|
+
assert.match(r.stdout, /处理 msgId=m1,但回复没发出去/);
|
|
167
|
+
assert.equal(s.agentCalls(), 1);
|
|
168
|
+
assert.match(s.botLog(), /回复没发出去/);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test('listen:日志里不记消息内容', async (t) => {
|
|
172
|
+
const s = setup(t);
|
|
173
|
+
const { env } = await withHook(t, s);
|
|
174
|
+
s.listen(['handle-frame', frame('me-0001', 'm10', '一句不该进日志的话')], env);
|
|
175
|
+
assert.match(s.botLog(), /msg 接受 msgId=m10/);
|
|
176
|
+
assert.ok(!s.botLog().includes('一句不该进日志的话'));
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
// ---------------------------------------------------------------- 配置
|
|
180
|
+
test('listen:没配 YZJ_BOT_AGENT 时退出码为 3', async (t) => {
|
|
181
|
+
const s = setup(t);
|
|
182
|
+
const { env } = await withHook(t, s);
|
|
183
|
+
const r = s.listen(['handle', 'x'], { ...env, YZJ_BOT_AGENT: '' });
|
|
184
|
+
assert.equal(r.code, 3);
|
|
185
|
+
assert.match(r.stderr, /没配 YZJ_BOT_AGENT/);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test('listen:没配 YZJ_MY_OPEN_ID 时退出码为 3', async (t) => {
|
|
189
|
+
const s = setup(t);
|
|
190
|
+
const { env } = await withHook(t, s);
|
|
191
|
+
const r = s.listen(['handle', 'x'], { ...env, YZJ_MY_OPEN_ID: '' });
|
|
192
|
+
assert.equal(r.code, 3);
|
|
193
|
+
assert.match(r.stderr, /写 YZJ_MY_OPEN_ID/);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test('listen:preferences.notify 不是 yzj-robot 时退出码为 3', async (t) => {
|
|
197
|
+
const s = setup(t);
|
|
198
|
+
const { env } = await withHook(t, s);
|
|
199
|
+
s.setNotify('""');
|
|
200
|
+
const r = s.listen(['handle', 'x'], env);
|
|
201
|
+
assert.equal(r.code, 3);
|
|
202
|
+
assert.match(r.stderr, /值守是 yzj-robot 渠道的收消息一半/);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
test('listen:钥匙串里没有地址时退出码为 3', async (t) => {
|
|
206
|
+
const s = setup(t);
|
|
207
|
+
const { env } = await withHook(t, s);
|
|
208
|
+
const r = s.listen(['handle', 'x'], { ...env, STUB_KEYCHAIN: 'no' });
|
|
209
|
+
assert.equal(r.code, 3);
|
|
210
|
+
assert.match(r.stderr, /钥匙串里没有 work-rhythm-yzj-robot-webhook/);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
test('listen:消息不带外层、直接是业务消息的写法也认', async (t) => {
|
|
214
|
+
const s = setup(t);
|
|
215
|
+
const { hook, env } = await withHook(t, s);
|
|
216
|
+
const r = s.listen(['handle-frame', JSON.stringify({ type: 2, robotId: 'BOT-bot', robotName: '工作王', operatorOpenid: 'me-0001', operatorName: '自测', time: 1764510000000, msgId: 'm9', content: '不带外层的写法', groupType: 3 })], env);
|
|
217
|
+
assert.match(r.stdout, /处理 msgId=m9/);
|
|
218
|
+
assert.equal(hook.contents().at(-1), '回复:不带外层的写法');
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
test('listen:配置写在 collectors/local.env 里也读得到', async (t) => {
|
|
222
|
+
const s = setup(t);
|
|
223
|
+
const { env } = await withHook(t, s);
|
|
224
|
+
write(`${s.box.work}/collectors/local.env`, `YZJ_MY_OPEN_ID="me-0001"\nYZJ_BOT_AGENT="${path.join(s.box.bin, 'fake-agent')}"\n`);
|
|
225
|
+
// 命令行上的同名变量给了别的值:local.env 里的为准(和原来 source 的效果一样)
|
|
226
|
+
const r = s.listen(['handle', '写在配置里'], { ...env, YZJ_BOT_AGENT: '/nonexistent-from-env' });
|
|
227
|
+
assert.equal(r.code, 0, r.out);
|
|
228
|
+
assert.equal(r.stdout, '回复:写在配置里\n');
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test('listen doctor:问题逐条报出来,退出码为 1;配好了什么都不说', async (t) => {
|
|
232
|
+
const s = setup(t);
|
|
233
|
+
const { env } = await withHook(t, s);
|
|
234
|
+
s.setNotify('""');
|
|
235
|
+
const bad = s.listen(['doctor'], { ...env, STUB_KEYCHAIN: 'no', YZJ_MY_OPEN_ID: '', YZJ_BOT_AGENT: '/nonexistent/agent' });
|
|
236
|
+
assert.equal(bad.code, 1);
|
|
237
|
+
assert.match(bad.stdout, /值守属于 yzj-robot 渠道/);
|
|
238
|
+
assert.match(bad.stdout, /钥匙串里没有 work-rhythm-yzj-robot-webhook/);
|
|
239
|
+
assert.match(bad.stdout, /要写 YZJ_MY_OPEN_ID/);
|
|
240
|
+
assert.match(bad.stdout, /YZJ_BOT_AGENT 的命令找不到: \/nonexistent\/agent/);
|
|
241
|
+
s.setNotify('"yzj-robot"');
|
|
242
|
+
const ok = s.listen(['doctor'], env);
|
|
243
|
+
assert.equal(ok.code, 0, ok.out);
|
|
244
|
+
assert.equal(ok.stdout, '');
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test('listen:用法不对时退出码为 2,列出子命令', async (t) => {
|
|
248
|
+
const s = setup(t);
|
|
249
|
+
assert.equal(s.listen(['nope']).code, 2);
|
|
250
|
+
assert.equal(s.listen([]).code, 2);
|
|
251
|
+
const r = s.listen(['--help']);
|
|
252
|
+
assert.match(r.stderr, /work-rhythm listen: 用法: work-rhythm listen <子命令>/);
|
|
253
|
+
assert.match(r.stderr, /handle-frame/);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
test('listen reset:丢掉机器人的专属会话', async (t) => {
|
|
257
|
+
const s = setup(t);
|
|
258
|
+
const { env } = await withHook(t, s);
|
|
259
|
+
write(`${s.box.work}/01-inbox/_bot/session-id`, 'old-session\n');
|
|
260
|
+
const r = s.listen(['reset'], env);
|
|
261
|
+
assert.equal(r.code, 0);
|
|
262
|
+
assert.match(r.stdout, /已丢掉机器人的专属会话/);
|
|
263
|
+
assert.equal(exists(`${s.box.work}/01-inbox/_bot/session-id`), false);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
// ---------------------------------------------------------------- zcode 预置
|
|
267
|
+
// 在工作目录里无界面运行,消息在最后一个参数;第二轮用机器人的专属会话接上
|
|
268
|
+
function zcode(s) {
|
|
269
|
+
return { ...s.stubs, ...s.bot, YZJ_BOT_AGENT: 'zcode' };
|
|
270
|
+
}
|
|
271
|
+
const real = (s) => fs.realpathSync(s.box.work);
|
|
272
|
+
|
|
273
|
+
test('listen:zcode 预置在工作目录里以 plan 模式无界面运行,我的话在最后一个参数里,后面跟着带给 agent 的提示', async (t) => {
|
|
274
|
+
const s = setup(t);
|
|
275
|
+
const env = zcode(s);
|
|
276
|
+
s.listen(['reset'], env);
|
|
277
|
+
const r = s.listen(['handle', '第一句'], env);
|
|
278
|
+
assert.equal(r.code, 0, r.out);
|
|
279
|
+
assert.match(r.stdout, /回复:第一句/);
|
|
280
|
+
assert.match(r.stdout, /这句来自云之家/);
|
|
281
|
+
const args = read(s.stubs.STUB_ZCODE_ARGS);
|
|
282
|
+
assert.ok(args.includes(`--cwd ${real(s)} --mode plan --no-color --json -p 第一句`), args);
|
|
283
|
+
assert.ok(!args.includes('--resume'), args);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
test('listen:zcode 预置第二轮用机器人的专属会话接上', async (t) => {
|
|
287
|
+
const s = setup(t);
|
|
288
|
+
const env = zcode(s);
|
|
289
|
+
s.listen(['handle', '第一句'], env);
|
|
290
|
+
s.listen(['handle', '第二句'], env);
|
|
291
|
+
assert.ok(read(s.stubs.STUB_ZCODE_ARGS).includes('--mode plan --resume sess_fake --no-color --json -p 第二句'), read(s.stubs.STUB_ZCODE_ARGS));
|
|
292
|
+
assert.equal(s.session(), 'sess_fake');
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
test('listen:zcode 预置遇到 /new 丢掉专属会话', async (t) => {
|
|
296
|
+
const s = setup(t);
|
|
297
|
+
const env = { ...zcode(s), STUB_ROBOT_URL: (await webhook(t)).url };
|
|
298
|
+
s.listen(['handle', '第一句'], env);
|
|
299
|
+
s.listen(['handle-frame', frame('me-0001', 'm8', '/new')], env);
|
|
300
|
+
assert.equal(exists(`${s.box.work}/01-inbox/_bot/session-id`), false);
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
test('listen:ZCODE_CLI 指的文件不存在时退出码为 3', async (t) => {
|
|
304
|
+
const s = setup(t);
|
|
305
|
+
const env = { ...zcode(s), ZCODE_CLI: '/nonexistent' };
|
|
306
|
+
assert.equal(s.listen(['handle', 'x'], env).code, 3);
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
// ---------------------------------------------------------------- dsh 预置
|
|
310
|
+
// 用 DSH 自带的 Node 跑它的 headless 模式,带一份只含模型配置、并打开凭据存储的覆盖文件。
|
|
311
|
+
// 模型和凭据跟着 DSH 桌面应用里的配置走,这份覆盖文件是值守从那份配置里摘出来的
|
|
312
|
+
function dsh(s) {
|
|
313
|
+
return { ...s.stubs, ...s.bot, YZJ_BOT_AGENT: 'dsh' };
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
test('listen:dsh 预置用 DSH 自带的 Node 以 headless 模式跑,覆盖文件只带模型配置并打开凭据存储,默认只读', async (t) => {
|
|
317
|
+
const s = setup(t);
|
|
318
|
+
const env = dsh(s);
|
|
319
|
+
const patch = `${real(s)}/01-inbox/_bot/dsh-headless.patch.yml`;
|
|
320
|
+
s.listen(['reset'], env);
|
|
321
|
+
const r = s.listen(['handle', '第一句'], env);
|
|
322
|
+
assert.equal(r.code, 0, r.out);
|
|
323
|
+
assert.match(r.stdout, /回复:第一句/);
|
|
324
|
+
assert.ok(read(s.stubs.STUB_DSH_ARGS).includes(`--profile headless --patch ${patch} --permission-mode read-only --json 第一句`), read(s.stubs.STUB_DSH_ARGS));
|
|
325
|
+
assert.equal(read(s.stubs.STUB_DSH_ENV), `run_as_node=1 dsh_home=${s.stubs.DSH_HOME}\n`);
|
|
326
|
+
const file = read(patch);
|
|
327
|
+
for (const need of ['- id: credentials', ' disabled: false', '- id: agent-default-model', '- id: llm-pi-ai']) assert.ok(file.includes(need), `${need} 不在覆盖文件里`);
|
|
328
|
+
assert.ok(!file.includes('ui-theme'), '无关的条目不该带进去');
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
test('listen:dsh 预置第二轮用专属会话接上;模式写 edit 就是允许改工作目录', async (t) => {
|
|
332
|
+
const s = setup(t);
|
|
333
|
+
const env = dsh(s);
|
|
334
|
+
s.listen(['handle', '第一句'], env);
|
|
335
|
+
s.listen(['handle', '第二句'], { ...env, YZJ_BOT_AGENT_MODE: 'edit' });
|
|
336
|
+
assert.ok(read(s.stubs.STUB_DSH_ARGS).includes('--permission-mode workspace-write --session-id session_fake --json 第二句'), read(s.stubs.STUB_DSH_ARGS));
|
|
337
|
+
assert.equal(s.session(), 'session_fake');
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
test('listen handle:dsh 预置的 final 事件说没跑完时退出码为 5', async (t) => {
|
|
341
|
+
const s = setup(t);
|
|
342
|
+
const env = dsh(s);
|
|
343
|
+
assert.equal(s.listen(['handle', '出错的一句'], { ...env, STUB_DSH_FINAL: 'error' }).code, 5);
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
test('listen:找不到 DSH 的配置时退出码为 3', async (t) => {
|
|
347
|
+
const s = setup(t);
|
|
348
|
+
const env = { ...dsh(s), DSH_HOME: path.join(s.box.root, 'no-such-home') };
|
|
349
|
+
const r = s.listen(['handle', 'x'], env);
|
|
350
|
+
assert.equal(r.code, 3);
|
|
351
|
+
assert.match(r.stderr, /找不到 DSH 的配置/);
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
test('agent:自定义命令按空白拆开,引号里的算一个参数,消息跟在最后', async (t) => {
|
|
355
|
+
const s = setup(t);
|
|
356
|
+
const { env } = await withHook(t, s);
|
|
357
|
+
// 假的 agent 把收到的参数一行一个记下来
|
|
358
|
+
s.box.stub('echo-args', "process.stdout.write(process.argv.slice(2).join('\\n') + '\\n');");
|
|
359
|
+
const r = s.listen(['handle', '一句话'], { ...env, YZJ_BOT_AGENT: `${path.join(s.box.bin, 'echo-args')} --name "a b" 'c d'` });
|
|
360
|
+
assert.equal(r.code, 0, r.out);
|
|
361
|
+
assert.equal(r.stdout, '--name\na b\nc d\n一句话\n');
|
|
362
|
+
});
|
|
@@ -0,0 +1,176 @@
|
|
|
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, webhook, freePort, read, write, setKey } = require('./harness');
|
|
7
|
+
const robot = require('../lib/robot');
|
|
8
|
+
|
|
9
|
+
const TOKEN = 'stubtoken0123456789abcd';
|
|
10
|
+
// 搭工作目录:消息渠道默认是 yzj-robot,钥匙串的假地址指向假 webhook
|
|
11
|
+
function setup(t, { notify = 'yzj-robot', hook } = {}) {
|
|
12
|
+
const box = makeSandbox(t);
|
|
13
|
+
box.workdir();
|
|
14
|
+
const file = `${box.work}/00-config/profile.yaml`;
|
|
15
|
+
write(file, setKey(read(file), 'notify', JSON.stringify(notify)));
|
|
16
|
+
const env = { ...box.stubs(), STUB_KEYCHAIN: 'ok', STUB_ROBOT_URL: hook ? hook.url : '', YZJ_MY_OPEN_ID: 'me-0001' };
|
|
17
|
+
return { box, env, notify: (args, extra = {}) => box.run(['notify', ...args], { env: { ...env, ...extra.env }, input: extra.input }) };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// ---------------------------------------------------------------- 命令
|
|
21
|
+
test('notify:preferences.notify 留空时什么都不发,退出码为 0', async (t) => {
|
|
22
|
+
const hook = await webhook(t);
|
|
23
|
+
const { notify } = setup(t, { notify: '', hook });
|
|
24
|
+
const r = notify(['-'], { input: '今天的重点三件\n' });
|
|
25
|
+
assert.equal(r.code, 0);
|
|
26
|
+
assert.match(r.stderr, /留空,不推送/);
|
|
27
|
+
assert.equal(r.stdout, '');
|
|
28
|
+
assert.deepEqual(hook.requests(), []);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('notify:渠道不认识时退出码为 2,不发请求', async (t) => {
|
|
32
|
+
const hook = await webhook(t);
|
|
33
|
+
const { notify } = setup(t, { notify: '云之家发给自己', hook });
|
|
34
|
+
const r = notify(['x']);
|
|
35
|
+
assert.equal(r.code, 2);
|
|
36
|
+
assert.match(r.stderr, /不认识的渠道「云之家发给自己」/);
|
|
37
|
+
assert.deepEqual(hook.requests(), []);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('notify:钥匙串里没有地址时退出码为 3,说清怎么存,不发请求', async (t) => {
|
|
41
|
+
const hook = await webhook(t);
|
|
42
|
+
const { notify } = setup(t, { hook });
|
|
43
|
+
const r = notify(['x'], { env: { STUB_KEYCHAIN: 'no' } });
|
|
44
|
+
assert.equal(r.code, 3);
|
|
45
|
+
assert.match(r.stderr, /钥匙串里没有 work-rhythm-yzj-robot-webhook/);
|
|
46
|
+
assert.match(r.stderr, /security add-generic-password -a "\$USER" -s work-rhythm-yzj-robot-webhook -U -w/);
|
|
47
|
+
assert.match(r.stderr, /docs\/channels\.md/);
|
|
48
|
+
assert.deepEqual(hook.requests(), []);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('notify:yzj-robot 用钥匙串里的地址、以我在 local.env 里的 openId 发文本,输出里不带 token', async (t) => {
|
|
52
|
+
const hook = await webhook(t);
|
|
53
|
+
const { box, notify } = setup(t, { hook });
|
|
54
|
+
write(`${box.work}/collectors/local.env`, 'YZJ_MY_OPEN_ID="me-0001"\n');
|
|
55
|
+
const r = notify(['计划正文']);
|
|
56
|
+
assert.equal(r.code, 0, r.stderr);
|
|
57
|
+
assert.equal(r.stdout, '');
|
|
58
|
+
assert.match(r.stderr, /已通过云之家机器人发给我(msgId BOT-m1,会话 BOT-me-0001-BOT-bot)/);
|
|
59
|
+
const requests = hook.requests();
|
|
60
|
+
assert.equal(requests.length, 1);
|
|
61
|
+
assert.equal(requests[0].method, 'POST');
|
|
62
|
+
assert.equal(requests[0].path, `/gateway/robot/webhook/send?yzjtype=0&yzjtoken=${TOKEN}`);
|
|
63
|
+
assert.deepEqual(JSON.parse(requests[0].body), { msgtype: 2, content: '计划正文', notifyParams: [{ type: 'openIds', values: ['me-0001'] }] });
|
|
64
|
+
assert.ok(!r.out.includes(TOKEN), r.out);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('notify:local.env 里没写 openId 时用 yzj-cli whoami 现取', async (t) => {
|
|
68
|
+
const hook = await webhook(t);
|
|
69
|
+
const { notify } = setup(t, { hook });
|
|
70
|
+
const r = notify(['复盘正文'], { env: { YZJ_MY_OPEN_ID: '' } });
|
|
71
|
+
assert.equal(r.code, 0, r.stderr);
|
|
72
|
+
const body = JSON.parse(hook.requests()[0].body);
|
|
73
|
+
assert.equal(body.content, '复盘正文');
|
|
74
|
+
assert.deepEqual(body.notifyParams, [{ type: 'openIds', values: ['me-0001'] }]);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test('notify:取不到 openId 时退出码为 3', async (t) => {
|
|
78
|
+
const hook = await webhook(t);
|
|
79
|
+
const { notify } = setup(t, { hook });
|
|
80
|
+
// 没写 openId,yzj-cli 又没登录(whoami 报错)
|
|
81
|
+
const r = notify(['x'], { env: { YZJ_MY_OPEN_ID: '', STUB_WHOAMI: 'fail' } });
|
|
82
|
+
assert.equal(r.code, 3);
|
|
83
|
+
assert.match(r.stderr, /取不到我的 openId/);
|
|
84
|
+
assert.deepEqual(hook.requests(), []);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test('notify:接口返回 success:false 时退出码为 5,带上 errorCode,地址里的 token 打码', async (t) => {
|
|
88
|
+
const hook = await webhook(t, { mode: 'apifail' });
|
|
89
|
+
const { notify } = setup(t, { hook });
|
|
90
|
+
const r = notify(['x']);
|
|
91
|
+
assert.equal(r.code, 5);
|
|
92
|
+
assert.match(r.stderr, /云之家没有接受这条消息(HTTP 200)/);
|
|
93
|
+
assert.match(r.stderr, /43001/);
|
|
94
|
+
assert.match(r.stderr, /yzjtoken=<已打码>/);
|
|
95
|
+
assert.ok(!r.out.includes(TOKEN), r.out);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test('notify:HTTP 不是 200 时退出码为 5', async (t) => {
|
|
99
|
+
const hook = await webhook(t, { mode: 'http' });
|
|
100
|
+
const { notify } = setup(t, { hook });
|
|
101
|
+
const r = notify(['x']);
|
|
102
|
+
assert.equal(r.code, 5);
|
|
103
|
+
assert.match(r.stderr, /HTTP 500/);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test('notify:连不上时退出码为 5,说清原因', async (t) => {
|
|
107
|
+
const { notify } = setup(t);
|
|
108
|
+
// 假 webhook 没起,地址指向一个刚空出来、没人听的端口
|
|
109
|
+
const r = notify(['x'], { env: { STUB_ROBOT_URL: `http://127.0.0.1:${await freePort()}/send?yzjtoken=${TOKEN}` } });
|
|
110
|
+
assert.equal(r.code, 5);
|
|
111
|
+
assert.match(r.stderr, /请求没发出去/);
|
|
112
|
+
assert.match(r.stderr, /ECONNREFUSED/);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test('notify:只给了空白时退出码为 2', async (t) => {
|
|
116
|
+
const hook = await webhook(t);
|
|
117
|
+
const { notify } = setup(t, { hook });
|
|
118
|
+
assert.equal(notify([' ']).code, 2);
|
|
119
|
+
assert.equal(notify(['-'], { input: '\n \n' }).code, 2);
|
|
120
|
+
assert.deepEqual(hook.requests(), []);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test('notify:没给文字时退出码为 2,不去读标准输入', async (t) => {
|
|
124
|
+
const hook = await webhook(t);
|
|
125
|
+
const { notify } = setup(t, { hook });
|
|
126
|
+
const r = notify([], { input: '本来想发的话' });
|
|
127
|
+
assert.equal(r.code, 2);
|
|
128
|
+
assert.match(r.stderr, /用法: work-rhythm notify/);
|
|
129
|
+
assert.deepEqual(hook.requests(), []);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test('notify:文字从标准输入读', async (t) => {
|
|
133
|
+
const hook = await webhook(t);
|
|
134
|
+
const { notify } = setup(t, { hook });
|
|
135
|
+
const r = notify(['-'], { input: '第一行\n第二行\n' });
|
|
136
|
+
assert.equal(r.code, 0, r.stderr);
|
|
137
|
+
assert.equal(JSON.parse(hook.requests()[0].body).content, '第一行\n第二行\n');
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test('notify:没装 security 时退出码为 4', async (t) => {
|
|
141
|
+
const { notify, box } = setup(t);
|
|
142
|
+
const empty = path.join(box.root, 'empty-bin');
|
|
143
|
+
fs.mkdirSync(empty, { recursive: true });
|
|
144
|
+
const r = notify(['x'], { env: { PATH: empty } });
|
|
145
|
+
assert.equal(r.code, 4);
|
|
146
|
+
assert.match(r.stderr, /需要 security/);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test('notify:local.env 里的 YZJ_ROBOT_KEYCHAIN_SERVICE 换了服务名,提示里跟着换', async (t) => {
|
|
150
|
+
const { notify, box } = setup(t);
|
|
151
|
+
write(`${box.work}/collectors/local.env`, 'YZJ_ROBOT_KEYCHAIN_SERVICE="my-robot"\n');
|
|
152
|
+
const r = notify(['x'], { env: { STUB_KEYCHAIN: 'no' } });
|
|
153
|
+
assert.equal(r.code, 3);
|
|
154
|
+
assert.match(r.stderr, /钥匙串里没有 my-robot/);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// ---------------------------------------------------------------- 函数
|
|
158
|
+
test('robot:打码只动 token,别的内容原样', () => {
|
|
159
|
+
assert.equal(robot.mask('https://a.b/send?yzjtype=0&yzjtoken=abc_DEF-123&x=1'), 'https://a.b/send?yzjtype=0&yzjtoken=<已打码>&x=1');
|
|
160
|
+
assert.equal(robot.mask('普通文字'), '普通文字');
|
|
161
|
+
assert.equal(robot.mask(null), 'null');
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test('robot:HTTP 200 但 success 不是 true 不算成功;网络错误时 status 为 0', async () => {
|
|
165
|
+
const respond = (status, text) => async () => ({ ok: status >= 200 && status < 300, status, text: async () => text });
|
|
166
|
+
const ok = await robot.postRobot('http://x/', {}, { fetchImpl: respond(200, '{"success":true,"data":{"msgId":"m1"}}') });
|
|
167
|
+
assert.deepEqual(ok, { ok: true, status: 200, body: { success: true, data: { msgId: 'm1' } }, text: '{"success":true,"data":{"msgId":"m1"}}' });
|
|
168
|
+
const lie = await robot.postRobot('http://x/', {}, { fetchImpl: respond(200, '{"success":false}') });
|
|
169
|
+
assert.equal(lie.ok, false);
|
|
170
|
+
const down = await robot.postRobot('http://x/', {}, { fetchImpl: async () => { throw new TypeError('fetch failed', { cause: { code: 'ECONNREFUSED' } }); } });
|
|
171
|
+
assert.deepEqual([down.ok, down.status, down.error], [false, 0, 'fetch failed(ECONNREFUSED)']);
|
|
172
|
+
const slow = await robot.postRobot('http://x/', {}, { fetchImpl: () => new Promise(() => {}), timeoutMs: 100 });
|
|
173
|
+
assert.equal(slow.ok, false);
|
|
174
|
+
assert.match(slow.error, /请求超时/);
|
|
175
|
+
});
|
|
176
|
+
|