Avoid lifecycle history scans in fleet snapshots

This commit is contained in:
2026-10-05 14:59:08 -04:00
parent 041c024f95
commit 28bd08e1ea
7 changed files with 132 additions and 15 deletions
+32
View File
@@ -116,4 +116,36 @@ describe('persisted Fleet operations', () => {
expect(fleetOperations(now)).toContainEqual(expect.objectContaining({ kind: 'beta', label: 'Observing a match', href: '/betas/1' }));
expect(fleetOperations(now)).toContainEqual(expect.objectContaining({ kind: 'recovery', state: 'attention' }));
});
it('preserves lifecycle selection across cutoff, deletion, supersession, and overlapping warning branches', async () => {
const { db } = await import('../../src/lib/server/db/index.js');
const handle = db();
const since = iso(-86_400_000);
handle.transaction(() => {
const insert = handle.prepare(`INSERT INTO server_deployments
(id, server, action, state, stage, started_at, finished_at, server_deleted_at, triggered_by)
VALUES (?, ?, 'restart', ?, 'Stage', ?, ?, ?, 'test')`);
let i = 0;
for (const state of ['queued', 'running', 'waiting', 'awaiting-health', 'done', 'warning', 'cancelled', 'failed', 'interrupted']) {
for (const finished of [null, iso(-86_400_001), since, iso()]) {
for (const deleted of [null, iso()]) {
for (const successor of ['none', 'live', 'deleted']) {
const id = `operation-${i++}`;
insert.run(id, id, state, iso(-i), finished, deleted);
if (successor !== 'none') insert.run(`${id}-next`, id, 'done', iso(-i), null, successor === 'deleted' ? iso() : null);
}
}
}
}
})();
// The former OR query is the compatibility reference for all three branches.
const expected = handle.prepare(`SELECT d.id FROM server_deployments d
WHERE d.server_deleted_at IS NULL AND (d.state IN ('queued', 'running', 'waiting', 'awaiting-health')
OR (d.state IN ('done', 'warning', 'cancelled') AND d.finished_at >= ?)
OR (d.state IN ('failed', 'interrupted', 'warning') AND NOT EXISTS
(SELECT 1 FROM server_deployments n WHERE n.server = d.server AND n.rowid > d.rowid)))`)
.all(since) as { id: string }[];
const { fleetOperations } = await import('../../src/lib/server/fleet/overview.js');
expect(fleetOperations(now).map(row => row.key).sort()).toEqual(expected.map(row => `deployment:${row.id}`).sort());
});
});