'use strict'; const { test } = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const os = require('node:os'); const crypto = require('node:crypto'); const { managementFixture } = require('./management-fixture.cjs'); const { readRevisions } = require('../../src/shared/refresh-revisions.cjs'); const { NativeSchedules } = require('../../src/modules/assistant/native-schedules.cjs'); const { OperationJournal } = require('../../src/modules/assistant/operation-journal.cjs'); test('main refresh revisions follow DSH providers, extra quota calls and private native tasks', async (t) => { require('../fixtures/isolated-vault-key.cjs')(t); const f = await managementFixture(t); const user = f.users.users().find((u) => u.username === 'admin'); const viewer = f.users.users().find((u) => u.username === 'viewer'); const ownerScope = 'owner:' + user.id; const day = f.store.usage().today; const scopes = [ownerScope, 'config', 'usage:' + day]; const read = () => readRevisions(f.store.db, scopes); let before = read(); f.store.create(viewer.id); assert.equal(read()[ownerScope], before[ownerScope]); const conversation = f.store.create(user.id); assert.ok(read()[ownerScope] > before[ownerScope]); before = read(); const provider = f.store.providers.read('deepseek'); f.store.providers.save({ ...provider, name: 'Changed provider' }); assert.ok(read().config > before.config); const turn = f.store.beginTurn(conversation.id, user.id, 'Quota fixture'); before = read(); f.store.reserveModelCall(turn.id, 1); assert.ok(read()['usage:' + day] > before['usage:' + day]); f.store.updateTurn(turn.id, 'success', { reply: 'Done' }); const journalFile = path.join(os.tmpdir(), 'dsh-refresh-' + crypto.randomUUID() + '.sqlite'); f.assistant.journal = new OperationJournal({ file: journalFile }); t.after(() => { f.assistant.journal.close(); fs.rmSync(journalFile, { force: true }); }); await NativeSchedules.create({ assistant: f.assistant }); before = read(); const task = await f.assistant.schedules.createTask( conversation.id, { user, sessionId: 'integration-session' }, { confirm: true, request: { title: 'Native task', prompt: 'Read only', after_seconds: 3600 } }, ); assert.ok(read()[ownerScope] > before[ownerScope]); assert.equal( (await f.assistant.schedules.catalog({ user: viewer, sessionId: 'viewer' })).tasks.length, 0, ); await f.assistant.schedules.deleteTask( conversation.id, task.id, { user, sessionId: 'integration-session' }, { confirm: true }, ); }); test('main lazy assistant bundle includes schedules, attachments and approval dependencies', () => { const manifest = JSON.parse( fs.readFileSync( path.resolve(__dirname, '../../web/modules/navigation/module-manifest.json'), 'utf8', ), ); assert.deepEqual(manifest.assistant, ['$assistant']); assert.deepEqual(manifest['assistant-settings'], ['$assistant']); const files = manifest.$assistant; for (const dependency of [ 'assistant-schedules.js', 'assistant-schedule-manager.js', 'assistant-attachments.js', 'assistant-settings-review.js', ]) { assert.ok(files.includes(dependency), dependency); assert.ok(files.indexOf(dependency) < files.indexOf('assistant.js')); } assert.ok(files.indexOf('assistant-schedules.js') < files.indexOf('assistant-context-panel.js')); }); test('main manual refresh updates DSH permissions, quota and model catalogue without losing drafts', async (t) => { require('../fixtures/isolated-vault-key.cjs')(t); const f = await managementFixture(t, { browser: true }); f.store.saveSettings({ ...f.store.settings(), runtimePath: process.execPath, apiKey: 'TEST_REFRESH_KEY', allowActions: false, }); const user = f.users.users().find((u) => u.username === 'admin'); const c = f.store.create(user.id); const turn = f.store.beginTurn(c.id, user.id, 'Refresh fixture'); f.store.updateTurn(turn.id, 'success', { reply: 'Existing answer', actions: [ { id: 'refresh-action', kind: 'check_storage', targetId: 'server', label: '检查存储连接', status: 'proposed', }, ], }); await f.open(); await f.page.getByRole('button', { name: '查看操作(1)', exact: true }).click(); const action = f.page.getByRole('button', { name: '执行:检查存储连接', exact: true }); assert.equal(await action.isDisabled(), true); await f.page.getByLabel('发送给 DSH', { exact: true }).fill('Keep my draft'); f.store.saveSettings({ version: f.store.settings().version, allowActions: true, dailyLimit: 50 }); f.store.providers.save({ ...f.store.providers.read('deepseek'), models: [ { id: 'deepseek-flash', name: 'Original model' }, { id: 'new-model', name: 'Fresh model' }, ], }); await f.page.evaluate(() => PageLifecycle.refreshRegions('assistant')); assert.equal(await action.isEnabled(), true); assert.match(await f.page.locator('.aw-usage').innerText(), /50/); assert.equal( await f.page.getByLabel('发送给 DSH', { exact: true }).inputValue(), 'Keep my draft', ); await f.page.locator('#assistant-model-picker').click(); await f.page.getByRole('option', { name: 'Fresh model', exact: true }).waitFor(); });