31 lines
1.2 KiB
JavaScript
31 lines
1.2 KiB
JavaScript
|
|
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||
|
|
import { mkdtempSync, rmSync } from 'node:fs';
|
||
|
|
import { tmpdir } from 'node:os';
|
||
|
|
import { join } from 'node:path';
|
||
|
|
import { enqueueSpan, dequeueSpan, readCursor, writeCursor } from './secretary-queue.mjs';
|
||
|
|
|
||
|
|
let dir;
|
||
|
|
beforeEach(() => { dir = mkdtempSync(join(tmpdir(), 'secq-')); });
|
||
|
|
afterEach(() => { rmSync(dir, { recursive: true, force: true }); });
|
||
|
|
|
||
|
|
const job = (index) => ({ session: 's', span: { start: index * 10, end: index * 10 + 5, index, note: '' }, kind: 'span' });
|
||
|
|
|
||
|
|
describe('secretary-queue', () => {
|
||
|
|
it('enqueue then dequeue returns jobs in FIFO order', () => {
|
||
|
|
enqueueSpan(dir, job(0)); enqueueSpan(dir, job(1));
|
||
|
|
expect(dequeueSpan(dir).span.index).toBe(0);
|
||
|
|
expect(dequeueSpan(dir).span.index).toBe(1);
|
||
|
|
expect(dequeueSpan(dir)).toBe(null);
|
||
|
|
});
|
||
|
|
it('enqueue dedups by span index (no duplicate jobs)', () => {
|
||
|
|
enqueueSpan(dir, job(2)); enqueueSpan(dir, job(2));
|
||
|
|
expect(dequeueSpan(dir).span.index).toBe(2);
|
||
|
|
expect(dequeueSpan(dir)).toBe(null);
|
||
|
|
});
|
||
|
|
it('cursor defaults to -1 and round-trips', () => {
|
||
|
|
expect(readCursor(dir)).toBe(-1);
|
||
|
|
writeCursor(dir, 4);
|
||
|
|
expect(readCursor(dir)).toBe(4);
|
||
|
|
});
|
||
|
|
});
|