Files
Webklar-Kundenbereich/test/pushTimeEntry.test.js
T
2026-09-30 13:34:17 +00:00

75 lines
3.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { test, before, after } from 'node:test'
import assert from 'node:assert/strict'
import { startFakeAppwrite } from './helpers/fakeAppwrite.js'
let fake
let startFromEnd, needsTimeEntry, parseMinutes, formatMinutes, applyPushTime
const openGit = (id) => ({
$id: id, wsid: '100200', woid: '100118', serviceType: 'GIT',
endDate: '30.09.2026', endTime: '1646', startDate: '30.09.2026', startTime: '', totalTime: 0, isComment: true,
})
before(async () => {
fake = await startFakeAppwrite({
seed: {
worksheets: [
openGit('ws1'),
openGit('ws-race'),
{ $id: 'remote1', serviceType: 'Remote', startTime: '', totalTime: 0 },
],
},
})
Object.assign(process.env, { APPWRITE_ENDPOINT: fake.endpoint, APPWRITE_API_KEY: 'test-key' })
;({ startFromEnd, needsTimeEntry, parseMinutes, formatMinutes, applyPushTime } = await import(
'../server/services/pushTimeEntry.js'
))
})
after(async () => {
await fake.close()
})
test('startFromEnd: normal, über Mitternacht, Jahres- und Schalttag-Grenze', () => {
assert.deepEqual(startFromEnd('30.09.2026', '1646', 60), { startDate: '30.09.2026', startTime: '1546' })
assert.deepEqual(startFromEnd('01.10.2026', '0020', 45), { startDate: '30.09.2026', startTime: '2335' })
assert.deepEqual(startFromEnd('01.01.2027', '0010', 30), { startDate: '31.12.2026', startTime: '2340' })
assert.deepEqual(startFromEnd('01.03.2028', '0000', 1), { startDate: '29.02.2028', startTime: '2359' })
})
test('needsTimeEntry', () => {
assert.equal(needsTimeEntry({ serviceType: 'GIT', startTime: '', totalTime: 0 }), true)
assert.equal(needsTimeEntry({ serviceType: 'GIT', startTime: '1500', totalTime: 45 }), false)
assert.equal(needsTimeEntry({ serviceType: 'Remote', startTime: '', totalTime: 0 }), false)
})
test('parseMinutes: ganze Minuten 1–720', () => {
assert.equal(parseMinutes('45'), 45)
assert.equal(parseMinutes(720), 720)
for (const bad of ['1.5', '0', '721', '', 'abc', undefined, null, '-5', '45abc']) {
assert.equal(parseMinutes(bad), null, String(bad))
}
})
test('formatMinutes', () => {
assert.equal(formatMinutes(15), '15 Min')
assert.equal(formatMinutes(60), '1 Std')
assert.equal(formatMinutes(90), '1,5 Std')
assert.equal(formatMinutes(130), '2 Std 10 Min')
})
test('applyPushTime: speichert einmal, überschreibt nie', async () => {
assert.equal((await applyPushTime('ws1', 60)).status, 'saved')
const ws = fake.get('worksheets', 'ws1')
assert.deepEqual([ws.startDate, ws.startTime, ws.totalTime, ws.isComment], ['30.09.2026', '1546', 60, false])
assert.equal((await applyPushTime('ws1', 30)).status, 'already')
assert.equal(fake.get('worksheets', 'ws1').totalTime, 60)
assert.equal((await applyPushTime('gibtsnicht', 30)).status, 'missing')
assert.equal((await applyPushTime('remote1', 30)).status, 'not_git')
})
test('gleichzeitig: nur einer speichert', async () => {
const results = await Promise.all([applyPushTime('ws-race', 30), applyPushTime('ws-race', 90)])
assert.deepEqual(results.map((r) => r.status).sort(), ['already', 'saved'])
})