|
1 | 1 | import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import { BunServices } from "@effect/platform-bun"; |
| 3 | +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import path from "node:path"; |
2 | 6 | import { Effect, Layer } from "effect"; |
3 | 7 | import { cliConfigLayer } from "../../next/config/cli-config.layer.ts"; |
4 | 8 | import { |
5 | 9 | mockProjectContext, |
6 | 10 | mockRuntimeInfo, |
7 | 11 | processEnvLayer, |
8 | 12 | } from "../../../tests/helpers/mocks.ts"; |
9 | | -import { getEffectiveConsent } from "./consent.ts"; |
| 13 | +import { getEffectiveConsent, readTelemetryConfig } from "./consent.ts"; |
10 | 14 | import type { TelemetryConfig } from "./types.ts"; |
11 | 15 |
|
12 | 16 | function makeConfig(consent: TelemetryConfig["consent"]): TelemetryConfig { |
@@ -40,6 +44,14 @@ function emptyEnv() { |
40 | 44 | ); |
41 | 45 | } |
42 | 46 |
|
| 47 | +function makeTempDir(): string { |
| 48 | + return mkdtempSync(path.join(tmpdir(), "supabase-consent-test-")); |
| 49 | +} |
| 50 | + |
| 51 | +function writeTelemetryFile(dir: string, content: string): void { |
| 52 | + writeFileSync(path.join(dir, "telemetry.json"), content); |
| 53 | +} |
| 54 | + |
43 | 55 | describe("getEffectiveConsent", () => { |
44 | 56 | it.live("returns denied when DO_NOT_TRACK=1", () => |
45 | 57 | Effect.gen(function* () { |
@@ -90,3 +102,18 @@ describe("getEffectiveConsent", () => { |
90 | 102 | }).pipe(Effect.provide(emptyEnv())), |
91 | 103 | ); |
92 | 104 | }); |
| 105 | + |
| 106 | +describe("readTelemetryConfig", () => { |
| 107 | + it.live("returns null for malformed JSON instead of throwing", () => { |
| 108 | + const dir = makeTempDir(); |
| 109 | + writeTelemetryFile(dir, ""); |
| 110 | + |
| 111 | + return Effect.gen(function* () { |
| 112 | + const config = yield* readTelemetryConfig(dir); |
| 113 | + expect(config).toBeNull(); |
| 114 | + }).pipe( |
| 115 | + Effect.provide(BunServices.layer), |
| 116 | + Effect.ensuring(Effect.sync(() => rmSync(dir, { recursive: true, force: true }))), |
| 117 | + ); |
| 118 | + }); |
| 119 | +}); |
0 commit comments