Files

75 lines
3.7 KiB
TypeScript
Raw Permalink Normal View History

import { expect, tap } from '@git.zone/tstest/tapbundle';
import { IppClient, IppMapper, type IIppAttributeRecord, type IIppSnapshot } from '../../ts/integrations/ipp/index.js';
const attributes: IIppAttributeRecord = {
'printer-name': 'Office Printer',
'printer-info': 'Office color printer',
'printer-location': 'Office',
'printer-make-and-model': 'HP Color LaserJet Pro',
'printer-device-id': 'MFG:HP;MDL:Color LaserJet Pro;CMD:PCL,POSTSCRIPT;SERIALNUMBER:CN123456;',
'printer-uuid': 'urn:uuid:printer-1',
'printer-state': 3,
'printer-state-message': 'Ready',
'printer-state-reasons': ['none'],
'printer-is-accepting-jobs': true,
'queued-job-count': 2,
'printer-up-time': 3600,
'printer-current-time': '2026-01-01T01:00:00.000Z',
'marker-names': ['Black Toner', 'Cyan Ink'],
'marker-types': ['toner-cartridge', 'ink-cartridge'],
'marker-colors': ['black', 'cyan'],
'marker-levels': [88, 42],
'marker-low-levels': [5, 10],
'marker-high-levels': [100, 100],
'job-id': [12],
'job-name': ['Test Page'],
'job-state': [5],
'job-originating-user-name': ['phil'],
};
tap.test('maps IPP attributes to printer device, marker, status, and job sensors', async () => {
const snapshot = IppClient.attributesToSnapshot(attributes, { host: 'printer.local', port: 631, basePath: '/ipp/print' }, true);
expect(snapshot.printer.manufacturer).toEqual('HP');
expect(snapshot.printer.serialNumber).toEqual('CN123456');
expect(snapshot.status.printerState).toEqual('idle');
expect(snapshot.status.bootedAt).toEqual('2026-01-01T00:00:00.000Z');
expect(snapshot.markers[0].kind).toEqual('toner');
expect(snapshot.markers[1].kind).toEqual('ink');
expect(snapshot.jobs[0].state).toEqual('processing');
const devices = IppMapper.toDevices(snapshot);
const entities = IppMapper.toEntities(snapshot);
expect(devices[0].id).toEqual('ipp.printer.urn_uuid_printer_1');
expect(devices[0].features.some((featureArg) => featureArg.id === 'marker_0')).toBeTrue();
expect(entities.find((entityArg) => entityArg.id === 'sensor.office_printer_status')?.state).toEqual('idle');
expect(entities.find((entityArg) => entityArg.id === 'sensor.black_toner')?.attributes?.markerKind).toEqual('toner');
expect(entities.find((entityArg) => entityArg.id === 'sensor.cyan_ink')?.state).toEqual(42);
expect(entities.find((entityArg) => entityArg.id === 'sensor.office_printer_queued_jobs')?.state).toEqual(2);
expect(entities.find((entityArg) => entityArg.id === 'binary_sensor.office_printer_accepting_jobs')?.state).toEqual('on');
expect(entities.find((entityArg) => entityArg.id === 'sensor.test_page')?.attributes?.owner).toEqual('phil');
});
tap.test('keeps hostless IPP runtime snapshots offline instead of faking live success', async () => {
const snapshot = await new IppClient({ name: 'Hostless Printer' }).getSnapshot();
expect(snapshot.online).toBeFalse();
expect(snapshot.source).toEqual('runtime');
expect(await new IppClient({ name: 'Hostless Printer' }).ping()).toBeFalse();
});
tap.test('maps offline snapshots without inventing marker or job values', async () => {
const snapshot: IIppSnapshot = {
printer: { id: 'offline-printer', name: 'Offline Printer' },
status: { printerState: 'unknown', stateReasons: [] },
markers: [],
jobs: [],
online: false,
updatedAt: '2026-01-01T00:00:00.000Z',
};
const entities = IppMapper.toEntities(snapshot);
expect(entities.find((entityArg) => entityArg.id === 'sensor.offline_printer_status')?.available).toBeFalse();
expect(entities.some((entityArg) => entityArg.id.includes('marker'))).toBeFalse();
expect(entities.some((entityArg) => entityArg.id.includes('job'))).toBeFalse();
});
export default tap.start();