|
| 1 | +import { test } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { TeslemetryTrigger } from "../src/nodes/TeslemetryTrigger.node.js"; |
| 4 | + |
| 5 | +function withMockedFetch<T>( |
| 6 | + handler: (request: Request) => Promise<Response> | Response, |
| 7 | + run: () => Promise<T>, |
| 8 | +): Promise<T> { |
| 9 | + const original = globalThis.fetch; |
| 10 | + globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => |
| 11 | + handler(new Request(input, init))) as typeof fetch; |
| 12 | + return run().finally(() => { |
| 13 | + globalThis.fetch = original; |
| 14 | + }); |
| 15 | +} |
| 16 | + |
| 17 | +async function waitFor(condition: () => boolean, timeoutMs = 2000): Promise<void> { |
| 18 | + const start = Date.now(); |
| 19 | + while (!condition()) { |
| 20 | + if (Date.now() - start > timeoutMs) { |
| 21 | + throw new Error("Timed out waiting for condition"); |
| 22 | + } |
| 23 | + await new Promise((resolve) => setTimeout(resolve, 10)); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +function fakeTriggerContext(params: Record<string, unknown>) { |
| 28 | + const emitted: unknown[] = []; |
| 29 | + const emittedErrors: Error[] = []; |
| 30 | + const warnings: string[] = []; |
| 31 | + const context = { |
| 32 | + getCredentials: async () => ({ accessToken: "token" }), |
| 33 | + getNodeParameter: (name: string, fallback?: unknown) => |
| 34 | + name in params ? params[name] : fallback, |
| 35 | + emit: (data: unknown) => emitted.push(data), |
| 36 | + emitError: (error: Error) => emittedErrors.push(error), |
| 37 | + logger: { |
| 38 | + info: () => {}, |
| 39 | + warn: (message: string) => warnings.push(message), |
| 40 | + error: () => {}, |
| 41 | + debug: () => {}, |
| 42 | + }, |
| 43 | + helpers: { |
| 44 | + returnJsonArray: (data: unknown) => data, |
| 45 | + }, |
| 46 | + }; |
| 47 | + return { context: context as never, emitted, emittedErrors, warnings }; |
| 48 | +} |
| 49 | + |
| 50 | +test("TeslemetryTrigger surfaces a terminal auth failure via emitError", async () => { |
| 51 | + const { context, emittedErrors } = fakeTriggerContext({ |
| 52 | + resource: "vehicle", |
| 53 | + event: "all", |
| 54 | + vin: "", |
| 55 | + }); |
| 56 | + const node = new TeslemetryTrigger(); |
| 57 | + |
| 58 | + const result = await withMockedFetch( |
| 59 | + () => new Response(null, { status: 401, statusText: "Unauthorized" }), |
| 60 | + () => node.trigger.call(context), |
| 61 | + ); |
| 62 | + |
| 63 | + await waitFor(() => emittedErrors.length > 0); |
| 64 | + assert.equal(emittedErrors.length, 1); |
| 65 | + assert.match(emittedErrors[0].message, /401|unauthorized/i); |
| 66 | + |
| 67 | + // Idempotent cleanup: calling closeFunction twice must not throw. |
| 68 | + await result.closeFunction!(); |
| 69 | + await result.closeFunction!(); |
| 70 | +}); |
| 71 | + |
| 72 | +test("TeslemetryTrigger logs stream disconnects without surfacing them as trigger errors", async () => { |
| 73 | + const { context, emittedErrors, warnings } = fakeTriggerContext({ |
| 74 | + resource: "vehicle", |
| 75 | + event: "all", |
| 76 | + vin: "", |
| 77 | + }); |
| 78 | + const node = new TeslemetryTrigger(); |
| 79 | + |
| 80 | + const result = await withMockedFetch( |
| 81 | + () => new Response(null, { status: 500, statusText: "Server Error" }), |
| 82 | + () => node.trigger.call(context), |
| 83 | + ); |
| 84 | + |
| 85 | + await waitFor(() => warnings.some((w) => w.includes("disconnected"))); |
| 86 | + assert.equal(emittedErrors.length, 0); |
| 87 | + |
| 88 | + await result.closeFunction!(); |
| 89 | +}); |
| 90 | + |
| 91 | +test("TeslemetryTrigger.closeFunction tears down listeners and stops the stream", async () => { |
| 92 | + const { context, emitted } = fakeTriggerContext({ |
| 93 | + resource: "vehicle", |
| 94 | + event: "all", |
| 95 | + vin: "", |
| 96 | + }); |
| 97 | + const node = new TeslemetryTrigger(); |
| 98 | + |
| 99 | + const result = await withMockedFetch( |
| 100 | + () => |
| 101 | + new Response(`data: ${JSON.stringify({ vin: "5YJSA1E14FF000000", state: "online" })}\n\n`, { |
| 102 | + status: 200, |
| 103 | + headers: { "Content-Type": "text/event-stream" }, |
| 104 | + }), |
| 105 | + () => node.trigger.call(context), |
| 106 | + ); |
| 107 | + |
| 108 | + await waitFor(() => emitted.length > 0); |
| 109 | + |
| 110 | + await result.closeFunction!(); |
| 111 | + const countAfterClose = emitted.length; |
| 112 | + |
| 113 | + // A second close is a no-op, not a re-teardown attempt. |
| 114 | + await result.closeFunction!(); |
| 115 | + assert.equal(emitted.length, countAfterClose); |
| 116 | +}); |
0 commit comments