import { describe, it, expect } from 'vitest'; import { cliErrorText, srvErrorText, srvEventText } from '../../src/utils/error-text.js'; import { errIsoConnect, errIsoDisconnect, errIsoInvalidPDU, errNegotiatingPDU, errCliInvalidParams, errCliJobPending, errCliSizeOverPDU, errCliInvalidPlcAnswer, errCliJobTimeout, errSrvCannotStart, errSrvDBNullPointer, errSrvAreaAlreadyExists, evcServerStarted, evcServerStopped, evcClientAdded, evcDataRead, evcDataWrite, } from '../../src/protocol/constants.js'; describe('error-text', () => { describe('cliErrorText', () => { it('should return "No error" for 0', () => { expect(cliErrorText(0)).toBe('No error'); }); it('should return ISO error descriptions', () => { expect(cliErrorText(errIsoConnect)).toBe('ISO connection error'); expect(cliErrorText(errIsoDisconnect)).toBe('ISO disconnect error'); expect(cliErrorText(errIsoInvalidPDU)).toBe('Invalid PDU received'); }); it('should return Client error descriptions', () => { expect(cliErrorText(errNegotiatingPDU)).toBe('PDU negotiation error'); expect(cliErrorText(errCliInvalidParams)).toBe('Invalid parameters'); expect(cliErrorText(errCliJobPending)).toBe('Job pending'); expect(cliErrorText(errCliSizeOverPDU)).toBe('Size over PDU'); expect(cliErrorText(errCliInvalidPlcAnswer)).toBe('Invalid PLC answer'); expect(cliErrorText(errCliJobTimeout)).toBe('Job timeout'); }); it('should return unknown error for invalid code', () => { const text = cliErrorText(0x99999999); expect(text).toContain('Unknown error'); }); }); describe('srvErrorText', () => { it('should return "No error" for 0', () => { expect(srvErrorText(0)).toBe('No error'); }); it('should return Server error descriptions', () => { expect(srvErrorText(errSrvCannotStart)).toBe('Server cannot start'); expect(srvErrorText(errSrvDBNullPointer)).toBe('DB null pointer'); expect(srvErrorText(errSrvAreaAlreadyExists)).toBe('Area already exists'); }); it('should return ISO error descriptions for shared codes', () => { expect(srvErrorText(errIsoConnect)).toBe('ISO connection error'); }); it('should return unknown error for invalid code', () => { const text = srvErrorText(0x99999999); expect(text).toContain('Unknown error'); }); }); describe('srvEventText', () => { it('should describe server started event', () => { const text = srvEventText({ EvtCode: evcServerStarted, EvtRetCode: 0, EvtParam1: 0, EvtParam2: 0 }); expect(text).toContain('Server started'); }); it('should describe server stopped event', () => { const text = srvEventText({ EvtCode: evcServerStopped, EvtRetCode: 0, EvtParam1: 0, EvtParam2: 0 }); expect(text).toContain('Server stopped'); }); it('should describe client added event', () => { const text = srvEventText({ EvtCode: evcClientAdded, EvtRetCode: 0, EvtParam1: 1, EvtParam2: 0 }); expect(text).toContain('Client added'); }); it('should include return code when non-zero', () => { const text = srvEventText({ EvtCode: evcDataRead, EvtRetCode: 5, EvtParam1: 0, EvtParam2: 0 }); expect(text).toContain('Data read'); expect(text).toContain('Result'); }); it('should include params when non-zero', () => { const text = srvEventText({ EvtCode: evcDataWrite, EvtRetCode: 0, EvtParam1: 42, EvtParam2: 0 }); expect(text).toContain('Data write'); expect(text).toContain('P1: 42'); }); it('should handle unknown event code', () => { const text = srvEventText({ EvtCode: 0x99999999, EvtRetCode: 0, EvtParam1: 0, EvtParam2: 0 }); expect(text).toContain('Event'); }); }); });