Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

asset-url-state.test.ts

54 lines2.1 KB

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