import assert from 'node:assert/strict'; import { mkdtempSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import LibDashStore from '../src/index.js'; import { BetterSqlite3Provider } from '../src/providers/sqlite3/index.js'; const tempDir = mkdtempSync(join(tmpdir(), 'dash-printeragent-test-')); const provider = new BetterSqlite3Provider({ filename: join(tempDir, 'test.db'), storageDir: join(tempDir, 'storage') }); await provider.initialize(); const store = await new LibDashStore(provider); try { const googleAuthId = await store.google_auth.add('printeragent-tester@example.com'); const company = await store.company.create({ name: 'PA GmbH', iso639: 'de', taxrate: 19, reducedtaxrate: 7, googleAuthId }); await store.google_auth.setSelectedCompany(googleAuthId, company.id); assert.deepEqual(await store.printeragent.list(googleAuthId), []); await assert.rejects(store.printeragent.create(googleAuthId, { name: ' ', pairingpin: '1' }), /Invalid printeragent/); const a = await store.printeragent.create(googleAuthId, { name: ' Bar ', pairingpin: '123456', sort: 5 }); assert.equal(a.name, 'Bar'); assert.equal(a.pairingpin, '123456'); assert.equal(a.token, null); assert.equal(a.sort, 5); assert.equal(a.companyId, company.id); assert.ok(a.updatedAt); // findByPairingpin const foundByPin = await store.printeragent.findByPairingpin('123456'); assert.equal(foundByPin.id, a.id); assert.equal(await store.printeragent.findByPairingpin('999999'), null); // pair const paired = await store.printeragent.pair(a.id, { token: 'test-token-123', clientserial: 'SN12345', clientmodel: 'Epson TM-T88VI', agentversion: '1.0.0', }); assert.equal(paired.token, 'test-token-123'); assert.equal(paired.clientserial, 'SN12345'); assert.equal(paired.clientmodel, 'Epson TM-T88VI'); assert.equal(paired.agentversion, '1.0.0'); assert.equal(paired.pairingpin, null); assert.ok(paired.pairedAt); // findByToken const foundByToken = await store.printeragent.findByToken('test-token-123'); assert.equal(foundByToken.id, a.id); assert.equal(foundByToken.clientmodel, 'Epson TM-T88VI'); assert.equal(await store.printeragent.findByToken('invalid-token'), null); // updateState const withState = await store.printeragent.updateState(a.id, { online: true, paper: 'ok' }); assert.equal(withState.clientstate, JSON.stringify({ online: true, paper: 'ok' })); // touchLastseen await store.printeragent.touchLastseen(a.id, '1.0.1'); const afterTouch = await store.printeragent.findByToken('test-token-123'); assert.equal(afterTouch.agentversion, '1.0.1'); // update name and sort const b = await store.printeragent.update(googleAuthId, { id: a.id, name: 'Kitchen', sort: 1 }); assert.equal(b.name, 'Kitchen'); assert.equal(b.sort, 1); // list const list = await store.printeragent.list(googleAuthId); assert.equal(list.length, 1); assert.equal(list[0].name, 'Kitchen'); assert.equal(list[0].sort, 1); // delete await store.printeragent.delete(googleAuthId, a.id); assert.equal(await store.printeragent.findByToken('test-token-123'), null); assert.deepEqual(await store.printeragent.list(googleAuthId), []); console.log('printeragent full test OK'); } finally { await provider.close?.(); rmSync(tempDir, { recursive: true, force: true }); }