1import { describe, expect, it } from "@effect/vitest";
2import { EnvironmentId } from "@t3tools/contracts";
3import * as Effect from "effect/Effect";
4
5import {
6 loadPreviewWebviewConfig,
7 PreviewWebviewBridgeUnavailableError,
8 PreviewWebviewConfigLoadError,
9} from "./previewWebviewConfigState";
10
11const environmentId = EnvironmentId.make("environment-1");
12
13describe("loadPreviewWebviewConfig", () => {
14 it.effect("reports a structurally distinct missing-bridge failure", () =>
15 Effect.gen(function* () {
16 const error = yield* loadPreviewWebviewConfig(environmentId, undefined, null).pipe(
17 Effect.flip,
18 );
19
20 expect(error).toBeInstanceOf(PreviewWebviewBridgeUnavailableError);
21 expect(error.environmentId).toBe(environmentId);
22 expect(error.message).toContain(environmentId);
23 expect("cause" in error).toBe(false);
24 }),
25 );
26
27 it.effect("preserves the bridge rejection as the load failure cause", () =>
28 Effect.gen(function* () {
29 const cause = new Error("ipc unavailable");
30 const error = yield* loadPreviewWebviewConfig(environmentId, undefined, {
31 getPreviewConfig: () => Promise.reject(cause),
32 }).pipe(Effect.flip);
33
34 expect(error).toBeInstanceOf(PreviewWebviewConfigLoadError);
35 expect(error.environmentId).toBe(environmentId);
36 expect(error.cause).toBe(cause);
37 expect(error.message).not.toContain(cause.message);
38 }),
39 );
40
41 it.effect("forwards the environment id and profile to the bridge", () =>
42 Effect.gen(function* () {
43 let requested: { environmentId: EnvironmentId; profileId: string | undefined } | null = null;
44 const config = {
45 partition: "persist:test-preview",
46 webPreferences: "sandbox=yes",
47 preloadUrl: null,
48 };
49 const result = yield* loadPreviewWebviewConfig(environmentId, "work", {
50 getPreviewConfig: (requestedEnvironmentId, profileId) => {
51 requested = { environmentId: requestedEnvironmentId, profileId };
52 return Promise.resolve(config);
53 },
54 });
55
56 // The partition is derived in main from both, so both have to arrive.
57 expect(requested).toEqual({ environmentId, profileId: "work" });
58 expect(result).toEqual(config);
59 }),
60 );
61});