mirror of
https://github.com/nimbold/Firelink.git
synced 2026-08-08 18:33:39 +00:00
fix(scheduler): run post-completion system actions
This commit is contained in:
@@ -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');
|
||||
});
|
||||
});
|
||||
@@ -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';
|
||||
};
|
||||
Reference in New Issue
Block a user