28 lines
1.5 KiB
JavaScript
28 lines
1.5 KiB
JavaScript
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { loadModule } from './load.mjs'
|
|
|
|
const { lookupWorkorder } = await loadModule('src/lib/workorderLookup.js')
|
|
const fake = ({ byWoid = null, byId, byIdError } = {}) => {
|
|
const calls = []
|
|
return { calls, findByWoid: async (w) => { calls.push(['woid', w]); return byWoid }, getById: async (id) => { calls.push(['id', id]); if (byIdError) throw byIdError; return byId } }
|
|
}
|
|
test('erst woid, dann $id', async () => {
|
|
let d = fake({ byWoid: { $id: 'a', woid: '5' } })
|
|
assert.deepEqual(await lookupWorkorder(' 5 ', d), { status: 'ok', ticket: { $id: 'a', woid: '5' } })
|
|
assert.deepEqual(d.calls, [['woid', '5']])
|
|
d = fake({ byId: { $id: 'abc' } })
|
|
assert.deepEqual(await lookupWorkorder('abc', d), { status: 'ok', ticket: { $id: 'abc' } })
|
|
assert.deepEqual(d.calls, [['woid', 'abc'], ['id', 'abc']])
|
|
})
|
|
test('400 und 404 sind nicht gefunden, leeres ref ohne Abfrage', async () => {
|
|
for (const code of [400, 404]) assert.deepEqual(await lookupWorkorder('x', fake({ byIdError: { code } })), { status: 'notfound' })
|
|
const d = fake()
|
|
for (const empty of ['', ' ', null, undefined]) assert.deepEqual(await lookupWorkorder(empty, d), { status: 'notfound' })
|
|
assert.deepEqual(d.calls, [])
|
|
})
|
|
test('andere Fehler werden weitergereicht', async () => {
|
|
await assert.rejects(lookupWorkorder('x', fake({ byIdError: { code: 500, message: 'boom' } })))
|
|
await assert.rejects(lookupWorkorder('x', { findByWoid: async () => { throw new Error('netz') }, getById: async () => ({}) }), /netz/)
|
|
})
|