95 lines · 4.8 KBRaw
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { captureRepositoryEvent } from "../analytics.ts"; |
| 4 | import type { Actor } from "../contract/actor.ts"; |
| 5 | |
| 6 | const config = { POSTHOG_ENABLED: "true", POSTHOG_PROJECT_TOKEN: "project-token", POSTHOG_HOST: "https://us.i.posthog.com" }; |
| 7 | const human: Actor = { kind: "human", person: { id: "user-id", name: "Private name", email: "private@example.test" }, via: "session" }; |
| 8 | const repository = { namespaceId: "namespace-id", namespaceKind: "user", visibility: "private", entryPath: "import" } as const; |
| 9 | |
| 10 | test("repository imports classify the provider without sending source URLs or account details", async (t) => { |
| 11 | const payloads: { event: string; distinct_id: string; properties: Record<string, unknown> }[] = []; |
| 12 | t.mock.method(globalThis, "fetch", async (_url: string, init: RequestInit) => { |
| 13 | payloads.push(JSON.parse(String(init.body))); |
| 14 | assert.doesNotMatch(String(init.body), /private@example|Private name|private-repo|secret-query|github\.com/); |
| 15 | return new Response(null, { status: 204 }); |
| 16 | }); |
| 17 | const pending: Promise<unknown>[] = []; |
| 18 | const ctx = { waitUntil: (promise: Promise<unknown>) => pending.push(promise) }; |
| 19 | for (const [url, provider] of [ |
| 20 | ["https://github.com/private-owner/private-repo?token=secret-query", "github"], |
| 21 | ["https://gitlab.com/private-owner/private-repo", "gitlab"], |
| 22 | ["https://bitbucket.org/private-owner/private-repo", "bitbucket"], |
| 23 | ["https://github.com.untrusted.test/private-repo", "other"], |
| 24 | ["https://enterprise.test/private-repo", "other"], |
| 25 | ["invalid", "other"], |
| 26 | ]) { |
| 27 | captureRepositoryEvent(config, ctx, human, { ...repository, name: "repo.imported", repoId: "repo-id", importUrl: url }); |
| 28 | assert.equal(payloads.at(-1)?.properties.import_provider, provider); |
| 29 | } |
| 30 | await Promise.all(pending); |
| 31 | const [imported] = payloads.filter((payload) => payload.event === "repo.imported"); |
| 32 | assert.deepEqual(imported, { |
| 33 | ...imported, |
| 34 | distinct_id: "human:user-id", |
| 35 | properties: { |
| 36 | schema_version: 1, |
| 37 | $geoip_disable: true, |
| 38 | actor_kind: "human", |
| 39 | subject_kind: "human", |
| 40 | is_user_activity: true, |
| 41 | auth_method: "session", |
| 42 | namespace_id: "namespace-id", |
| 43 | namespace_kind: "user", |
| 44 | visibility: "private", |
| 45 | entry_path: "import", |
| 46 | import_provider: "github", |
| 47 | repo_id: "repo-id", |
| 48 | $groups: { workspace: "namespace-id" }, |
| 49 | }, |
| 50 | }); |
| 51 | }); |
| 52 | |
| 53 | test("agent repository work remains separate from human activity", async (t) => { |
| 54 | let payload: { distinct_id: string; properties: Record<string, unknown> } | undefined; |
| 55 | t.mock.method(globalThis, "fetch", async (_url: string, init: RequestInit) => { |
| 56 | payload = JSON.parse(String(init.body)); |
| 57 | return new Response(null, { status: 204 }); |
| 58 | }); |
| 59 | const pending: Promise<unknown>[] = []; |
| 60 | const actor: Actor = { |
| 61 | kind: "agent", |
| 62 | agent: { id: "agent-id", name: "Private agent name", runtime: "private-runtime" }, |
| 63 | onBehalfOf: human.person, |
| 64 | sessionId: "session-id", |
| 65 | grant: { repos: "all", org: "admin" }, |
| 66 | }; |
| 67 | captureRepositoryEvent(config, { waitUntil: (promise) => pending.push(promise) }, actor, { ...repository, entryPath: "new_project", name: "repo.created", repoId: "repo-id" }); |
| 68 | await Promise.all(pending); |
| 69 | assert.equal(payload?.distinct_id, "agent:session-id"); |
| 70 | assert.equal(payload?.properties.owner_id, "human:user-id"); |
| 71 | assert.equal(payload?.properties.actor_kind, "agent"); |
| 72 | assert.equal(payload?.properties.is_user_activity, false); |
| 73 | assert.equal(payload?.properties.$process_person_profile, false); |
| 74 | assert.equal(payload?.properties.import_provider, undefined); |
| 75 | assert.doesNotMatch(JSON.stringify(payload), /Private|private-runtime|private@example/); |
| 76 | }); |
| 77 | |
| 78 | test("provisioning failures use fixed reason codes without claiming a repository exists", async (t) => { |
| 79 | let properties: Record<string, unknown> | undefined; |
| 80 | t.mock.method(globalThis, "fetch", async (_url: string, init: RequestInit) => { |
| 81 | properties = JSON.parse(String(init.body)).properties; |
| 82 | return new Response(null, { status: 204 }); |
| 83 | }); |
| 84 | const pending: Promise<unknown>[] = []; |
| 85 | captureRepositoryEvent(config, { waitUntil: (promise) => pending.push(promise) }, human, { ...repository, name: "repo.provision_failed", reason: "storage_unavailable" }); |
| 86 | await Promise.all(pending); |
| 87 | assert.equal(properties?.failure_reason, "storage_unavailable"); |
| 88 | assert.equal(properties?.repo_id, undefined); |
| 89 | }); |
| 90 | |
| 91 | test("anonymous repository activity is not captured", (t) => { |
| 92 | const fetch = t.mock.method(globalThis, "fetch", async () => new Response(null, { status: 204 })); |
| 93 | captureRepositoryEvent(config, { waitUntil: () => assert.fail("Unexpected delivery") }, { kind: "anonymous" }, { ...repository, name: "repo.imported", repoId: "repo-id" }); |
| 94 | assert.equal(fetch.mock.callCount(), 0); |
| 95 | }); |