Files
open-design/apps/packaged/tests/download-attribution.test.ts
d7151b2b60 feat(analytics): link downloads and first-party browser opens (#5442)
* feat(analytics): attribute desktop downloads

* fix(analytics): initialize attribution routes after HTTP deps

* feat(analytics): bridge first-party browser opens

* feat(analytics): bridge first-party anchor opens

* fix(analytics): make attribution claims idempotent

* fix(analytics): record pending attribution results

* chore(analytics): bind landing attribution KV

* fix(ci): remove landing preview wrangler config override

* fix(landing): resolve download attribution function

* fix(analytics): harden attribution token consumption

* fix(analytics): preserve same-installation claim retries

* fix(analytics): merge recovered attribution retries

* fix(analytics): preserve attributed browser links

* fix(analytics): clear attribution on privacy reset

* fix(analytics): bridge stopped AMR console links

* fix(ci): deploy attribution schema before Pages

* fix(ci): run attribution migrations with Wrangler 4

---------

Co-authored-by: Siri-Ray <2667192167@qq.com>
Co-authored-by: PerishCode <perishcode@gmail.com>
2026-07-14 08:46:04 +00:00

63 lines
2.4 KiB
TypeScript

import { mkdtemp, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import {
claimPackagedDownloadAttribution,
discoverPackagedDownloadAttribution,
extractDownloadAttributionTokenFromUrl,
} from "../src/download-attribution.js";
const originalFetch = globalThis.fetch;
afterEach(() => {
globalThis.fetch = originalFetch;
});
describe("download attribution token extraction", () => {
it("extracts the token from the distributor path segment", () => {
expect(
extractDownloadAttributionTokenFromUrl(
"https://download.open-design.ai/mac/arm64/oddl_Abc12345/Open-Design.dmg",
),
).toBe("oddl_Abc12345");
expect(
extractDownloadAttributionTokenFromUrl(
"https://download.open-design.ai/windows/x64/token-123456/Open%20Design-setup.exe",
),
).toBe("token-123456");
});
it("does not treat GitHub release paths as attributed download URLs", () => {
expect(
extractDownloadAttributionTokenFromUrl(
"https://github.com/nexu-io/open-design/releases/download/open-design-v1/Open-Design-mac-arm64.dmg",
),
).toBeNull();
});
});
describe("packaged download attribution lifecycle", () => {
it("does not re-submit a terminal installer observation on the next launch", async () => {
const installerObservationRoot = await mkdtemp(join(tmpdir(), "od-download-attribution-"));
const rawUrl = "https://download.open-design.ai/windows/x64/oddl_terminal_123/Open-Design-setup.exe";
await writeFile(join(installerObservationRoot, "download-attribution.json"), JSON.stringify({
token: "oddl_terminal_123", rawUrl,
}));
const paths = { installerObservationRoot } as Parameters<typeof discoverPackagedDownloadAttribution>[0];
const first = await discoverPackagedDownloadAttribution(paths);
expect(first?.token).toBe("oddl_terminal_123");
globalThis.fetch = vi.fn(async () => new Response(JSON.stringify({
ok: true, status: "claimed", found: true, pending: false, merged: true,
}), { status: 200 })) as typeof fetch;
await claimPackagedDownloadAttribution({
attribution: first,
daemonUrl: "http://127.0.0.1:17000",
installerObservationRoot,
});
expect(await discoverPackagedDownloadAttribution(paths)).toBeNull();
expect(globalThis.fetch).toHaveBeenCalledTimes(1);
});
});