|
| 1 | +import { |
| 2 | + updateInstrumentRunstate, |
| 3 | + updateInstrumentRunstatePV, |
| 4 | +} from "@/app/wall/utils"; |
| 5 | +import { IfcInstrumentStatus, instListEntry, targetStation } from "@/app/types"; |
| 6 | + |
| 7 | +test("updateInstrumentRunstate returns new array with runstate of instrument changed", () => { |
| 8 | + const instrumentName = "anInstrumentName"; |
| 9 | + const runStatePV = "AN:INST:DAE:RUNSTATE"; |
| 10 | + const instrument: IfcInstrumentStatus = { |
| 11 | + name: instrumentName, |
| 12 | + runstatePV: runStatePV, |
| 13 | + }; |
| 14 | + const original: targetStation = { |
| 15 | + targetStation: "Target station -1", |
| 16 | + instruments: [instrument], |
| 17 | + }; |
| 18 | + const expectedValue = "RUNNING"; |
| 19 | + expect( |
| 20 | + updateInstrumentRunstate([original], runStatePV, expectedValue)[0] |
| 21 | + .instruments[0].runstate, |
| 22 | + ).toBe(expectedValue); |
| 23 | +}); |
| 24 | +test("updateInstrumentRunstate returns untouched array if runstate PV is not found", () => { |
| 25 | + const runStatePV = "AN:INST:DAE:RUNSTATE"; |
| 26 | + const instrument: IfcInstrumentStatus = { |
| 27 | + name: "notThatInstrument", |
| 28 | + runstatePV: "BLAH", |
| 29 | + }; |
| 30 | + const original: targetStation = { |
| 31 | + targetStation: "Target station 42", |
| 32 | + instruments: [instrument], |
| 33 | + }; |
| 34 | + const expectedValue = "RUNNING"; |
| 35 | + expect( |
| 36 | + updateInstrumentRunstate([original], runStatePV, expectedValue)[0] |
| 37 | + .instruments[0].runstate, |
| 38 | + ).toBe(undefined); |
| 39 | +}); |
| 40 | + |
| 41 | +test("updateInstrumentRunstatePV returns untouched array if instrument is not found", () => { |
| 42 | + const runStatePvThatShouldNeverGetUsed = "INV:RUNSTATE"; |
| 43 | + const mockSendJsonMessage = jest.fn(); |
| 44 | + const invalidInstrument: instListEntry = { |
| 45 | + name: "invalidInstrument", |
| 46 | + groups: [], |
| 47 | + pvPrefix: "invalidPrefix", |
| 48 | + isScheduled: true, |
| 49 | + seci: false, |
| 50 | + hostName: "invalidHostname", |
| 51 | + }; |
| 52 | + const original: targetStation = { |
| 53 | + targetStation: "Target station 3", |
| 54 | + instruments: [], |
| 55 | + }; |
| 56 | + const returned = updateInstrumentRunstatePV( |
| 57 | + [original], |
| 58 | + invalidInstrument, |
| 59 | + runStatePvThatShouldNeverGetUsed, |
| 60 | + mockSendJsonMessage, |
| 61 | + ); |
| 62 | + expect(returned[0].instruments.length).toBe(0); |
| 63 | + expect(mockSendJsonMessage).toHaveBeenCalledTimes(0); |
| 64 | +}); |
| 65 | + |
| 66 | +test("updateInstrumentRunstatePV returns new array with runstate PV updated and subscribed to", () => { |
| 67 | + const runStatePvThatShouldGetUsed = "NEW:RUNSTATE"; |
| 68 | + const mockSendJsonMessage = jest.fn(); |
| 69 | + const inst: instListEntry = { |
| 70 | + name: "instrument", |
| 71 | + groups: [], |
| 72 | + pvPrefix: "prefix", |
| 73 | + isScheduled: true, |
| 74 | + seci: false, |
| 75 | + hostName: "hostname", |
| 76 | + }; |
| 77 | + const original: targetStation = { |
| 78 | + targetStation: "Target station 4", |
| 79 | + instruments: [inst], |
| 80 | + }; |
| 81 | + const returned = updateInstrumentRunstatePV( |
| 82 | + [original], |
| 83 | + inst, |
| 84 | + runStatePvThatShouldGetUsed, |
| 85 | + mockSendJsonMessage, |
| 86 | + ); |
| 87 | + expect(returned[0].instruments.length).toBe(1); |
| 88 | + expect(returned[0].instruments[0].runstatePV).toBe( |
| 89 | + inst.pvPrefix + runStatePvThatShouldGetUsed, |
| 90 | + ); |
| 91 | + expect(mockSendJsonMessage).toHaveBeenCalledTimes(1); |
| 92 | + expect(mockSendJsonMessage).toHaveBeenCalledWith({ |
| 93 | + type: "subscribe", |
| 94 | + pvs: [inst.pvPrefix + runStatePvThatShouldGetUsed], |
| 95 | + }); |
| 96 | +}); |
0 commit comments