1import { describe, expect, it } from "@effect/vitest";
2
3import { deriveAssetUrlState } from "./asset-url-state";
4
5const SUCCESS = {
6 _tag: "Success" as const,
7 url: "https://environment.example/api/assets/abc",
8 expiresAt: Number.MAX_SAFE_INTEGER,
9};
10
11describe("deriveAssetUrlState", () => {
12 it("passes a resolved URL through on a live environment", () => {
13 expect(deriveAssetUrlState({ connectionPhase: "connected", shared: SUCCESS })).toEqual(SUCCESS);
14 });
15
16 it("waits while the query is pending and the environment is still reachable", () => {
17 for (const connectionPhase of ["available", "connecting", "connected"] as const) {
18 expect(deriveAssetUrlState({ connectionPhase, shared: { _tag: "Loading" } })).toEqual({
19 _tag: "Loading",
20 });
21 }
22 });
23
24 it("stops waiting once the environment is offline, retrying, or in error", () => {
25 for (const connectionPhase of ["offline", "reconnecting", "error"] as const) {
26 expect(deriveAssetUrlState({ connectionPhase, shared: { _tag: "Loading" } })).toEqual({
27 _tag: "Failure",
28 reason: "disconnected",
29 });
30 }
31 });
32
33 // A dead environment fails the URL query itself, so the query outcome alone
34 // cannot tell a missing file from a missing connection.
35 it("reports disconnected when the query failed while the environment is down", () => {
36 for (const connectionPhase of ["available", "offline", "reconnecting", "error"] as const) {
37 expect(deriveAssetUrlState({ connectionPhase, shared: { _tag: "Failure" } })).toEqual({
38 _tag: "Failure",
39 reason: "disconnected",
40 });
41 }
42 });
43
44 it("does not hand out a resolved URL for a disconnected environment", () => {
45 for (const connectionPhase of ["offline", "reconnecting", "error"] as const) {
46 expect(deriveAssetUrlState({ connectionPhase, shared: SUCCESS })).toEqual({
47 _tag: "Failure",
48 reason: "disconnected",
49 });
50 }
51 });
52
53 it("reports a failed query on a connected environment", () => {
54 expect(
55 deriveAssetUrlState({ connectionPhase: "connected", shared: { _tag: "Failure" } }),
56 ).toEqual({ _tag: "Failure", reason: "failed" });
57 });
58});