|
| 1 | +import type { ServerBuild } from "@remix-run/server-runtime"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { createSdkDefaultAttribution } from "~/services/sdkDefaultAttribution.server"; |
| 4 | + |
| 5 | +const routes = { |
| 6 | + root: { id: "root", path: "", module: {} }, |
| 7 | + runs: { id: "routes/api.runs.$runId", parentId: "root", path: "api/runs/:runId", module: {} }, |
| 8 | + packets: { id: "routes/api.packets.$", parentId: "root", path: "api/packets/*", module: {} }, |
| 9 | +} satisfies ServerBuild["routes"]; |
| 10 | + |
| 11 | +// Catches: concrete IDs, query data, or arbitrary version suffixes leaking into default attribution. |
| 12 | +describe("SDK default destination attribution", () => { |
| 13 | + it("uses manifest route IDs and numeric SDK release versions without request data", () => { |
| 14 | + const attribute = createSdkDefaultAttribution(routes); |
| 15 | + const result = attribute( |
| 16 | + new Request("https://example.com/api/runs/run_secret?token=secret", { |
| 17 | + method: "POST", |
| 18 | + headers: { "trigger-version": "4.5.0-beta.secret+private", Authorization: "Bearer secret" }, |
| 19 | + }) |
| 20 | + ); |
| 21 | + expect(result).toEqual({ |
| 22 | + attributes: { http_method: "POST", route: "routes/api.runs.$runId" }, |
| 23 | + diagnostic: { |
| 24 | + http_method: "POST", |
| 25 | + route: "routes/api.runs.$runId", |
| 26 | + sdk_version: "4.5.0", |
| 27 | + linesHeldBack: 0, |
| 28 | + }, |
| 29 | + }); |
| 30 | + expect( |
| 31 | + attribute(new Request("https://example.com/api/packets/private/nested")).attributes.route |
| 32 | + ).toBe("routes/api.packets.$"); |
| 33 | + expect(attribute(new Request("https://example.com/private")).attributes.route).toBe( |
| 34 | + "unmatched" |
| 35 | + ); |
| 36 | + expect( |
| 37 | + attribute(new Request("https://example.com/private", { method: "CUSTOM" })).attributes |
| 38 | + .http_method |
| 39 | + ).toBe("OTHER"); |
| 40 | + }); |
| 41 | + |
| 42 | + it("bounds diagnostics globally even when callers vary routes and versions", () => { |
| 43 | + let now = 0; |
| 44 | + const attribute = createSdkDefaultAttribution(routes, () => now); |
| 45 | + for (let i = 0; i < 5; i++) { |
| 46 | + expect( |
| 47 | + attribute( |
| 48 | + new Request(`https://example.com/api/runs/${i}`, { |
| 49 | + headers: { "trigger-version": `4.0.${i}` }, |
| 50 | + }) |
| 51 | + ).diagnostic |
| 52 | + ).toBeDefined(); |
| 53 | + } |
| 54 | + const suppressed = attribute(new Request("https://example.com/api/packets/secret")); |
| 55 | + expect(suppressed.attributes.route).toBe("routes/api.packets.$"); |
| 56 | + expect(suppressed.diagnostic).toBeUndefined(); |
| 57 | + now = 60_000; |
| 58 | + expect(attribute(new Request("https://example.com/api/runs/next")).diagnostic).toMatchObject({ |
| 59 | + sdk_version: "missing", |
| 60 | + linesHeldBack: 1, |
| 61 | + }); |
| 62 | + expect( |
| 63 | + attribute( |
| 64 | + new Request("https://example.com/api/runs/next", { |
| 65 | + headers: { "trigger-version": "arbitrary-secret" }, |
| 66 | + }) |
| 67 | + ).diagnostic?.sdk_version |
| 68 | + ).toBe("invalid"); |
| 69 | + }); |
| 70 | +}); |
0 commit comments