test: add comprehensive Curio installation and runtime fixtures

This commit is contained in:
2026-09-09 17:14:52 +08:00
parent 0d38ef2959
commit 775d891288
38 changed files with 3609 additions and 4 deletions
+170
View File
@@ -0,0 +1,170 @@
export const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
export function assert(condition, message) { if (!condition) throw new Error(message); }
export function equal(actual, expected, label = '返回值不匹配') {
// Compare JSON objects independently of property insertion order.
const canonical = value => Array.isArray(value) ? value.map(canonical) : value && typeof value === 'object'
? Object.fromEntries(Object.keys(value).sort().map(key => [key, canonical(value[key])])) : value;
assert(JSON.stringify(canonical(actual)) === JSON.stringify(canonical(expected)), `${label}: ${JSON.stringify(actual)}`);
}
export async function expectError(action, expected) {
try { await action(); } catch (error) {
assert(error.code === expected, `预期 ${expected},实际 ${error.code ?? error.message}`);
return { expected, actual: error.code, message: error.message };
}
throw new Error(`预期 ${expected},调用却成功了`);
}
export function hostChecks(client) {
const call = (service, method, params = {}) => client.call(service, method, params);
return [
['SDK 与运行上下文', async () => {
const context = await call('host.runtime', 'getContext');
equal(context.curioId, client.context.curioId);
equal(context.curioVersion, client.context.curioVersion);
equal(context.apiVersion, 1);
return context;
}],
['Host 信息与数据目录', async () => {
const info = await call('host.runtime', 'getHostInfo');
const paths = await call('host.runtime', 'getPaths');
for (const key of ['data', 'cache', 'logs', 'temp']) assert(paths[key]?.startsWith('/'), `缺少绝对路径 ${key}`);
equal(info.protocol, 1);
return { info, paths };
}],
['JSON 与 Unicode 往返', async () => {
const value = { text: '中文 / emoji 🧪 / e\u0301 / \n换行', number: 12.5, null: null, array: [false, {}, ''] };
equal(await call('host.runtime', 'echo', value), value);
return value;
}],
['32 路并发响应匹配', async () => {
const values = await Promise.all(Array.from({ length: 32 }, (_, index) => call('host.runtime', 'echo', { index })));
values.forEach((value, index) => equal(value, { index }));
return { responses: values.length };
}],
['服务发现、描述、解析与状态', async () => {
const { services } = await call('host.services', 'list', { prefix: 'host.' });
for (const id of ['host.runtime', 'host.storage', 'host.fs']) assert(services.some(item => item.id === id), `缺少 ${id}`);
const description = await call('host.services', 'describe', { service: 'host.storage' });
assert(description.contract.methods.set, '缺少 storage.set 契约');
await call('host.services', 'resolve', { service: 'host.storage', version: '^1.0.0' });
equal((await call('host.services', 'getStatus', { service: 'host.storage' })).state, 'Available');
return services;
}],
['独立存储、null 与不存在、前缀查询、删除', async () => {
const key = `qa.temporary.${crypto.randomUUID()}`;
try {
equal((await call('host.storage', 'get', { key })).found, false);
await call('host.storage', 'set', { key, value: null });
equal(await call('host.storage', 'get', { key }), { found: true, value: null });
const value = { owner: client.context.curioId, unicode: '存储 🧪', array: [1, false] };
await call('host.storage', 'set', { key, value });
equal((await call('host.storage', 'get', { key })).value, value);
assert((await call('host.storage', 'list', { prefix: key })).keys.includes(key), '前缀查询缺少测试键');
await call('host.storage', 'delete', { key });
equal((await call('host.storage', 'get', { key })).found, false);
return { namespace: client.context.curioId };
} finally { await call('host.storage', 'delete', { key }); }
}],
['持久化与同名键隔离标记', async () => {
const key = 'qa.namespace-owner';
const previous = await call('host.storage', 'get', { key });
if (previous.found) equal(previous.value, client.context.curioId, '存储命名空间混用');
await call('host.storage', 'set', { key, value: client.context.curioId });
return { previous, stored: client.context.curioId, next: '在对照插件运行检查,然后关闭并重新打开本插件再运行。' };
}],
['存储单值大小限制', () => expectError(() => call('host.storage', 'set', { key: 'qa.oversize', value: 'x'.repeat(1024 * 1024 + 1) }), 'host.storage.value_too_large')],
['文件 CRUD、二进制、复制与移动', async () => {
const paths = await call('host.runtime', 'getPaths');
const directory = `${paths.temp}/qa-${crypto.randomUUID()}`;
const text = `${directory}/中文 空格.txt`;
await call('host.fs', 'createDirectory', { path: directory });
try {
await call('host.fs', 'writeText', { path: text, content: 'Trove 文件 🧪\n第二行' });
equal((await call('host.fs', 'readText', { path: text })).content, 'Trove 文件 🧪\n第二行');
equal((await call('host.fs', 'stat', { path: text })).type, 'file');
assert((await call('host.fs', 'list', { path: directory })).entries.some(item => item.name === '中文 空格.txt'), '目录列表缺少文件');
const binary = `${directory}/bytes.bin`;
await call('host.fs', 'writeBinary', { path: binary, data: 'AAECA/7/' });
equal((await call('host.fs', 'readBinary', { path: binary })).data, 'AAECA/7/');
await call('host.fs', 'copy', { from: binary, to: `${directory}/copy.bin` });
await call('host.fs', 'move', { from: `${directory}/copy.bin`, to: `${directory}/moved.bin` });
equal((await call('host.fs', 'exists', { path: `${directory}/copy.bin` })).exists, false);
equal((await call('host.fs', 'readBinary', { path: `${directory}/moved.bin` })).data, 'AAECA/7/');
const temporary = await call('host.fs', 'createTempFile');
await call('host.fs', 'remove', temporary);
return { unicodeFilename: true, binaryBytes: 6, directory };
} finally { await call('host.fs', 'remove', { path: directory, recursive: true }); }
}],
['未知服务错误', () => expectError(() => call('dev.trove.qa.absent.probe', 'echo'), 'service.not_found')],
['未知方法错误', () => expectError(() => call('host.runtime', 'unknownQaMethod'), 'service.method_not_found')],
['Host 参数契约校验', () => expectError(() => call('host.storage', 'get', { key: 123 }), 'request.invalid_params')],
['服务版本约束', () => expectError(() => call('host.services', 'resolve', { service: 'host.storage', version: '^99.0.0' }), 'service.version_mismatch')],
];
}
export function backendChecks(client, service) {
const call = (method, params = {}, options) => client.call(service, method, params, options);
return [
['后端启动、身份、环境与参数', async () => {
const info = await call('info');
assert(info.pid > 0 && info.curioId.startsWith('dev.trove.qa.'), '后端身份缺失');
assert(info.label.startsWith('test-'), 'Manifest 环境变量未传入');
return info;
}],
['原生 Unicode 与超过 200 KiB 消息', async () => {
const value = { text: '中🧪'.repeat(32768), null: null, nested: [false, { n: 3 }] };
equal(await call('echo', value), value);
return { utf8Bytes: new TextEncoder().encode(JSON.stringify(value)).length };
}],
['24 路乱序并发响应匹配', async () => {
const responses = await Promise.all(Array.from({ length: 24 }, (_, index) => call('delay', { ms: (23 - index) * 8, value: { index } })));
responses.forEach((value, index) => equal(value, { index }));
return { responses: responses.length };
}],
['结构化业务错误', () => expectError(() => call('fail'), 'qa.intentional_error')],
['未知后端方法', () => expectError(() => call('unknownQaMethod'), 'service.method_not_found')],
['请求超时与后续可用性', async () => {
const error = await expectError(() => call('delay', { ms: 1500 }, { timeoutMs: 80 }), 'request.timeout');
equal(await call('echo', { recovered: true }), { recovered: true });
return error;
}],
['AbortSignal 取消与后续可用性', async () => {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 60);
try {
const error = await expectError(() => call('delay', { ms: 1500 }, { signal: controller.signal }), 'request.cancelled');
equal(await call('echo', { recovered: true }), { recovered: true });
return error;
} finally { clearTimeout(timer); }
}],
['后端 → Host 存储调用', async () => {
const key = 'qa.backend-roundtrip';
const value = { unicode: '双向 🧪', caller: client.context.curioId };
const response = await call('storageRoundtrip', { key, value });
equal(response, { found: true, value });
// Backend writes to its own namespace. Fixed small probes are retained there for inspection.
return { key, value, note: '存储所有者为后端提供方;可在插件数据目录检查。' };
}],
['事件订阅、顺序、取消订阅', async () => {
const events = [];
const taskId = crypto.randomUUID();
const subscription = await client.subscribe(service, 'progress', { taskId }, data => events.push(data));
try {
equal(await call('uppercase', { text: 'hello 中文' }), { text: 'HELLO 中文' });
const deadline = Date.now() + 1500;
while (!events.some(item => item.progress === 1) && Date.now() < deadline) await sleep(20);
assert(events.length >= 2 && events[0].progress === 0 && events.at(-1).progress === 1, '未收到完整进度序列');
assert(events.every((item, i) => item.taskId === taskId && (i === 0 || item.progress >= events[i - 1].progress)), '事件身份或顺序不正确');
} finally { await subscription.close(); }
const before = events.length;
await call('uppercase', { text: 'after unsubscribe' });
await sleep(100);
equal(events.length, before, '取消订阅后仍收到事件');
return { events };
}],
...(service === '@self' ? [] : [
['公共服务参数契约', () => expectError(() => call('uppercase', { text: 123 }), 'request.invalid_params')],
['公共服务返回值契约', () => expectError(() => call('invalidResult'), 'service.contract_violation')],
]),
];
}
+41
View File
@@ -0,0 +1,41 @@
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Trove 插件测试台</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<header>
<p class="eyebrow">TROVE / PLUGIN TEST LAB</p>
<h1 id="title">插件测试台</h1>
<p id="context">正在连接 Trove…</p>
</header>
<main>
<section aria-label="测试控制">
<p id="description"></p>
<div class="actions">
<button id="run" disabled>运行常规检查</button>
<button id="save-report" disabled>保存 JSON 报告</button>
</div>
<p id="status" role="status">等待连接。</p>
</section>
<section>
<h2>检查结果</h2>
<p class="hint">通过代表断言已满足;需观察的系统交互不会计入自动通过。</p>
<ol id="results" aria-live="polite"></ol>
</section>
<section id="manual-section">
<h2>手动检查</h2>
<p class="hint">这些操作可能打开窗口、改写剪贴板、注册快捷键或终止本插件后端。按需逐项执行。</p>
<div id="manual" class="actions"></div>
</section>
<details>
<summary>原始报告</summary>
<pre id="report">[]</pre>
</details>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
+184
View File
@@ -0,0 +1,184 @@
import { trove } from './sdk/index.js';
import config from './config.js';
import { assert, equal, expectError, hostChecks, backendChecks } from './checks.js';
const $ = selector => document.querySelector(selector);
const results = [];
let running = false;
const call = (service, method, params = {}, options) => trove.call(service, method, params, options);
const report = () => ({ schemaVersion: 1, fixture: config, context: trove.context, generatedAt: new Date().toISOString(), results });
const serializeError = error => ({ code: error.code ?? 'qa.assertion_failed', message: error.message ?? String(error), data: error.data });
function refresh() {
$('#results').replaceChildren(...results.map(result => {
const row = document.createElement('li');
const heading = document.createElement('div');
heading.className = 'result-heading';
const title = document.createElement('strong');
title.className = result.status;
const labels = { pass: '通过', fail: '失败', observed: '已执行 · 请观察', skip: '跳过' };
title.textContent = `${labels[result.status]} · ${result.name}`;
const duration = document.createElement('small');
duration.textContent = `${result.durationMs} ms`;
heading.append(title, duration);
const detail = document.createElement('details');
const summary = document.createElement('summary');
summary.textContent = result.error?.message || '查看返回值';
const pre = document.createElement('pre');
pre.textContent = JSON.stringify(result.error || result.value, null, 2);
detail.append(summary, pre);
row.append(heading, detail);
return row;
}));
$('#report').textContent = JSON.stringify(report(), null, 2);
const counts = status => results.filter(result => result.status === status).length;
$('#status').textContent = `${running ? '运行中' : '已完成'} · 通过 ${counts('pass')} / 失败 ${counts('fail')} / 待观察 ${counts('observed')} / 跳过 ${counts('skip')}`;
}
async function check(name, action, status = 'pass') {
const start = performance.now();
try { results.push({ name, status, value: await action(), durationMs: Math.round(performance.now() - start) }); }
catch (error) { results.push({ name, status: 'fail', error: serializeError(error), durationMs: Math.round(performance.now() - start) }); }
refresh();
}
async function exclusive(action) {
if (running) return;
running = true;
document.querySelectorAll('button').forEach(button => { button.disabled = true; });
try { await action(); }
finally {
running = false;
document.querySelectorAll('button').forEach(button => { button.disabled = false; });
refresh();
}
}
function manual(name, action, status = 'observed') {
const button = document.createElement('button');
button.textContent = name;
button.onclick = () => exclusive(() => check(name, action, status));
$('#manual').append(button);
}
const descriptions = {
host: '检查真实 Host 桥接与数据操作。常规检查只在本插件命名空间和临时目录写入测试数据;隔离标记会保留以便复测。',
backend: `检查 ${config.service}:原生进程、并发、超时、取消、Host 回调和事件。跨插件测试需先安装公共服务提供方。公共服务返回值校验要求底座开启严格契约模式;默认未开启时,该项会报告失败。`,
dependency: `此插件声明了 ${config.service}。预期调用错误为 ${config.expectedError};“service.not_found”不能代替版本不兼容错误。`,
fault: '此后端故意无法完成启动。点击运行检查记录实际错误码;恢复与重试应在底座管理界面操作。',
upgrade: '先安装 1.0.0 并写入基线标记,再安装 1.1.0。关闭后重新打开,检查运行版本与数据保留;也可在底座回滚。',
};
$('#title').textContent = config.name;
document.title = config.name;
$('#description').textContent = descriptions[config.mode];
function configureManualActions() {
manual('读取持久化标记', () => call('host.storage', 'get', { key: config.mode === 'upgrade' ? 'qa.upgrade-marker' : 'qa.namespace-owner' }), 'pass');
if (config.mode === 'host') {
manual('读取本窗口尺寸', () => call('host.window', 'getBounds'), 'pass');
manual('设置窗口标题', () => call('host.window', 'setTitle', { title: `${config.name} · 标题测试成功` }));
manual('窗口居中', () => call('host.window', 'center'));
manual('文件选择(可取消)', () => call('host.dialog', 'openFile', { title: 'Trove QA:选择文件或取消' }, { timeoutMs: 120000 }));
manual('目录选择(可取消)', () => call('host.dialog', 'openDirectory', { title: 'Trove QA:选择目录或取消' }, { timeoutMs: 120000 }));
manual('保存对话框(仅选择路径)', () => call('host.dialog', 'saveFile', { title: 'Trove QA:只选择路径,不写入文件' }, { timeoutMs: 120000 }));
manual('确认对话框', () => call('host.dialog', 'confirm', { title: 'Trove QA', message: '确认和取消都应返回明确结果。' }, { timeoutMs: 120000 }));
manual('写入并读取测试剪贴板', async () => {
const text = 'Trove QA 剪贴板 🧪';
await call('host.clipboard', 'writeText', { text });
equal((await call('host.clipboard', 'readText')).text, text);
return { text, note: '系统剪贴板已被替换为测试文本。' };
}, 'pass');
manual('发送测试通知', () => call('host.notification', 'show', { title: 'Trove QA', body: `来自 ${config.id} 的测试通知` }));
manual('快捷键触发(15 秒)', async () => {
const events = [];
let registration;
const subscription = await trove.subscribe('host.shortcut', 'triggered', {}, event => events.push(event));
try {
registration = await call('host.shortcut', 'register', { shortcut: 'CommandOrControl+Shift+Alt+F9' });
$('#status').textContent = '请在 15 秒内按 Cmd+Shift+Option+F9(部分键盘需要 Fn)。';
const end = Date.now() + 15000;
while (!events.length && Date.now() < end) await new Promise(resolve => setTimeout(resolve, 50));
assert(events.some(event => event.registrationId === registration.registrationId), '15 秒内没有收到匹配的快捷键事件');
return events;
} finally {
try { if (registration) await call('host.shortcut', 'unregister', registration); }
finally { await subscription.close(); }
}
}, 'pass');
manual('列出正在运行的应用', () => call('host.application', 'listRunning'), 'pass');
manual('检查随底座启动的服务', async () => {
const service = 'dev.trove.qa.autostart.probe';
// Discovery does not start the backend; keep this separate from info().
return call('host.services', 'getStatus', { service });
});
manual('关闭本窗口', () => call('host.window', 'close'));
}
if (config.mode === 'backend') {
manual('读取后端 PID 与活动数', () => call(config.service, 'info'), 'pass');
if (config.service !== '@self') manual('查询提供方状态(不启动)', () => call('host.services', 'getStatus', { service: config.service }));
if (config.directory === 'test-crash') manual('触发本后端崩溃(exit 42)', async () => {
try { await call('@self', 'crash', {}, { timeoutMs: 6000 }); }
catch (error) {
assert(['provider.disconnected', 'provider.crashed'].includes(error.code), `预期进程断开,实际 ${error.code}`);
return { error: serializeError(error), next: '再次读取 PID 验证恢复;短时间反复触发可观察熔断。' };
}
throw new Error('后端未断开');
});
}
if (config.mode === 'upgrade') {
manual('写入升级基线标记', async () => {
const value = { token: crypto.randomUUID(), writtenByVersion: trove.context.curioVersion, writtenAt: new Date().toISOString() };
await call('host.storage', 'set', { key: 'qa.upgrade-marker', value });
return value;
}, 'pass');
}
}
$('#run').onclick = () => exclusive(async () => {
results.length = 0;
if (config.mode === 'host') for (const [name, action] of hostChecks(trove)) await check(name, action);
if (config.mode === 'backend') for (const [name, action] of backendChecks(trove, config.service)) await check(name, action);
if (config.mode === 'dependency') {
await check('缺失依赖时 Host 仍可用', async () => equal(await call('host.runtime', 'echo', { alive: true }), { alive: true }));
await check('依赖调用返回预期错误', () => expectError(() => call(config.service, 'echo'), config.expectedError));
}
if (config.mode === 'fault') {
await check('启动失败被限制在截止时间内', async () => {
try { await call('@self', 'info', {}, { timeoutMs: 6000 }); }
catch (error) {
const expected = config.directory === 'test-start-timeout'
? ['provider.start_timeout']
: ['provider.start_failed', 'provider.disconnected', 'provider.crashed', 'provider.start_timeout'];
assert(expected.includes(error.code), `非预期错误 ${error.code}`);
return { actual: serializeError(error), next: '查看底座后端日志确认具体启动原因。' };
}
throw new Error('故障后端意外启动成功');
});
}
if (config.mode === 'upgrade') {
await check('运行版本与打包版本一致', async () => { equal(trove.context.curioVersion, config.version); return trove.context; });
await check('升级基线标记保留', async () => {
const marker = await call('host.storage', 'get', { key: 'qa.upgrade-marker' });
assert(marker.found, '尚无基线标记,请在 1.0.0 中写入后再验证升级');
equal(marker.value.writtenByVersion, '1.0.0', '标记不是由基线版本写入');
return marker.value;
});
}
});
$('#save-report').onclick = () => exclusive(() => check('保存 JSON 报告', async () => {
const paths = await call('host.runtime', 'getPaths');
const path = `${paths.data}/qa-report-${Date.now()}.json`;
await call('host.fs', 'writeText', { path, content: JSON.stringify(report(), null, 2) });
return { path };
}));
try {
await trove.ready();
$('#context').textContent = `${trove.context.curioId} · v${trove.context.curioVersion} · ${trove.context.mode} · ${trove.context.instanceId}`;
configureManualActions();
document.querySelectorAll('button').forEach(button => { button.disabled = false; });
$('#status').textContent = '已连接真实 Trove Host,点击运行常规检查。';
} catch (error) {
$('#status').textContent = `连接失败:${error.code ?? ''} ${error.message}。请从 Trove 安装并打开本插件。`;
}
+33
View File
@@ -0,0 +1,33 @@
:root { color-scheme: light dark; font: 14px/1.6 -apple-system, BlinkMacSystemFont, sans-serif; color: #203129; background: #f3f5f1; }
* { box-sizing: border-box; }
body { max-width: 1080px; margin: auto; padding: 28px; }
header { margin-bottom: 24px; }
.eyebrow { font-size: 11px; font-weight: 650; letter-spacing: 2px; color: #53745e; }
h1 { font-size: 27px; line-height: 1.3; margin: 8px 0; }
h2 { font-size: 16px; margin: 0 0 8px; }
p { margin: 8px 0; }
#context, .hint { color: #626f66; overflow-wrap: anywhere; }
section, details { padding: 20px; background: #fff; border: 1px solid #dce3db; border-radius: 12px; margin-bottom: 16px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin: 14px 0; }
button { font: inherit; color: inherit; background: #f1f5f0; border: 1px solid #cbd7ca; border-radius: 7px; padding: 8px 12px; cursor: pointer; }
#run { background: #286047; border-color: #286047; color: #fff; }
button:hover { filter: brightness(.94); }
button:disabled { opacity: .5; cursor: default; }
button:focus-visible, summary:focus-visible { outline: 3px solid #629d78; outline-offset: 3px; }
ol { list-style: none; margin: 0; padding: 0; }
li { padding: 12px 0; border-bottom: 1px solid #e7ece5; }
li:last-child { border: 0; }
.pass { color: #236b3b; } .fail { color: #ba352d; } .observed, .skip { color: #8c5e15; }
.result-heading { display: flex; gap: 12px; justify-content: space-between; }
small { color: #68746b; }
pre { white-space: pre-wrap; overflow-wrap: anywhere; font: 12px/1.6 ui-monospace, monospace; max-height: 360px; overflow: auto; }
summary { cursor: pointer; }
@media (max-width: 640px) { body { padding: 14px; } section { padding: 14px; } .result-heading { display: block; } }
@media (prefers-color-scheme: dark) {
:root { color: #e1e9df; background: #172019; }
section, details { background: #202b23; border-color: #37473a; }
button { background: #2d3c30; border-color: #506153; }
#context, .hint, small { color: #a4b4a6; }
li { border-color: #37473a; }
.pass { color: #8adb9e; } .fail { color: #ff9c92; } .observed, .skip { color: #edc579; }
}