220 lines
8.0 KiB
JavaScript
220 lines
8.0 KiB
JavaScript
'use strict';
|
|
const { test } = require('node:test'),
|
|
assert = require('node:assert/strict'),
|
|
fs = require('node:fs'),
|
|
os = require('node:os'),
|
|
path = require('node:path'),
|
|
crypto = require('node:crypto');
|
|
const { managementFixture } = require('./management-fixture.cjs');
|
|
const { OperationJournal } = require('../../src/modules/assistant/operation-journal.cjs');
|
|
const { NativeSchedules } = require('../../src/modules/assistant/native-schedules.cjs');
|
|
const until = async (predicate) => {
|
|
const end = Date.now() + 6000;
|
|
while (!(await predicate())) {
|
|
if (Date.now() > end) throw Error('Schedule condition did not settle');
|
|
await new Promise((r) => setTimeout(r, 20));
|
|
}
|
|
};
|
|
test(
|
|
'native DSH reminders persist, deliver once to original owner and retain approval boundaries',
|
|
{ timeout: 90000 },
|
|
async (t) => {
|
|
require('../fixtures/isolated-vault-key.cjs')(t);
|
|
const f = await managementFixture(t),
|
|
{ store, assistant, users } = f,
|
|
file = path.join(os.tmpdir(), 'schedule-audit-' + crypto.randomUUID() + '.sqlite'),
|
|
journal = new OperationJournal({ file });
|
|
assistant.journal = journal;
|
|
t.after(() => {
|
|
journal.close();
|
|
fs.rmSync(file, { force: true });
|
|
});
|
|
store.saveSettings({
|
|
...store.settings(),
|
|
runtimePath: process.execPath,
|
|
apiKey: 'TEST_NATIVE_SCHEDULE_KEY',
|
|
allowActions: true,
|
|
});
|
|
let modelCalls = 0,
|
|
operations = 0;
|
|
assistant.deps.storage = {
|
|
store: {
|
|
servers: () => [{ id: 'server', name: 'Fixture', enabled: true, state: {} }],
|
|
archives: () => [],
|
|
},
|
|
check: async () => {
|
|
operations++;
|
|
},
|
|
};
|
|
assistant.runtime.chat = async () => {
|
|
modelCalls++;
|
|
return JSON.stringify({
|
|
reply: '定时提醒已收到。',
|
|
actions: [{ kind: 'check_storage', targetId: 'server' }],
|
|
});
|
|
};
|
|
const principal = {
|
|
user: users.users().find((u) => u.username === 'admin'),
|
|
sessionId: 'interactive-admin',
|
|
},
|
|
c = store.create(principal.user.id);
|
|
let schedules = await NativeSchedules.create({ assistant });
|
|
assistant.schedules = schedules;
|
|
await assert.rejects(
|
|
schedules.createTask(c.id, principal, {
|
|
request: { title: '未确认', prompt: 'test', after_seconds: 60 },
|
|
}),
|
|
(e) => e.status === 403,
|
|
);
|
|
assert.equal((await schedules.list(c.id, principal)).tasks.length, 0);
|
|
await assert.rejects(
|
|
schedules.createTask(c.id, principal, {
|
|
confirm: true,
|
|
request: { title: '过频', prompt: 'test', every_seconds: 30 },
|
|
}),
|
|
(e) => e.status === 400,
|
|
);
|
|
const daily = await schedules.createTask(c.id, principal, {
|
|
confirm: true,
|
|
request: {
|
|
title: '北京时间每日',
|
|
prompt: '查看状态',
|
|
daily: { time: '09:00:00', time_zone: 'Asia/Shanghai' },
|
|
},
|
|
});
|
|
assert.equal(daily.kind, 'daily');
|
|
assert.equal(daily.timeZone, 'Asia/Shanghai');
|
|
await assert.rejects(
|
|
schedules.list(c.id, {
|
|
user: users.users().find((u) => u.username === 'viewer'),
|
|
sessionId: 'viewer',
|
|
}),
|
|
(e) => e.status === 404,
|
|
);
|
|
const updated = await schedules.updateTask(c.id, daily.id, principal, {
|
|
confirm: true,
|
|
expected: daily,
|
|
changes: { title: '改名' },
|
|
});
|
|
assert.equal(updated.record.title, '改名');
|
|
await assert.rejects(
|
|
schedules.updateTask(c.id, daily.id, principal, {
|
|
confirm: true,
|
|
expected: daily,
|
|
changes: { title: '过期修改' },
|
|
}),
|
|
(e) => e.status === 409,
|
|
);
|
|
const task = await schedules.createTask(c.id, principal, {
|
|
confirm: true,
|
|
request: {
|
|
title: '一次原生提醒',
|
|
prompt: '检查状态并提出建议,不执行操作',
|
|
after_seconds: 1,
|
|
},
|
|
});
|
|
await until(async () => {
|
|
const list = await schedules.list(c.id, principal);
|
|
return list.history.some((h) => h.taskId === task.id && h.status === 'success');
|
|
});
|
|
assert.equal(modelCalls, 1);
|
|
assert.equal(operations, 0);
|
|
const turns = store.conversation(c.id, principal.user.id).turns;
|
|
assert.equal(turns.at(-1).actions[0].status, 'proposed');
|
|
await schedules.stop();
|
|
const row = store.db
|
|
.prepare('SELECT payload FROM assistant_schedule_tasks WHERE id=?')
|
|
.get(task.id),
|
|
old = store.decode('schedule-task:' + task.id, row.payload),
|
|
lostAck = {
|
|
sessionId: old.sessionId,
|
|
record: old.record,
|
|
status: 'active',
|
|
deliveryHistory: { records: [], earlierRecordsUnavailable: false },
|
|
};
|
|
store.db
|
|
.prepare('UPDATE assistant_schedule_tasks SET payload=? WHERE id=?')
|
|
.run(store.encode('schedule-task:' + task.id, lostAck), task.id);
|
|
schedules = await NativeSchedules.create({ assistant });
|
|
assistant.schedules = schedules;
|
|
await until(
|
|
async () =>
|
|
(await schedules.list(c.id, principal)).tasks.find((x) => x.id === task.id)?.status ===
|
|
'inactive',
|
|
);
|
|
assert.equal(modelCalls, 1);
|
|
await schedules.deleteTask(c.id, daily.id, principal, { confirm: true });
|
|
assert.equal(
|
|
(await schedules.list(c.id, principal)).tasks.some((x) => x.id === daily.id),
|
|
false,
|
|
);
|
|
const at = new Date(Date.now() + 1500).toISOString();
|
|
const paired = await Promise.all(
|
|
['甲', '乙'].map((name) =>
|
|
schedules.createTask(c.id, principal, {
|
|
confirm: true,
|
|
request: { title: '同刻原生提醒' + name, prompt: 'check ' + name, at },
|
|
}),
|
|
),
|
|
);
|
|
await until(async () => {
|
|
const h = (await schedules.list(c.id, principal)).history;
|
|
return paired.every((t) => h.some((r) => r.taskId === t.id && r.status === 'success'));
|
|
});
|
|
assert.equal(modelCalls, 3);
|
|
assert.equal(operations, 0);
|
|
for (const t of paired) await schedules.deleteTask(c.id, t.id, principal, { confirm: true });
|
|
for (const request of [
|
|
{ title: '原生间隔', prompt: 'check', every_seconds: 3600 },
|
|
{
|
|
title: '原生Cron',
|
|
prompt: 'check',
|
|
cron: { expression: '0 9 * * *', time_zone: 'Asia/Shanghai' },
|
|
},
|
|
]) {
|
|
const result = await schedules.createTask(c.id, principal, { confirm: true, request });
|
|
assert.ok(['every', 'cron'].includes(result.kind));
|
|
await schedules.deleteTask(c.id, result.id, principal, { confirm: true });
|
|
}
|
|
const originalRecord = assistant.record.bind(assistant);
|
|
assistant.record = (id, actor, step, status, detail) => {
|
|
if (step === 'schedule_create' && status === 'success')
|
|
throw Error('audit failed after save');
|
|
return originalRecord(id, actor, step, status, detail);
|
|
};
|
|
await assert.rejects(
|
|
schedules.createTask(c.id, principal, {
|
|
confirm: true,
|
|
request: { title: '核实未知结果', prompt: 'check', after_seconds: 3600 },
|
|
}),
|
|
(e) => e.status === 502 && e.mutationApplied === true,
|
|
);
|
|
assistant.record = originalRecord;
|
|
const uncertain = (await schedules.list(c.id, principal)).tasks.filter(
|
|
(t) => t.title === '核实未知结果',
|
|
);
|
|
assert.equal(uncertain.length, 1);
|
|
await schedules.deleteTask(c.id, uncertain[0].id, principal, { confirm: true });
|
|
for (let i = 0; i < 29; i++)
|
|
await schedules.createTask(c.id, principal, {
|
|
confirm: true,
|
|
request: { title: '并发限制' + i, prompt: 'check', after_seconds: 3600 },
|
|
});
|
|
const raced = await Promise.allSettled(
|
|
[0, 1].map((i) =>
|
|
schedules.createTask(c.id, principal, {
|
|
confirm: true,
|
|
request: { title: '竞争' + i, prompt: 'check', after_seconds: 3600 },
|
|
}),
|
|
),
|
|
);
|
|
assert.equal(raced.filter((r) => r.status === 'fulfilled').length, 1);
|
|
assert.equal((await schedules.native.list({ sessionId: c.id })).length, 30);
|
|
await schedules.stopSession(c.id);
|
|
assert.equal(
|
|
(await schedules.list(c.id, principal)).tasks.some((x) => x.status === 'active'),
|
|
false,
|
|
);
|
|
},
|
|
);
|