105 lines
3.5 KiB
TypeScript
105 lines
3.5 KiB
TypeScript
import { beforeEach, describe, expect, it } from 'vitest';
|
|
import { useTaskCenterStore } from '@/stores/task-center';
|
|
|
|
const binding = {
|
|
cronJobId: 'cron-1',
|
|
quickTask: {
|
|
id: 'qt-1',
|
|
name: '写报告',
|
|
description: '生成报告',
|
|
skills: [{ id: 'docx', name: 'docx' }],
|
|
},
|
|
taskContent: '每天生成日报',
|
|
composedMessage: '使用docx skill 每天生成日报',
|
|
createdAt: 1,
|
|
updatedAt: 1,
|
|
};
|
|
|
|
describe('task center store', () => {
|
|
beforeEach(() => {
|
|
window.localStorage.clear();
|
|
useTaskCenterStore.setState({
|
|
scheduledBindings: {},
|
|
runRecords: [],
|
|
pinnedTaskIds: [],
|
|
});
|
|
});
|
|
|
|
it('upserts and deletes scheduled task bindings by cron job id', () => {
|
|
useTaskCenterStore.getState().upsertScheduledBinding(binding);
|
|
expect(useTaskCenterStore.getState().scheduledBindings['cron-1']).toMatchObject({
|
|
cronJobId: 'cron-1',
|
|
taskContent: '每天生成日报',
|
|
});
|
|
expect(JSON.parse(window.localStorage.getItem('yinian:scheduled-task-bindings') || '{}')['cron-1']).toMatchObject({
|
|
cronJobId: 'cron-1',
|
|
});
|
|
|
|
useTaskCenterStore.getState().upsertScheduledBinding({
|
|
...binding,
|
|
taskContent: '每周生成周报',
|
|
updatedAt: 2,
|
|
});
|
|
expect(useTaskCenterStore.getState().scheduledBindings['cron-1']?.taskContent).toBe('每周生成周报');
|
|
expect(useTaskCenterStore.getState().scheduledBindings['cron-1']?.updatedAt).toBe(2);
|
|
|
|
useTaskCenterStore.getState().deleteScheduledBinding('cron-1');
|
|
expect(useTaskCenterStore.getState().scheduledBindings['cron-1']).toBeUndefined();
|
|
expect(JSON.parse(window.localStorage.getItem('yinian:scheduled-task-bindings') || '{}')['cron-1']).toBeUndefined();
|
|
});
|
|
|
|
it('adds, updates, and trims run records', () => {
|
|
useTaskCenterStore.getState().addRunRecord({
|
|
id: 'run-1',
|
|
source: 'manual',
|
|
status: 'submitted',
|
|
quickTask: binding.quickTask,
|
|
quickTaskName: binding.quickTask.name,
|
|
skillNames: ['docx'],
|
|
taskContent: binding.taskContent,
|
|
composedMessage: binding.composedMessage,
|
|
sessionKey: 'agent:main:task-1',
|
|
startedAt: 1,
|
|
});
|
|
|
|
useTaskCenterStore.getState().updateRunRecord('run-1', {
|
|
status: 'failed',
|
|
error: 'timeout',
|
|
finishedAt: 2,
|
|
});
|
|
|
|
expect(useTaskCenterStore.getState().runRecords[0]).toMatchObject({
|
|
id: 'run-1',
|
|
status: 'failed',
|
|
error: 'timeout',
|
|
finishedAt: 2,
|
|
});
|
|
|
|
for (let index = 0; index < 130; index += 1) {
|
|
useTaskCenterStore.getState().addRunRecord({
|
|
id: `run-${index + 2}`,
|
|
source: 'scheduled',
|
|
status: 'submitted',
|
|
quickTaskName: '巡检',
|
|
skillNames: [],
|
|
taskContent: '检查状态',
|
|
composedMessage: '检查状态',
|
|
startedAt: index + 3,
|
|
});
|
|
}
|
|
|
|
expect(useTaskCenterStore.getState().runRecords).toHaveLength(120);
|
|
expect(JSON.parse(window.localStorage.getItem('yinian:task-run-records') || '[]')).toHaveLength(120);
|
|
});
|
|
|
|
it('toggles pinned task ids for sidebar quick triggers', () => {
|
|
useTaskCenterStore.getState().togglePinnedTask('cron-1');
|
|
useTaskCenterStore.getState().togglePinnedTask('cron-2');
|
|
expect(useTaskCenterStore.getState().pinnedTaskIds).toEqual(['cron-1', 'cron-2']);
|
|
expect(JSON.parse(window.localStorage.getItem('yinian:pinned-task-ids') || '[]')).toEqual(['cron-1', 'cron-2']);
|
|
|
|
useTaskCenterStore.getState().togglePinnedTask('cron-1');
|
|
expect(useTaskCenterStore.getState().pinnedTaskIds).toEqual(['cron-2']);
|
|
});
|
|
});
|