fix(scheduler): run post-completion system actions

This commit is contained in:
NimBold
2026-07-04 17:24:16 +03:30
parent 4a322196de
commit f726b058f7
4 changed files with 186 additions and 62 deletions
+42
View File
@@ -0,0 +1,42 @@
import { describe, expect, it } from 'vitest';
import type { DownloadItem } from '../bindings/DownloadItem';
import { schedulerCompletionState } from './schedulerCompletion';
const download = (id: string, status: DownloadItem['status']): DownloadItem => ({
id,
url: `https://example.com/${id}`,
fileName: `${id}.bin`,
status,
size: '0 B',
category: 'Other',
dateAdded: new Date().toISOString(),
destination: '/tmp',
queueId: 'queue',
});
describe('schedulerCompletionState', () => {
it('stays active while any tracked scheduler download can still progress', () => {
expect(schedulerCompletionState([
download('a', 'completed'),
download('b', 'retrying'),
], ['a', 'b'])).toBe('active');
});
it('completes only when every tracked scheduler download completed', () => {
expect(schedulerCompletionState([
download('a', 'completed'),
download('b', 'completed'),
], ['a', 'b'])).toBe('completed');
});
it('treats failed or missing tracked downloads as incomplete', () => {
expect(schedulerCompletionState([
download('a', 'completed'),
download('b', 'failed'),
], ['a', 'b'])).toBe('incomplete');
expect(schedulerCompletionState([
download('a', 'completed'),
], ['a', 'missing'])).toBe('incomplete');
});
});
+21
View File
@@ -0,0 +1,21 @@
import type { DownloadItem } from '../bindings/DownloadItem';
import { isActiveDownloadStatus } from './downloads';
export type SchedulerCompletionState = 'active' | 'completed' | 'incomplete';
export const schedulerCompletionState = (
downloads: DownloadItem[],
schedulerActiveDownloadIds: string[],
): SchedulerCompletionState => {
const scheduledItems = schedulerActiveDownloadIds.map(id =>
downloads.find(download => download.id === id)
);
if (scheduledItems.some(item => item && isActiveDownloadStatus(item.status))) {
return 'active';
}
return scheduledItems.every(item => item?.status === 'completed')
? 'completed'
: 'incomplete';
};