Files
u-claw/tests/self-heal.test.mjs
zheng 3221856d50
Some checks failed
Tests / test (push) Has been cancelled
docs: 文档改为英文,并修正过时内容
不是翻译 —— 多数文档描述的行为已经不存在了。

先修一个更基本的问题:我们不拥有 u-claw.org 域名,那是上游的。所以之前写在
README、诊断包提示、联系方式里的 help@u-claw.org 全都会把用户的问题发给
上游 —— 一个没有理由回复的人。改为指向我们自己的 issue tracker,并加断言
禁止再出现指向该域名的支持入口。

重写(内容过时,不是语言问题):
- install/README.md —— 还写着 10 个中国技能、DeepSeek 优先、国内镜像。
  现在按实际流程写:技能读 manifest、模型菜单 Gemini 优先、bundle 有
  SHA256 校验。并如实写明 curl|bash 在受管企业电脑上会被 EDR 拦。
- CLAUDE.md 的模型配置整节 —— 还在描述虾盘云首选卡片和 12 个 provider,
  那个界面已经换成单框 Key 输入了。
- SECURITY.md —— 安全报告原本指向上游维护者个人邮箱。fork 之后那条路由
  是错的:漏洞会发给写不了这份代码、也修不了的人。
- CONTRIBUTING.md —— 补上 fork 关系、pre-push 钩子怎么装、以及
  `node --test tests/` 为什么不能用。

翻译并保留:
- bootable/README.md、TROUBLESHOOTING.md —— 面向用户,顺带把
  「国内镜像」「小米/华为 BIOS 按键」等换成目标市场的实际情况

HANDOFF.md 重写为一份事故复盘:原文一半是过时的一次性交接笔记(引用的
website/guide.html 已不在本仓库),另一半是 persistence.dat 未格式化导致
启动失败的排查记录 —— 后者有长期价值,尤其是「读 offset 1080 的两字节
验证 ext4」这个判断方法,已同时写进 bootable/README.md。

bootable/IMPROVEMENTS_SUMMARY.md 保留中文,加了说明:它是上游 fork 前的
历史改进记录,没人引用,描述的是已完成的工作而非当前行为。翻译它反而会
让人误以为是现行文档。

新增 skills/en/uclaw-help —— 把「怎么用、东西在哪、出问题怎么办」做成
内置知识,每个角色都装。方案 C10.8:能问的产品才是不需要学的产品。
2026-08-17 19:12:07 +08:00

146 lines
6.8 KiB
JavaScript

