118 lines
3.9 KiB
TypeScript
118 lines
3.9 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import type { ProjectSyncRunner } from './core/engine.ts';
|
|
import { emptyCounts, type Project, type RunTrigger } from './core/model.ts';
|
|
import type { ProjectRepository } from './core/ports.ts';
|
|
import { SyncCoordinator } from './coordinator.ts';
|
|
import { silentLogger } from './testing/harness.ts';
|
|
|
|
function project(id: number, overrides: Partial<Project> = {}): Project {
|
|
return { id, enabled: true, pausedReason: null, ...overrides } as Project;
|
|
}
|
|
|
|
/** A runner whose runs stay in flight until `finish` is called. */
|
|
class ControlledRunner implements ProjectSyncRunner {
|
|
readonly started: Array<[number, RunTrigger]> = [];
|
|
private readonly pending: Array<() => void> = [];
|
|
|
|
runProject(projectId: number, trigger: RunTrigger) {
|
|
this.started.push([projectId, trigger]);
|
|
return new Promise<never>((resolve) => {
|
|
this.pending.push(() =>
|
|
resolve({ runId: 1, outcome: 'success', error: null, counts: emptyCounts() } as never)
|
|
);
|
|
});
|
|
}
|
|
|
|
finishAll() {
|
|
this.pending.splice(0).forEach((finish) => finish());
|
|
}
|
|
}
|
|
|
|
function setup(projects: Project[]) {
|
|
let now = 0;
|
|
const runner = new ControlledRunner();
|
|
const repo = {
|
|
get: async (id: number) => projects.find((p) => p.id === id) ?? null,
|
|
list: async () => projects
|
|
} as ProjectRepository;
|
|
const coordinator = new SyncCoordinator({
|
|
runner,
|
|
projects: repo,
|
|
clock: () => new Date(now),
|
|
logger: silentLogger,
|
|
manualCooldownMs: 30_000
|
|
});
|
|
return { runner, coordinator, advance: (ms: number) => (now += ms) };
|
|
}
|
|
|
|
describe('SyncCoordinator', () => {
|
|
it('starts a manual run and reports it as already running on a second click', async () => {
|
|
const { runner, coordinator } = setup([project(1)]);
|
|
|
|
expect(await coordinator.requestManualRun(1)).toEqual({ status: 'started' });
|
|
expect(await coordinator.requestManualRun(1)).toEqual({ status: 'already-running' });
|
|
expect(runner.started).toEqual([[1, 'manual']]);
|
|
});
|
|
|
|
it('enforces the manual cooldown after a run finishes', async () => {
|
|
const { runner, coordinator, advance } = setup([project(1)]);
|
|
await coordinator.requestManualRun(1);
|
|
runner.finishAll();
|
|
await coordinator.drain();
|
|
|
|
advance(10_000);
|
|
expect(await coordinator.requestManualRun(1)).toEqual({
|
|
status: 'cooldown',
|
|
retryAfterSeconds: 20
|
|
});
|
|
|
|
advance(20_000);
|
|
expect(await coordinator.requestManualRun(1)).toEqual({ status: 'started' });
|
|
});
|
|
|
|
it('keeps cooldowns per project', async () => {
|
|
const { coordinator } = setup([project(1), project(2)]);
|
|
await coordinator.requestManualRun(1);
|
|
expect(await coordinator.requestManualRun(2)).toEqual({ status: 'started' });
|
|
});
|
|
|
|
it('refuses manual runs for disabled, paused or unknown projects', async () => {
|
|
const [off, paused, unknown] = [1, 2, 99];
|
|
const { coordinator } = setup([
|
|
project(off, { enabled: false }),
|
|
project(paused, { pausedReason: 'Root deleted' })
|
|
]);
|
|
|
|
expect(await coordinator.requestManualRun(off)).toMatchObject({ status: 'unavailable' });
|
|
expect(await coordinator.requestManualRun(paused)).toEqual({
|
|
status: 'unavailable',
|
|
reason: 'Root deleted'
|
|
});
|
|
expect(await coordinator.requestManualRun(unknown)).toMatchObject({ status: 'unavailable' });
|
|
});
|
|
|
|
it('runs scheduled passes sequentially, skipping busy, disabled and paused projects', async () => {
|
|
const [busy, off, paused, a, b] = [1, 2, 3, 4, 5];
|
|
const { runner, coordinator } = setup([
|
|
project(busy),
|
|
project(off, { enabled: false }),
|
|
project(paused, { pausedReason: 'x' }),
|
|
project(a),
|
|
project(b)
|
|
]);
|
|
await coordinator.requestManualRun(busy);
|
|
|
|
const pass = coordinator.runScheduledPass();
|
|
await Promise.resolve();
|
|
await Promise.resolve();
|
|
expect(runner.started.map(([id]) => id)).toEqual([busy, a]);
|
|
|
|
runner.finishAll();
|
|
await new Promise((r) => setTimeout(r));
|
|
expect(runner.started.map(([id]) => id)).toEqual([busy, a, b]);
|
|
|
|
runner.finishAll();
|
|
await pass;
|
|
});
|
|
});
|