import test from 'node:test' import assert from 'node:assert/strict' import { loadModule } from './load.mjs' const t = await loadModule('src/lib/ticketTimes.js') const tickets = (n, from = 100000) => Array.from({ length: n }, (_, i) => ({ $id: 'id' + i, woid: String(from + i) })) const sup = JSON.stringify([{ employeeId: 'e', name: 'N', task: 'x' }]) test('chunk und planTimeLoad: erste 200 Zeilen, Blöcke zu 100', () => { assert.deepEqual(t.chunk([1, 2, 3, 4, 5], 2), [[1, 2], [3, 4], [5]]) const plan = t.planTimeLoad(tickets(250), new Set()) assert.deepEqual(plan.blocks.map((b) => b.length), [100, 100]) assert.equal(plan.blocks[1].at(-1), '100199') assert.equal(plan.capped, true) assert.equal(t.planTimeLoad(tickets(200), new Set()).capped, false) }) test('planTimeLoad: nur Neues, ohne woid, ohne Doppelte', () => { const known = new Set(tickets(100).map((x) => x.woid)) assert.deepEqual(t.planTimeLoad(tickets(120), known).blocks, [tickets(20, 100100).map((x) => x.woid)]) assert.deepEqual(t.planTimeLoad([{ $id: 'a' }, { woid: '' }, { woid: '5' }, { woid: 5 }, { woid: '5' }], new Set()).blocks, [['5']]) assert.deepEqual(t.planTimeLoad(tickets(3), new Set(tickets(3).map((x) => x.woid))).blocks, []) }) test('blockMinutes: Summe ohne Kommentare, mit Unterstützung, nur woids des Blocks', () => { const docs = [ { woid: '1', totalTime: 60 }, { woid: '1', totalTime: 30, supporters: sup }, { woid: '1', totalTime: 0, isComment: true }, { woid: '2', totalTime: 45, isComment: true }, { woid: 7, totalTime: 15 }, { woid: '99', totalTime: 15 }, ] assert.deepEqual(t.blockMinutes(['1', '2', '3', '7'], docs, 6), { 1: 105, 2: 0, 3: 0, 7: 15 }) }) test('blockMinutes: abgeschnittene Blöcke liefern null, nie eine falsche Zahl', () => { assert.equal(t.blockMinutes(['1'], Array.from({ length: 5000 }, () => ({ woid: '1', totalTime: 15 })), 5000), null) assert.equal(t.blockMinutes(['1'], [{ woid: '1', totalTime: 15 }], 7), null) assert.deepEqual(t.blockMinutes(['1'], [{ woid: '1', totalTime: 15 }], 1), { 1: 15 }) }) test('minutesCellText: Fehler, null, 0, ohne woid und jenseits von 200 zeigen –', () => { const by = new Map([['1', 105], ['2', 0], ['3', null]]) assert.equal(t.minutesCellText({ woid: '1' }, 0, by), '1 h 45 min') for (const x of [{ woid: '2' }, { woid: '3' }, { woid: '4' }, {}, { woid: '' }]) assert.equal(t.minutesCellText(x, 0, by), '–') assert.equal(t.minutesCellText({ woid: '1' }, 199, by), '1 h 45 min') assert.equal(t.minutesCellText({ woid: '1' }, 200, by), '–') }) test('mapLimit: höchstens 3 gleichzeitig, Reihenfolge, Fehler je Element', async () => { let running = 0, peak = 0 const out = await t.mapLimit([1, 2, 3, 4, 5, 6, 7], 3, async (n) => { running += 1; peak = Math.max(peak, running) await new Promise((r) => setTimeout(r, 5)) running -= 1 if (n === 4) throw new Error('x') return n * 2 }) assert.equal(peak, 3) assert.deepEqual(out.map((r) => r.ok), [true, true, true, false, true, true, true]) assert.deepEqual(out.filter((r) => r.ok).map((r) => r.value), [2, 4, 6, 10, 12, 14]) assert.deepEqual(await t.mapLimit([], 3, async () => 1), []) })