'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'), { NativeSchedules } = require('../../src/modules/assistant/native-schedules.cjs'), { OperationJournal } = require('../../src/modules/assistant/operation-journal.cjs'); async function fixture(t, browser = false) { require('../fixtures/isolated-vault-key.cjs')(t); const f = await managementFixture(t, { browser }), file = path.join(os.tmpdir(), 'task-manager-' + 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 }); return f; } test( 'host catalog shows own tasks across sessions and never another owners tasks', { timeout: 90000 }, async (t) => { const f = await fixture(t), cookie = await f.login(), other = await f.login('viewer'), user = f.users.users().find((u) => u.username === 'admin'), c1 = f.store.create(user.id), c2 = f.store.create(user.id), principal = { user, sessionId: 'test-admin' }; for (const c of [c1, c2]) await f.assistant.schedules.createTask(c.id, principal, { confirm: true, request: { title: '任务 ' + c.id, prompt: 'check', after_seconds: 3600 }, }); assert.equal((await f.call('/assistant/schedules')).status, 401); const response = await f.call('/assistant/schedules', undefined, cookie); assert.equal(response.status, 200); const result = await response.json(); assert.equal(result.tasks.length, 2); assert.deepEqual(new Set(result.tasks.map((x) => x.sessionId)), new Set([c1.id, c2.id])); assert.equal(result.conversations.length, 2); assert.ok(result.tasks.every((x) => typeof x.conversationTitle === 'string')); assert.equal( (await (await f.call('/assistant/schedules', undefined, other)).json()).tasks.length, 0, ); }, ); test( 'task manager exposes cross-session search detail create edit delete and preserves chat draft', { timeout: 90000 }, async (t) => { const f = await fixture(t, true), { page, store, assistant } = f, user = f.users.users().find((u) => u.username === 'admin'), c1 = store.create(user.id), c2 = store.create(user.id), principal = { user, sessionId: 'test-admin' }; store.saveSettings({ ...store.settings(), runtimePath: process.execPath, apiKey: 'TEST_TASK_MANAGER_KEY', }); const t1 = await assistant.schedules.createTask(c1.id, principal, { confirm: true, request: { title: '别的会话任务甲', prompt: '甲的提醒内容', after_seconds: 3600 }, }); await assistant.schedules.createTask(c2.id, principal, { confirm: true, request: { title: '任务乙', prompt: '乙的提醒内容', after_seconds: 7200 }, }); await f.open(); await page.getByLabel('发送给 DSH', { exact: true }).fill('未发送的原始草稿'); await page.getByRole('button', { name: '定时任务', exact: true }).click(); const manager = page.getByRole('region', { name: '定时任务管理', exact: true }); await manager.waitFor(); await manager .locator('[data-task-list]') .getByText('别的会话任务甲', { exact: true }) .waitFor(); await manager.locator('[data-task-list]').getByText('任务乙', { exact: true }).waitFor(); await manager.getByRole('button', { name: '已结束', exact: true }).click(); await manager.getByText('没有匹配的任务', { exact: true }).waitFor(); await manager.getByRole('button', { name: '进行中', exact: true }).click(); await manager.locator('[data-task-list]').getByText('任务乙', { exact: true }).waitFor(); await manager.getByLabel('搜索定时任务', { exact: true }).fill('甲'); await manager .locator('[data-task-list]') .getByText('任务乙', { exact: true }) .waitFor({ state: 'hidden' }); await manager.getByRole('button', { name: '查看任务 别的会话任务甲', exact: true }).click(); const details = page.getByRole('dialog', { name: '定时任务详情', exact: true }); await details.getByText(t1.id, { exact: true }).waitFor(); await details.getByText('甲的提醒内容', { exact: true }).waitFor(); await details.getByRole('button', { name: '关闭', exact: true }).click(); await manager.getByRole('button', { name: '编辑任务 别的会话任务甲', exact: true }).click(); let form = page.getByRole('dialog', { name: '编辑定时任务', exact: true }); await form.getByLabel('任务名称', { exact: true }).fill('跨会话改名甲'); await form.getByRole('checkbox').check(); await form.getByRole('button', { name: '确认保存', exact: true }).click(); await manager.locator('[data-task-list]').getByText('跨会话改名甲', { exact: true }).waitFor(); assert.equal((await assistant.schedules.list(c1.id, principal)).tasks[0].title, '跨会话改名甲'); await manager.getByRole('button', { name: '删除任务 跨会话改名甲', exact: true }).click(); await page .getByRole('dialog', { name: '删除定时任务', exact: true }) .getByRole('button', { name: '确认删除任务', exact: true }) .click(); await manager .locator('[data-task-list]') .getByText('跨会话改名甲', { exact: true }) .waitFor({ state: 'hidden' }); await manager.getByLabel('搜索定时任务', { exact: true }).fill(''); await manager.getByRole('button', { name: '新建定时任务', exact: true }).click(); form = page.getByRole('dialog', { name: '新建定时任务', exact: true }); await form.getByLabel('所属会话', { exact: true }).selectOption(c1.id); await form.getByLabel('任务名称', { exact: true }).fill('面板新建任务'); await form.getByLabel('任务内容', { exact: true }).fill('新任务内容'); await form.getByRole('checkbox').check(); await form.getByRole('button', { name: '确认保存', exact: true }).click(); await manager.locator('[data-task-list]').getByText('面板新建任务', { exact: true }).waitFor(); assert.equal((await assistant.schedules.list(c1.id, principal)).tasks[0].title, '面板新建任务'); await page.route('**/api/assistant/schedules', (r) => r.fulfill({ status: 503, contentType: 'application/json', body: JSON.stringify({ error: 'fixture unavailable' }), }), ); await manager.getByRole('button', { name: '刷新任务', exact: true }).click(); await manager .getByRole('status') .getByText(/任务列表读取失败/) .waitFor(); assert.equal( await manager.getByRole('button', { name: '新建定时任务', exact: true }).isDisabled(), true, ); assert.equal( await manager .getByRole('button', { name: '编辑任务 面板新建任务', exact: true }) .isDisabled(), true, ); await manager.locator('[data-task-list]').getByText('面板新建任务', { exact: true }).waitFor(); await page.unroute('**/api/assistant/schedules'); await manager.getByRole('button', { name: '刷新任务', exact: true }).click(); await page.waitForFunction(() => !document.querySelector('[data-task-create]').disabled); const output = path.resolve(__dirname, '../../output/verification/dsh-schedule-manager'); fs.mkdirSync(output, { recursive: true }); await page.screenshot({ path: path.join(output, 'desktop.png'), fullPage: true }); await page.setViewportSize({ width: 390, height: 844 }); assert.equal( await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth), true, ); await page.screenshot({ path: path.join(output, 'mobile.png'), fullPage: true }); await manager.getByRole('button', { name: '返回对话', exact: true }).click(); assert.equal( await page.getByLabel('发送给 DSH', { exact: true }).inputValue(), '未发送的原始草稿', ); }, );