import { TransactionReportHelper } from "../src/TransactionReportHelper"; import { Events, Message, SuccessState } from "../src/Messages"; import { Spi } from "../src/Spi"; import { SpiFlow, SpiStatus, TransactionFlowState, TransactionType, } from "../src/SpiModels"; import { getLastConsoleCallArgs } from "./utils/TestHelpers"; import PurchaseResponseFixture from './fixtures/PurchaseResponse.json' import CashoutOnlyResponseFixture from './fixtures/CashoutOnlyResponse.json' import MotoResponsePhoneFixture from './fixtures/MotoResponse_Phone.json' import RefundResponseFixture from './fixtures/RefundResponse.json' import SettleResponseFixture from './fixtures/SettleResponse.json' import SettlementEnquiryResponseFixture from './fixtures/SettlementEnquiryResponse.json' import ReversalResponseSuccessFixture from './fixtures/ReversalResponse_Success.json' import GetTransactionResponsePurchaseSuccessFixture from './fixtures/GetTransactionResponse_PurchaseSuccess.json' import GetTransactionResponseSignatureRequiredFixture from './fixtures/GetTransactionResponse_SignatureRequired.json' import GetTransactionResponseAuthCodeRequiredFixture from './fixtures/GetTransactionResponse_AuthCodeRequired.json' import GetTransactionResponseTransactionInProgressFixture from './fixtures/GetTransactionResponse_TransactionInProgress.json' import GetTransactionResponsePosRefIdNotFoundFixture from './fixtures/GetTransactionResponse_PosRefIdNotFound.json' import GetTransactionResponsePosRefIdInvalidFixture from './fixtures/GetTransactionResponse_PosRefIdInvalid.json' import GetTransactionResponseMissingPosRefIdFixture from './fixtures/GetTransactionResponse_MissingPosRefId.json' import GetTransactionResponseUnexpectedFixture from './fixtures/GetTransactionResponse_Unexpected.json' import GetTransactionResponseMissingTransactionDataFixture from './fixtures/GetTransactionResponse_MissingTransactionData.json' import GetTransactionResponseOpInProgressFixture from './fixtures/GetTransactionResponse_OpInProgress.json' import GetLastTransactionResponseNoTransactionsFixture from './fixtures/GetLastTransactionResponse_NoTransactions.json' import GetLastTransactionResponsePurchaseSuccessFixture from './fixtures/GetLastTransactionResponse_PurchaseSuccess.json' import { ProvisionPosSpiTokenData } from "../src/types"; describe("Spi,", () => { const eftposAddress = "10.20.30.40"; const posId = "DummyPos"; const posVendorId = "mx51"; const posVersion = "2.9.8"; const serialNumber = "321-321-321"; let fetchHelper; beforeEach(() => { spyOn(console, "error"); spyOn(console, "info"); spyOn(console, "warn"); spyOn(document, "dispatchEvent"); const fetchPromise = new Promise((resolve, reject) => { fetchHelper = { resolve, reject }; // eslint-disable-next-line @typescript-eslint/no-explicit-any }) as any; spyOn(window, "fetch").and.returnValue(fetchPromise); }); it("should set PosId correctly when SetPosId is passed a PosId that is valid", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentStatus = SpiStatus.Unpaired; // act spi.SetPosId(posId); // assert expect(spi._posId).toBe(posId); }); it("should set PosId correctly when Start is called after instantiating with a PosId that is valid", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentStatus = SpiStatus.Unpaired; spi.SetPosInfo(posVendorId, posVersion); // act spi.Start(); // assert expect(spi._posId).toBe(posId); }); it("should set PosId to empty when SetPosId is passed a PosId that is empty", () => { // arrange const invalidPosId = ""; const spi = new Spi(invalidPosId, "", eftposAddress, null); spi.CurrentStatus = SpiStatus.Unpaired; // act spi.SetPosId(invalidPosId); // assert expect(spi._posId).toBe(""); }); it("should set PosId to empty when Start is called after instantiating with a PosId that is empty", () => { // arrange const invalidPosId = ""; const spi = new Spi(invalidPosId, "", eftposAddress, null); spi.CurrentStatus = SpiStatus.Unpaired; spi.SetPosInfo(posVendorId, posVersion); // act spi.Start(); // assert expect(spi._posId).toBe(""); }); it("should set PosId to empty when SetPosId is passed a PosId that is too long", () => { // arrange const invalidPosId = "12345678901234567"; const spi = new Spi(invalidPosId, "", eftposAddress, null); spi.CurrentStatus = SpiStatus.Unpaired; // act spi.SetPosId(invalidPosId); // assert expect(spi._posId).toBe(""); }); it("should set PosId to empty when Start is called after instantiating with a PosId that is too long", () => { // arrange const invalidPosId = "12345678901234567"; const spi = new Spi(invalidPosId, "", eftposAddress, null); spi.CurrentStatus = SpiStatus.Unpaired; spi.SetPosInfo(posVendorId, posVersion); // act spi.Start(); // assert expect(spi._posId).toBe(""); }); it("should set PosId to empty when SetPosId is passed a PosId that has invalid characters", () => { // arrange const invalidPosId = "RamenPos@"; const spi = new Spi(invalidPosId, "", eftposAddress, null); spi.CurrentStatus = SpiStatus.Unpaired; // act spi.SetPosId(invalidPosId); // assert expect(spi._posId).toBe(""); }); it("should set PosId to empty when Start is called after instantiating with a PosId that has invalid characters", () => { // arrange const invalidPosId = "RamenPos@"; const spi = new Spi(invalidPosId, "", eftposAddress, null); spi.CurrentStatus = SpiStatus.Unpaired; spi.SetPosInfo(posVendorId, posVersion); // act spi.Start(); // assert expect(spi._posId).toBe(""); }); it("should set EftposAddress correctly when SetEftposAddress is passed an EftposAddress that is valid", () => { // arrange const spi = new Spi("", "", "", null); spi.CurrentStatus = SpiStatus.Unpaired; // act spi.SetEftposAddress(eftposAddress); const spiEftposAddress = spi._eftposAddress.replace(/^w[s]?s:\/\//, ""); // assert expect(spiEftposAddress).toBe(eftposAddress); }); it("should set EftposAddress correctly when Start is called after instantiating with an EftposAddress that is valid", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentStatus = SpiStatus.Unpaired; spi.SetPosInfo(posVendorId, posVersion); // act spi.Start(); const spiEftposAddress = spi._eftposAddress.replace(/^w[s]?s:\/\//, ""); // assert expect(spiEftposAddress).toBe(eftposAddress); }); it("should set EftposAddress to empty when SetEftposAddress is passed an EftposAddress that is empty", () => { // arrange const invalidEftposAddress = ""; const spi = new Spi(posId, "", "", null); spi.CurrentStatus = SpiStatus.Unpaired; // act spi.SetEftposAddress(invalidEftposAddress); const spiEftposAddress = spi._eftposAddress.replace(/^w[s]?s:\/\//, ""); // assert expect(spiEftposAddress).toBe(""); }); it("should set EftposAddress to empty when Start is called after instantiating with an EftposAddress that is empty", () => { // arrange const invalidEftposAddress = ""; const spi = new Spi(posId, "", invalidEftposAddress, null); spi.CurrentStatus = SpiStatus.Unpaired; spi.SetPosInfo(posVendorId, posVersion); // act spi.Start(); // assert expect(spi._eftposAddress).toBe(""); }); it("should set EftposAddress to empty when SetEftposAddress is passed an EftposAddress that is invalid", () => { // arrange const invalidEftposAddress = "10.20.30"; const spi = new Spi("", "", "", null); spi.CurrentStatus = SpiStatus.Unpaired; // act spi.SetEftposAddress(invalidEftposAddress); const spiEftposAddress = spi._eftposAddress.replace(/^w[s]?s:\/\//, ""); // assert expect(spiEftposAddress).toBe(""); }); it("should set EftposAddress to empty when Start is called after instantiating with an EftposAddress that is invalid", () => { // arrange const invalidEftposAddress = "10.20.30"; const spi = new Spi(posId, "", invalidEftposAddress, null); spi.CurrentStatus = SpiStatus.Unpaired; spi.SetPosInfo(posVendorId, posVersion); // act spi.Start(); // assert expect(spi._eftposAddress).toBe(""); }); it("should append port 8080 to eftpos address for non WBC terminal on ws connection", () => { // arrange const validEftposAddress = "ws://10.20.30.40"; const spi = new Spi( posId, "", validEftposAddress, null ); // act const address = spi._getConnectionAddress( validEftposAddress, "gko", "test" ); // assert expect(address).toBe("ws://10.20.30.40:8080?posId=test"); }); it("should append port 8443 to eftpos address for non WBC terminal for WSS connection", () => { // arrange const validEftposAddress = "wss://10.20.30.40"; const spi = new Spi( posId, "", validEftposAddress, null ); // act const address = spi._getConnectionAddress( validEftposAddress, "gko", "test" ); // assert expect(address).toBe("wss://10.20.30.40:8443?posId=test"); }); it("should take default port to eftpos address for WBC terminal for WSS connection", () => { // arrange const validEftposAddress = "wss://10.20.30.40"; const spi = new Spi( posId, "", validEftposAddress, null ); // act const address = spi._getConnectionAddress( validEftposAddress, "wbc", "test" ); // assert expect(address).toBe("wss://10.20.30.40?posId=test"); }); it("should take default port to eftpos address for non WBC terminal on ws connection", () => { // arrange const validEftposAddress = "ws://10.20.30.40"; const spi = new Spi( posId, "", validEftposAddress, null ); // act const address = spi._getConnectionAddress( validEftposAddress, "wbc", "test" ); // assert expect(address).toBe("ws://10.20.30.40?posId=test"); }); describe("InitiateReversal()", () => { it("should not initiate a Reversal when not paired", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentStatus = SpiStatus.Unpaired; // act const initiateReversalResult = spi.InitiateReversal(posId); // assert expect(initiateReversalResult.Message).toMatch(/not paired/i); }); it("should not initiate a Reversal when not idle", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; // act const initiateReversalResult = spi.InitiateReversal(posId); // assert expect(initiateReversalResult.Message).toMatch(/not idle/i); }); it("should initiate a Reversal", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Idle; spi._send = () => true; // act const initiateReversalResult = spi.InitiateReversal(posId); // assert expect(initiateReversalResult.Message).toMatch(/reversal initiated/i); expect(spi.CurrentTxFlowState.Type).toBe(TransactionType.Reversal); }); }); describe("InitiateGetLastTx()", () => { it("should not initiate a GLT when not paired", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentStatus = SpiStatus.Unpaired; // act const initiateTxResult = spi.InitiateGetLastTx(); // assert expect(initiateTxResult.Message).toMatch(/not paired/i); }); it("should not initiate a GLT when not idle", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; // act const initiateTxResult = spi.InitiateGetLastTx(); // assert expect(initiateTxResult.Message).toMatch(/not idle/i); }); it("should initiate a GLT", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Idle; spi._send = () => true; // act const initiateTxResult = spi.InitiateGetLastTx(); // assert expect(initiateTxResult.Message).toMatch(/glt initiated/i); expect(spi.CurrentTxFlowState.Type).toBe( TransactionType.GetLastTransaction ); }); }); describe("InitiateGetTx()", () => { it("should not initiate a GT when not paired", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentStatus = SpiStatus.Unpaired; // act const initiateTxResult = spi.InitiateGetTx(posId); // assert expect(initiateTxResult.Message).toMatch(/not paired/i); }); it("should not initiate a GT when not idle", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; // act const initiateTxResult = spi.InitiateGetTx(posId); // assert expect(initiateTxResult.Message).toMatch(/not idle/i); }); it("should initiate a GT", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Idle; spi._send = () => true; // act const initiateTxResult = spi.InitiateGetTx(posId); // assert expect(initiateTxResult.Message).toMatch(/gt initiated/i); expect(spi.CurrentTxFlowState.Type).toBe(TransactionType.GetTransaction); }); }); describe("InitiateRecovery()", () => { it("should not initiate a Recovery when not paired", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentStatus = SpiStatus.Unpaired; // act const initiateTxResult = spi.InitiateRecovery(posId, TransactionType.GetLastTransaction); // assert expect(initiateTxResult.Message).toMatch(/not paired/i); }); it("should not initiate a Recovery when not idle", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; // act const initiateTxResult = spi.InitiateRecovery(posId, TransactionType.GetLastTransaction); // assert expect(initiateTxResult.Message).toMatch(/not idle/i); }); it("should initiate a Recovery", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Idle; spi._send = () => true; // act const initiateTxResult = spi.InitiateRecovery(posId, TransactionType.GetLastTransaction); // assert expect(initiateTxResult.Message).toMatch(/recovery initiated/i); expect(spi.CurrentTxFlowState.AwaitingGtResponse).toBeTrue; }); }); describe("PrintReceipt()", () => { it("should not initiate a PrintReceipt when not paired", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentStatus = SpiStatus.Unpaired; spi._send = () => true; spyOn(spi, "_send"); // act spi.PrintReceipt("", ""); // assert expect(spi._send).not.toHaveBeenCalled(); }); it("should initiate a PrintReceipt when paired", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentStatus = SpiStatus.PairedConnected; spi._send = () => true; spyOn(spi, "_send"); // act spi.PrintReceipt("", ""); // assert expect(spi._send).toHaveBeenCalledWith( jasmine.objectContaining({ EventName: "print" }) ); }); }); describe("GetTerminalStatus()", () => { it("should not initiate a GetTerminalStatus when not paired", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentStatus = SpiStatus.Unpaired; spi._send = () => true; spyOn(spi, "_send"); // act spi.GetTerminalStatus(); // assert expect(spi._send).not.toHaveBeenCalled(); }); it("should initiate a GetTerminalStatus when paired", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentStatus = SpiStatus.PairedConnected; spi._send = () => true; spyOn(spi, "_send"); // act spi.GetTerminalStatus(); // assert expect(spi._send).toHaveBeenCalledWith( jasmine.objectContaining({ EventName: "get_terminal_status" }) ); }); }); describe("GetAvailableTenants()", () => { it("should return the available tenants", async () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); fetchHelper.resolve({ json: () => Promise.resolve({ data: [ { code: "gko", name: "Gecko Demo Bank", }, ], }), }); // Mock the response from ReportTx // act const tenants = await Spi.GetAvailableTenants("gko", "", "AU"); // assert expect(tenants.Data[0].code).toBe("gko"); }); it("should return empty data when request fails", async () => { const spi = new Spi(posId, "", eftposAddress, null); fetchHelper.resolve({ json: () => Promise.reject({}), }); // Mock the response from ReportTx // act const tenants = await Spi.GetAvailableTenants("gko", "", "AU"); // assert expect(tenants.Data.length).toBe(0); }); }); describe("GetTerminalConfiguration()", () => { it("should not initiate a GetTerminalConfiguration when not paired", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentStatus = SpiStatus.Unpaired; spi._send = () => true; spyOn(spi, "_send"); // act spi.GetTerminalConfiguration(); // assert expect(spi._send).not.toHaveBeenCalled(); }); it("should initiate a GetTerminalConfiguration when paired", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentStatus = SpiStatus.PairedConnected; spi._send = () => true; spyOn(spi, "_send"); // act spi.GetTerminalConfiguration(); // assert expect(spi._send).toHaveBeenCalledWith( jasmine.objectContaining({ EventName: "get_terminal_configuration" }) ); }); }); describe("_handlePurchaseResponse()", () => { it("should handle a purchase response that was unexpected", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Idle; const message = Message.FromJson( JSON.stringify(PurchaseResponseFixture), null ); // act spi._handlePurchaseResponse(message); // assert expect(getLastConsoleCallArgs()).toMatch(/not waiting for one/); }); it("should handle a purchase response where the response was returned", () => { // arrange const { message: { data: { pos_ref_id: posRefId }, }, } = PurchaseResponseFixture const message = Message.FromJson( JSON.stringify(PurchaseResponseFixture), null ); const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; spi.CurrentTxFlowState = new TransactionFlowState( posRefId, TransactionType.Purchase, 30, message, "" ); fetchHelper.resolve({ ok: true }); // Mock the response from ReportTx // act spi._handlePurchaseResponse(message); // assert expect(spi.CurrentTxFlowState.Finished).toBeTrue(); expect(spi.CurrentTxFlowState.DisplayMessage).toMatch( /purchase .* ended/i ); }); }); describe("_handleCashoutOnlyResponse()", () => { it("should handle a cashout only response that was unexpected", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Idle; const message = Message.FromJson( JSON.stringify(CashoutOnlyResponseFixture), null ); // act spi._handleCashoutOnlyResponse(message); // assert expect(getLastConsoleCallArgs()).toMatch(/not waiting for one/); }); it("should handle a cashout only response where the response was returned", () => { // arrange const { message: { data: { pos_ref_id: posRefId, cash_amount }, }, } = CashoutOnlyResponseFixture; const message = Message.FromJson( JSON.stringify(CashoutOnlyResponseFixture), null ); const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; spi.CurrentTxFlowState = new TransactionFlowState( posRefId, TransactionType.CashoutOnly, cash_amount, message, "cash_amount" ); fetchHelper.resolve({ ok: true }); // Mock the response from ReportTx // act spi._handleCashoutOnlyResponse(message); // assert expect(spi.CurrentTxFlowState.Finished).toBeTrue(); expect(spi.CurrentTxFlowState.DisplayMessage).toMatch( /cashout .* ended/i ); }); }); describe("_handleMotoPurchaseResponse()", () => { it("should handle a MOTO response that was unexpected", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Idle; const message = Message.FromJson( JSON.stringify(MotoResponsePhoneFixture), null ); // act spi._handleMotoPurchaseResponse(message); // assert expect(getLastConsoleCallArgs()).toMatch(/not waiting for one/); }); it("should handle a MOTO response where the response was returned", () => { // arrange const { message: { data: { pos_ref_id: posRefId, purchase_amount }, }, } = MotoResponsePhoneFixture; const message = Message.FromJson( JSON.stringify(MotoResponsePhoneFixture), null ); const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; spi.CurrentTxFlowState = new TransactionFlowState( posRefId, TransactionType.MOTO, purchase_amount, message, "" ); fetchHelper.resolve({ ok: true }); // Mock the response from ReportTx // act spi._handleMotoPurchaseResponse(message); // assert expect(spi.CurrentTxFlowState.Finished).toBeTrue(); expect(spi.CurrentTxFlowState.DisplayMessage).toMatch(/moto .* ended/i); }); }); describe("_handleRefundResponse()", () => { it("should handle a refund response that was unexpected", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Idle; const message = Message.FromJson( JSON.stringify(RefundResponseFixture), null ); // act spi._handleRefundResponse(message); // assert expect(getLastConsoleCallArgs()).toMatch(/not waiting for this one/); }); it("should handle a refund response where the response was returned", () => { // arrange const { message: { data: { pos_ref_id: posRefId, refund_amount }, }, } = RefundResponseFixture; const message = Message.FromJson( JSON.stringify(RefundResponseFixture), null ); const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; spi.CurrentTxFlowState = new TransactionFlowState( posRefId, TransactionType.Refund, refund_amount, message, "" ); fetchHelper.resolve({ ok: true }); // Mock the response from ReportTx // act spi._handleRefundResponse(message); // assert expect(spi.CurrentTxFlowState.Finished).toBeTrue(); expect(spi.CurrentTxFlowState.DisplayMessage).toMatch(/refund .* ended/i); }); }); describe("_handleSettleResponse()", () => { it("should handle a settle response that was unexpected", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Idle; const message = Message.FromJson( JSON.stringify(SettleResponseFixture), null ); // act spi._handleSettleResponse(message); // assert expect(getLastConsoleCallArgs()).toMatch(/not waiting for one/); }); it("should handle a settle response where the response was returned", () => { // arrange const { message: { data: { pos_ref_id: posRefId }, }, } = SettleResponseFixture; const message = Message.FromJson( JSON.stringify(SettleResponseFixture), null ); const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; spi.CurrentTxFlowState = new TransactionFlowState( posRefId, TransactionType.Settle, 2000, message, "as" ); fetchHelper.resolve({ ok: true }); // Mock the response from ReportTx // act spi._handleSettleResponse(message); // assert expect(spi.CurrentTxFlowState.Finished).toBeTrue(); expect(spi.CurrentTxFlowState.DisplayMessage).toMatch(/settle .* ended/i); }); }); describe("_handleSettlementEnquiryResponse()", () => { it("should handle a settlement enquiry response that was unexpected", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Idle; const message = Message.FromJson( JSON.stringify(SettlementEnquiryResponseFixture), null ); // act spi._handleSettlementEnquiryResponse(message); // assert expect(getLastConsoleCallArgs()).toMatch(/not waiting for one/); }); it("should handle a settlement enquiry response where the response was returned", () => { // arrange const { message: { data: { pos_ref_id: posRefId }, }, } = SettlementEnquiryResponseFixture; const message = Message.FromJson( JSON.stringify(SettlementEnquiryResponseFixture), null ); const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; spi.CurrentTxFlowState = new TransactionFlowState( posRefId, TransactionType.SettlementEnquiry, 2000, message, "" ); fetchHelper.resolve({ ok: true }); // Mock the response from ReportTx // act spi._handleSettlementEnquiryResponse(message); // assert expect(spi.CurrentTxFlowState.Finished).toBeTrue(); expect(spi.CurrentTxFlowState.DisplayMessage).toMatch( /settlement enquiry ended/i ); }); }); describe("_handleReversalTransaction()", () => { it("should handle a Reversal response that was unexpected", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Idle; const message = Message.FromJson( JSON.stringify(ReversalResponseSuccessFixture), null ); // act spi._handleReversalTransaction(message); // assert expect(getLastConsoleCallArgs()).toMatch(/was not waiting/); }); it("should handle a Reversal response where the reversal was successful", () => { // arrange const { message: { data: { pos_ref_id: posRefId }, }, } = ReversalResponseSuccessFixture; const message = Message.FromJson( JSON.stringify(ReversalResponseSuccessFixture), null ); const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; spi.CurrentTxFlowState = new TransactionFlowState( posRefId, TransactionType.Reversal, 3000, message, "" ); // act spi._handleReversalTransaction(message); // assert expect(spi.CurrentTxFlowState.Finished).toBeTrue(); expect(spi.CurrentTxFlowState.DisplayMessage).toMatch( /reversal .* ended/i ); }); }); describe("_handleGetTransactionResponse()", () => { it("should handle a GT response that is outside of a TX", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Idle; const transactionResponse = new Message('1', '', null, false); // act spi._handleGetTransactionResponse(transactionResponse); // assert expect(getLastConsoleCallArgs()).toMatch(/not in .* tx/); }); it("should handle a GT response that was received inside a TX but is unsolicited", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; const transactionResponse = new Message('1', '', null, false); // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore spi.CurrentTxFlowState = new TransactionFlowState(); // act spi._handleGetTransactionResponse(transactionResponse); // assert expect(getLastConsoleCallArgs()).toMatch(/leftover/); expect(spi.CurrentTxFlowState.Finished).toBeFalse(); }); it("should handle a GT response that is mismatched with the GT Id that was expected", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore spi.CurrentTxFlowState = new TransactionFlowState(); spi.CurrentTxFlowState.CallingGt("DummyId"); const message = Message.FromJson( JSON.stringify(GetTransactionResponsePurchaseSuccessFixture), null ); // act spi._handleGetTransactionResponse(message); // assert expect(getLastConsoleCallArgs()).toMatch(/strange/); expect(spi.CurrentTxFlowState.Finished).toBeFalse(); }); it("should handle a GT response where a signature response is about to be required", () => { // arrange const { message: { data: { pos_ref_id: posRefId }, id: lastGtRequestId, }, } = GetTransactionResponseSignatureRequiredFixture; const message = Message.FromJson( JSON.stringify(GetTransactionResponseSignatureRequiredFixture), null ); const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; spi.CurrentTxFlowState = new TransactionFlowState( posRefId, TransactionType.Purchase, 2000, message, "2asd" ); spi.CurrentTxFlowState.CallingGt(lastGtRequestId); // act spi._handleGetTransactionResponse(message); // assert expect(spi.CurrentTxFlowState.AwaitingSignatureCheck).toBeTrue(); expect(spi.CurrentTxFlowState.Finished).toBeFalse(); }); it("should handle a GT response where a signature is currently being requested on the terminal", () => { // arrange const { message: { data: { pos_ref_id: posRefId }, id: lastGtRequestId, }, } = GetTransactionResponseSignatureRequiredFixture; const message = Message.FromJson( JSON.stringify(GetTransactionResponseSignatureRequiredFixture), null ); const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; spi.CurrentTxFlowState = new TransactionFlowState( posRefId, TransactionType.Purchase, 2000, message, "" ); spi.CurrentTxFlowState.SignatureRequired("SpiMessage", "Message"); spi.CurrentTxFlowState.CallingGt(lastGtRequestId); // act spi._handleGetTransactionResponse(message); // assert expect(getLastConsoleCallArgs()).toMatch(/waiting for signature/); expect(spi.CurrentTxFlowState.Finished).toBeFalse(); }); it("should handle a GT response where a phone authorisation code is currently being requested on the terminal", () => { // arrange const { message: { data: { pos_ref_id: posRefId }, id: lastGtRequestId, }, } = GetTransactionResponseAuthCodeRequiredFixture; const message = Message.FromJson( JSON.stringify(GetTransactionResponseAuthCodeRequiredFixture), null ); const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; spi.CurrentTxFlowState = new TransactionFlowState( posRefId, TransactionType.Purchase, 2000, message, "" ); spi.CurrentTxFlowState.CallingGt(lastGtRequestId); // act spi._handleGetTransactionResponse(message); // assert expect(spi.CurrentTxFlowState.AwaitingPhoneForAuth).toBeTrue(); expect(spi.CurrentTxFlowState.Finished).toBeFalse(); }); it("should handle a GT response where a transaction is currently in progress on the terminal", () => { // arrange const { message: { data: { pos_ref_id: posRefId }, id: lastGtRequestId, }, } = GetTransactionResponseTransactionInProgressFixture; const message = Message.FromJson( JSON.stringify( GetTransactionResponseTransactionInProgressFixture ), null ); const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; spi.CurrentTxFlowState = new TransactionFlowState( posRefId, TransactionType.Purchase, 2000, message, "" ); spi.CurrentTxFlowState.CallingGt(lastGtRequestId); // act spi._handleGetTransactionResponse(message); // assert expect(getLastConsoleCallArgs()).toMatch(/in progress/); expect(spi.CurrentTxFlowState.Finished).toBeFalse(); }); it("should handle a GT response where the PosRefId is not found", () => { // arrange const { message: { data: { pos_ref_id: posRefId }, id: lastGtRequestId, }, } = GetTransactionResponsePosRefIdNotFoundFixture; const message = Message.FromJson( JSON.stringify(GetTransactionResponsePosRefIdNotFoundFixture), null ); const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; spi.CurrentTxFlowState = new TransactionFlowState( posRefId, TransactionType.Purchase, 3000, message, "" ); spi.CurrentTxFlowState.CallingGt(lastGtRequestId); // act spi._handleGetTransactionResponse(message); // assert expect(getLastConsoleCallArgs()).toMatch(/posrefid is not found/); expect(spi.CurrentTxFlowState.Finished).toBeTrue(); }); it("should handle a GT response where the PosRefId is invalid", () => { // arrange const { message: { data: { pos_ref_id: posRefId }, id: lastGtRequestId, }, } = GetTransactionResponsePosRefIdInvalidFixture; const message = Message.FromJson( JSON.stringify(GetTransactionResponsePosRefIdInvalidFixture), null ); const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; spi.CurrentTxFlowState = new TransactionFlowState( posRefId, TransactionType.Purchase, 2000, message, "" ); spi.CurrentTxFlowState.CallingGt(lastGtRequestId); // act spi._handleGetTransactionResponse(message); // assert expect(getLastConsoleCallArgs()).toMatch(/posrefid is invalid/); expect(spi.CurrentTxFlowState.Finished).toBeTrue(); }); it("should handle a GT response where the request is missing a PosRefId", () => { // arrange const { message: { data: { pos_ref_id: posRefId }, id: lastGtRequestId, }, } = GetTransactionResponseMissingPosRefIdFixture; const message = Message.FromJson( JSON.stringify(GetTransactionResponseMissingPosRefIdFixture), null ); const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; spi.CurrentTxFlowState = new TransactionFlowState( posRefId, TransactionType.Purchase, 2000, message, "" ); spi.CurrentTxFlowState.CallingGt(lastGtRequestId); // act spi._handleGetTransactionResponse(message); // assert expect(getLastConsoleCallArgs()).toMatch(/posrefid is missing/); expect(spi.CurrentTxFlowState.Finished).toBeTrue(); }); it("should handle a GT response where an operation is currently in progress on the terminal", () => { // arrange const { message: { data: { pos_ref_id: posRefId }, id: lastGtRequestId, }, } = GetTransactionResponseOpInProgressFixture; const message = Message.FromJson( JSON.stringify(GetTransactionResponseOpInProgressFixture), null ); const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; spi.CurrentTxFlowState = new TransactionFlowState( posRefId, TransactionType.Purchase, 2000, message, "" ); spi.CurrentTxFlowState.CallingGt(lastGtRequestId); // act spi._handleGetTransactionResponse(message); // assert expect(getLastConsoleCallArgs()).toMatch(/blocked/); expect(spi.CurrentTxFlowState.Finished).toBeFalse(); }); it("should handle a GT response where there is no case that handles this response", () => { // arrange const { message: { data: { pos_ref_id: posRefId }, id: lastGtRequestId, }, } = GetTransactionResponseUnexpectedFixture; const message = Message.FromJson( JSON.stringify(GetTransactionResponseUnexpectedFixture), null ); const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; spi.CurrentTxFlowState = new TransactionFlowState( posRefId, TransactionType.Purchase, 2000, message, "" ); spi.CurrentTxFlowState.CallingGt(lastGtRequestId); // act spi._handleGetTransactionResponse(message); // assert expect(getLastConsoleCallArgs()).toMatch(/unexpected/); expect(spi.CurrentTxFlowState.Finished).toBeTrue(); expect(spi.CurrentTxFlowState.Success).toBe(SuccessState.Failed); }); it("should handle a GT response where the transaction data is missing", () => { // arrange const { message: { data: { pos_ref_id: posRefId }, id: lastGtRequestId, }, } = GetTransactionResponseMissingTransactionDataFixture; const message = Message.FromJson( JSON.stringify( GetTransactionResponseMissingTransactionDataFixture ), null ); const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; spi.CurrentTxFlowState = new TransactionFlowState( posRefId, TransactionType.Purchase, 2000, message, "" ); spi.CurrentTxFlowState.CallingGt(lastGtRequestId); // act spi._handleGetTransactionResponse(message); // assert expect(getLastConsoleCallArgs()).toMatch(/missing tx payload/); expect(spi.CurrentTxFlowState.Finished).toBeFalse(); }); it("should handle a GT response that was explicitly requested by the user (completed independent of a TX)", () => { // arrange const { message: { data: { tx: { pos_ref_id: posRefId } }, id: lastGtRequestId, }, } = GetTransactionResponsePurchaseSuccessFixture; const message = Message.FromJson( JSON.stringify(GetTransactionResponsePurchaseSuccessFixture), null ); const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; spi.CurrentTxFlowState = new TransactionFlowState( posRefId, TransactionType.GetTransaction, 2000, message, "" ); // User requested GT spi.CurrentTxFlowState.CallingGt(lastGtRequestId); // act spi._handleGetTransactionResponse(message); // assert expect(getLastConsoleCallArgs()).toMatch(/user/); expect(spi.CurrentTxFlowState.Finished).toBeTrue(); expect(spi.CurrentTxFlowState.AwaitingGtResponse).toBeFalse(); }); it("should handle a GT response that was requested during a recovery", () => { // arrange const { message: { data: { tx: { pos_ref_id: posRefId } }, id: lastGtRequestId, }, } = GetTransactionResponsePurchaseSuccessFixture; const message = Message.FromJson( JSON.stringify(GetTransactionResponsePurchaseSuccessFixture), null ); const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; spi.CurrentTxFlowState = new TransactionFlowState( posRefId, TransactionType.Purchase, 2000, message, "" ); spi.CurrentTxFlowState.CallingGt(lastGtRequestId); // act spi._handleGetTransactionResponse(message); // assert expect(getLastConsoleCallArgs()).toMatch(/recovery/); expect(spi.CurrentTxFlowState.Finished).toBeTrue(); expect(spi.CurrentTxFlowState.AwaitingGtResponse).toBeFalse(); }); }); describe("_handleGetLastTransactionResponse()", () => { it("should handle a GLT response that was unexpected", () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Idle; const transactionResponse = new Message('1', '', null, false); // act spi._handleGetLastTransactionResponse(transactionResponse); // assert expect(getLastConsoleCallArgs()).toMatch(/ignoring/); }); it("should handle a GLT response where the transaction was cancelled on the terminal", () => { // arrange const { message: { id: posRefId, }, } = GetLastTransactionResponseNoTransactionsFixture; const spi = new Spi(posId, "", eftposAddress, null); const message = Message.FromJson( JSON.stringify(GetLastTransactionResponseNoTransactionsFixture), null ); spi.CurrentFlow = SpiFlow.Transaction; spi.CurrentTxFlowState = new TransactionFlowState( posRefId, TransactionType.GetLastTransaction, 2000, message, "" ); fetchHelper.resolve({ ok: true }); // Mock the response from ReportTx // act spi._handleGetLastTransactionResponse(message); // assert expect(getLastConsoleCallArgs()).toMatch(/unknown/); expect(spi.CurrentTxFlowState.Finished).toBeTrue(); expect(spi.CurrentTxFlowState.Success).toBe(SuccessState.Unknown); }); it("should handle a GLT response where the GLT was successfully returned", () => { // arrange const { message: { data: { pos_ref_id: posRefId }, }, } = GetLastTransactionResponsePurchaseSuccessFixture; const message = Message.FromJson( JSON.stringify(GetLastTransactionResponsePurchaseSuccessFixture), null ); const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Transaction; spi.CurrentTxFlowState = new TransactionFlowState( posRefId, TransactionType.GetLastTransaction, 2000, message, "" ); fetchHelper.resolve({ ok: true }); // Report TX mock // act spi._handleGetLastTransactionResponse(message); // assert expect(getLastConsoleCallArgs()).toMatch(/retrieved last transaction/); expect(spi.CurrentTxFlowState.Finished).toBeTrue(); }); }); describe("_sendTransactionReport()", () => { it("should handle a successfully sent transaction report", async () => { // arrange const posRefId = "purchase"; const spi = new Spi(posId, serialNumber, eftposAddress, null); spi.CurrentFlow = SpiFlow.Idle; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore spi.CurrentTxFlowState = new TransactionFlowState( posRefId, TransactionType.Purchase, ); spi.CurrentStatus = SpiStatus.PairedConnected; spi.CurrentTxFlowState.Sent("Message"); spi.CurrentTxFlowState.Completed(SuccessState.Success, null, null); spi._transactionReport = TransactionReportHelper.CreateTransactionReportEnvelope( posVendorId, posVersion, spi._libraryLanguage, Spi.GetVersion(), serialNumber ); // act fetchHelper.resolve({ ok: true }); // Mock the response from ReportTx await spi._sendTransactionReport(); // assert expect(spi._transactionReport).toEqual( jasmine.objectContaining({ PosVendorId: posVendorId, PosVersion: posVersion, LibraryLanguage: spi._libraryLanguage, LibraryVersion: spi._transactionReport.LibraryVersion, // Not testing as library version is dynamic PosRefId: posRefId, SerialNumber: serialNumber, TxType: TransactionType.Purchase, TxResult: SuccessState.Success, CurrentFlow: SpiFlow.Idle, CurrentTxFlowState: TransactionType.Purchase, CurrentStatus: SpiStatus.PairedConnected, }) ); expect(spi._transactionReport.Event).toMatch( /signature: false.*cancel: false.*finished: true/i ); expect(spi._transactionReport.DurationMs).toEqual(jasmine.any(Number)); expect(spi._transactionReport.TxStartTime).toEqual(jasmine.any(Number)); expect(spi._transactionReport.TxEndTime).toEqual(jasmine.any(Number)); expect(window.fetch).toHaveBeenCalled(); }); it("should handle a failed to send transaction report", async () => { // arrange const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Idle; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore spi.CurrentTxFlowState = new TransactionFlowState( "purchase", TransactionType.Purchase ); const errorResponse = { error: { code: 418, message: "Still a teapot, not a terminal", }, }; fetchHelper.resolve({ json: () => Promise.resolve(errorResponse), ok: false, status: 418, }); // Mock the response from ReportTx // act await spi._sendTransactionReport(); // assert expect(getLastConsoleCallArgs("error")).toMatch(/teapot/i); expect(getLastConsoleCallArgs("warn")).toMatch(/error reporting/i); }); }); describe("_handleIncomingPosSpiToken", () => { it("should log the last 4 digits of the token", () => { const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Idle; const message = new Message("1", Events.ProvisionPosSpiToken, { pos_spi_auth_token: "12345678910" }, false) spi._handleIncomingPosSpiToken(message) expect(getLastConsoleCallArgs()).toMatch(/\*\*\*8910/i); }) it("should forward that token to the POS via PosSpiToken event", () => { const spi = new Spi(posId, "", eftposAddress, null); spi.CurrentFlow = SpiFlow.Idle; const message = new Message("1", Events.ProvisionPosSpiToken, { pos_spi_auth_token: "12345678910" }, false) spi._handleIncomingPosSpiToken(message) expect(document.dispatchEvent).toHaveBeenCalledWith(new CustomEvent("PosSpiToken")) }) }) });