133 lines
4.3 KiB
JavaScript
133 lines
4.3 KiB
JavaScript
'use strict';
|
|
const { test } = require('node:test'),
|
|
assert = require('node:assert/strict'),
|
|
fs = require('node:fs'),
|
|
path = require('node:path'),
|
|
os = require('node:os'),
|
|
crypto = require('node:crypto');
|
|
const { managementFixture } = require('./management-fixture.cjs'),
|
|
{ NativeSchedules } = require('../../src/modules/assistant/native-schedules.cjs'),
|
|
{ OperationJournal } = require('../../src/modules/assistant/operation-journal.cjs');
|
|
test(
|
|
'schedule HTTP uses native rules, requires confirmation and protects ownership and update versions',
|
|
{ timeout: 90000 },
|
|
async (t) => {
|
|
require('../fixtures/isolated-vault-key.cjs')(t);
|
|
const f = await managementFixture(t),
|
|
file = path.join(os.tmpdir(), 'native-http-' + crypto.randomUUID() + '.sqlite');
|
|
f.assistant.journal = new OperationJournal({ file });
|
|
t.after(() => {
|
|
f.assistant.journal.close();
|
|
fs.rmSync(file, { force: true });
|
|
});
|
|
await NativeSchedules.create({ assistant: f.assistant });
|
|
const admin = await f.login(),
|
|
viewer = await f.login('viewer'),
|
|
c = await (await f.call('/assistant/conversations', {}, admin)).json(),
|
|
base = '/assistant/conversations/' + c.id + '/schedules';
|
|
assert.equal((await f.call(base)).status, 401);
|
|
assert.equal((await f.call(base, undefined, viewer)).status, 404);
|
|
assert.equal(
|
|
(
|
|
await f.call(
|
|
base,
|
|
{ request: { title: '未确认', prompt: '检查', after_seconds: 600 } },
|
|
admin,
|
|
)
|
|
).status,
|
|
403,
|
|
);
|
|
const created = await f.call(
|
|
base,
|
|
{
|
|
confirm: true,
|
|
request: {
|
|
title: '原生每周提醒',
|
|
prompt: '检查状态',
|
|
weekly: { time: '09:00:00', time_zone: 'Asia/Shanghai', weekdays: [1, 3, 5] },
|
|
},
|
|
},
|
|
admin,
|
|
);
|
|
assert.equal(created.status, 201);
|
|
const task = await created.json();
|
|
assert.equal(task.kind, 'weekly');
|
|
let listed = await (await f.call(base, undefined, admin)).json();
|
|
assert.equal(listed.tasks.length, 1);
|
|
assert.equal(listed.history.length, 0);
|
|
assert.equal(
|
|
(
|
|
await f.call(
|
|
base + '/' + task.id + '/update',
|
|
{ confirm: true, expected: task, changes: { title: '新标题' } },
|
|
admin,
|
|
)
|
|
).status,
|
|
200,
|
|
);
|
|
assert.equal(
|
|
(
|
|
await f.call(
|
|
base + '/' + task.id + '/update',
|
|
{ confirm: true, expected: task, changes: { title: '旧版本修改' } },
|
|
admin,
|
|
)
|
|
).status,
|
|
409,
|
|
);
|
|
assert.equal((await f.call(base + '/' + task.id + '/delete', {}, admin)).status, 403);
|
|
assert.equal(
|
|
(await f.call(base + '/' + task.id + '/delete', { confirm: true }, admin)).status,
|
|
200,
|
|
);
|
|
listed = await (await f.call(base, undefined, admin)).json();
|
|
assert.equal(listed.tasks.length, 0);
|
|
await f.call(
|
|
base,
|
|
{ confirm: true, request: { title: '延续前任务', prompt: '检查', after_seconds: 3600 } },
|
|
admin,
|
|
);
|
|
await f.call('/assistant/conversations/' + c.id + '/continue', { version: c.version }, admin);
|
|
assert.equal((await (await f.call(base, undefined, admin)).json()).tasks.length, 0);
|
|
const current = await (
|
|
await f.call('/assistant/conversations/' + c.id, undefined, admin)
|
|
).json();
|
|
const trashed = await (
|
|
await f.call(
|
|
'/assistant/conversations/' + c.id + '/delete',
|
|
{ version: current.version },
|
|
admin,
|
|
)
|
|
).json();
|
|
f.store.db
|
|
.prepare(
|
|
'INSERT INTO assistant_schedule_inbox(id,conversation_id,owner,task_id,status,payload,created_at) VALUES(?,?,?,?,?,?,?)',
|
|
)
|
|
.run(
|
|
'purge-test',
|
|
c.id,
|
|
f.store.db.prepare('SELECT owner FROM conversations WHERE id=?').get(c.id).owner,
|
|
'old',
|
|
'success',
|
|
f.store.encode('schedule-inbox:purge-test', { prompt: 'private prompt' }),
|
|
new Date().toISOString(),
|
|
);
|
|
assert.equal(
|
|
(
|
|
await f.call(
|
|
'/assistant/conversations/' + c.id + '/purge',
|
|
{ version: trashed.version, confirm: 'delete' },
|
|
admin,
|
|
)
|
|
).status,
|
|
200,
|
|
);
|
|
assert.equal(
|
|
f.store.db
|
|
.prepare('SELECT COUNT(*) n FROM assistant_schedule_inbox WHERE conversation_id=?')
|
|
.get(c.id).n,
|
|
0,
|
|
);
|
|
},
|
|
);
|