import { mkdirSync, mkdtempSync, writeFileSync, readFileSync, existsSync, readdirSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import test from 'node:test';
import assert from 'node:assert/strict';
import { buildChecks, runRepairs, writeDiagnostics } from '../portable/lib/self-heal.mjs';
const repoRoot = fileURLToPath(new URL('..', import.meta.url));
const HEALTHY_CONFIG = {
gateway: { auth: { token: 'uclaw' } },
models: { providers: { anthropic: { apiKey: 'sk-ant-api03-SECRETVALUE0123456789', baseUrl: 'https://api.anthropic.com/v1' } } },
channels: { telegram: { botToken: '123456:TELEGRAMSECRET' } },
uclaw: { locale: 'en', personas: ['finance'], tier: 'standard' },
};
function makeDrive() {
const root = mkdtempSync(join(tmpdir(), 'uclaw-heal-'));
for (const dir of ['data/.openclaw', 'data/memory', 'data/backups', 'data/logs', 'app/core/node_modules/openclaw']) {
mkdirSync(join(root, dir), { recursive: true });
}
writeFileSync(join(root, 'app/core/node_modules/openclaw/openclaw.mjs'), '// entry\n');
const paths = {
data: join(root, 'data'),
state: join(root, 'data/.openclaw'),
config: join(root, 'data/.openclaw/openclaw.json'),
runtimeJson: join(root, 'data/.openclaw/runtime.json'),
core: join(root, 'app/core'),
};
writeFileSync(paths.config, JSON.stringify(HEALTHY_CONFIG, null, 2));
return { root, paths };
}
const checksFor = (paths, stamp = 'TEST') =>
buildChecks({ paths, defaultConfigPath: null, portRange: { from: 18789, to: 18799 }, stamp });
test('a healthy drive is left completely alone', async () => {
const { root, paths } = makeDrive();
try {
const before = readFileSync(paths.config, 'utf8');
const applied = await runRepairs(checksFor(paths));
// Every check runs on every failed start, including failures none of them
// explain. Touching a healthy drive would turn one problem into two.
assert.deepEqual(applied, [], `repairs fired on a healthy drive: ${JSON.stringify(applied)}`);
assert.equal(readFileSync(paths.config, 'utf8'), before, 'the settings file was rewritten');
} finally {
rmSync(root, { recursive: true, force: true });
}
});
test('a corrupt settings file is rebuilt, and the original is kept', async () => {
const { root, paths } = makeDrive();
try {
writeFileSync(paths.config, '{"gateway": {half-written');
const applied = await runRepairs(checksFor(paths, 'STAMP1'));
assert.ok(applied.some((a) => a.id === 'config-readable'), 'the damaged file was not noticed');
JSON.parse(readFileSync(paths.config, 'utf8')); // throws if still broken
// Repairs must never destroy user data — the diagnosis could be wrong.
const kept = readdirSync(paths.state).filter((n) => n.includes('broken-STAMP1'));
assert.equal(kept.length, 1, 'the original file was not preserved');
assert.match(readFileSync(join(paths.state, kept[0]), 'utf8'), /half-written/);
} finally {
rmSync(root, { recursive: true, force: true });
}
});
test('an interrupted copy is detected so startup can fetch it again', async () => {
const { root, paths } = makeDrive();
try {
rmSync(join(paths.core, 'node_modules/openclaw/openclaw.mjs'));
const applied = await runRepairs(checksFor(paths, 'STAMP2'));
assert.ok(applied.some((a) => a.id === 'openclaw-present'), 'a half-copied drive went unnoticed');
assert.ok(!existsSync(join(paths.core, 'node_modules')), 'node_modules should be moved aside so it reinstalls');
} finally {
rmSync(root, { recursive: true, force: true });
}
});
test('a leftover port record from a crash is cleared', async () => {
const { root, paths } = makeDrive();
try {
// Nothing is listening on 18795, so this record only makes the launcher wait.
writeFileSync(paths.runtimeJson, JSON.stringify({ configServerPort: 18795 }));
const applied = await runRepairs(checksFor(paths));
assert.ok(applied.some((a) => a.id === 'stale-runtime'), 'the stale record survived');
assert.ok(!existsSync(paths.runtimeJson));
} finally {
rmSync(root, { recursive: true, force: true });
}
});
test('missing folders are recreated', async () => {
const { root, paths } = makeDrive();
try {
rmSync(join(paths.data, 'memory'), { recursive: true });
rmSync(join(paths.data, 'logs'), { recursive: true });
const applied = await runRepairs(checksFor(paths));
assert.ok(applied.some((a) => a.id === 'data-dirs'));
assert.ok(existsSync(join(paths.data, 'memory')) && existsSync(join(paths.data, 'logs')));
} finally {
rmSync(root, { recursive: true, force: true });
}
});
test('the diagnostics report carries no secrets', async () => {
const { root, paths } = makeDrive();
try {
const checks = checksFor(paths);
const report = await writeDiagnostics({
paths, checks, applied: [], stamp: 'STAMP3',
versions: { openclaw: '2026.7.1-2', node_pinned: 'v22.22.1' },
});
const body = readFileSync(report, 'utf8');
// The user should be able to read this before deciding to send it anywhere.
for (const secret of ['sk-ant-api03-SECRETVALUE0123456789', 'TELEGRAMSECRET']) {
assert.ok(!body.includes(secret), `${secret} leaked into the diagnostics report`);
}
assert.match(body, /redacted/, 'redaction should be visible so the user can tell it happened');
assert.match(body, /api\.anthropic\.com/, 'non-secret settings should survive — the report has to be useful');
assert.match(body, /## System[\s\S]*## Repairs attempted[\s\S]*## Settings/, 'report sections are missing');
} finally {
rmSync(root, { recursive: true, force: true });
}
});
test('startup heals before it complains, and never dead-ends', () => {
const start = readFileSync(join(repoRoot, 'portable', 'lib', 'start.mjs'), 'utf8');
assert.match(start, /healAndRetry/, 'startup should try to repair itself');
// Diagnose was a tool you had to know existed; by the time startup visibly
// failed, anyone who did not know had already given up.
assert.match(start, /start\.checking/, 'the user should see "checking", not a stack trace');
assert.match(start, /writeDiagnostics/, 'an unrepairable failure should still leave a report');
assert.match(start, /start\.diagnostics_written/, 'the user should be told where the report is');
const en = JSON.parse(readFileSync(join(repoRoot, 'portable', 'lib', 'messages', 'en.json'), 'utf8'));
const origin = JSON.parse(readFileSync(join(repoRoot, 'origin.json'), 'utf8'));
// A report with nowhere to go is just a file on a drive.
assert.ok(
en['start.diagnostics_hint'].includes(origin.support.issues),
'the report should point at the support route origin.json declares',
);
assert.match(en['start.diagnostics_hint'], /removed/i, 'say that keys were stripped, or nobody will send it');
});