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

workspaceModel.test.ts

124 lines4.0 KB

1import type { EnvironmentShellSummary } from "@t3tools/client-runtime/state/shell";
2import {
3 BearerConnectionProfile,
4 BearerConnectionTarget,
5} from "@t3tools/client-runtime/connection";
6import { EnvironmentId } from "@t3tools/contracts";
7import { describe, expect, it } from "@effect/vitest";
8import * as Option from "effect/Option";
9​
10import { projectWorkspaceEnvironment, projectWorkspaceState } from "./workspaceModel";
11import type { EnvironmentPresentation } from "./environments";
12​
13const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
14​
15function environment(
16 phase: EnvironmentPresentation["connection"]["phase"],
17): EnvironmentPresentation {
18 const connectionId = `bearer:${ENVIRONMENT_ID}`;
19 return {
20 environmentId: ENVIRONMENT_ID,
21 label: "Julius's MacBook Pro",
22 displayUrl: "https://environment.example.test",
23 relayManaged: false,
24 entry: {
25 target: new BearerConnectionTarget({
26 environmentId: ENVIRONMENT_ID,
27 label: "Julius's MacBook Pro",
28 connectionId,
29 }),
30 profile: Option.some(
31 new BearerConnectionProfile({
32 connectionId,
33 environmentId: ENVIRONMENT_ID,
34 label: "Julius's MacBook Pro",
35 httpBaseUrl: "https://environment.example.test",
36 wsBaseUrl: "wss://environment.example.test",
37 }),
38 ),
39 enabled: true,
40 },
41 connection: {
42 phase,
43 error: phase === "error" ? "Connection failed." : null,
44 traceId: phase === "error" ? "trace-1" : null,
45 },
46 serverConfig: null,
47 };
49​
50const EMPTY_SHELL_SUMMARY: EnvironmentShellSummary = {
51 hasSnapshot: false,
52 hasSynchronizingShell: false,
53 hasCachedShell: false,
54 hasLiveShell: false,
55 firstError: null,
56 latestSnapshotUpdatedAt: null,
57};
58​
59const CACHED_SHELL_SUMMARY: EnvironmentShellSummary = {
60 ...EMPTY_SHELL_SUMMARY,
61 hasSnapshot: true,
62 hasSynchronizingShell: true,
63 hasCachedShell: true,
64 latestSnapshotUpdatedAt: "2026-06-07T00:00:00.000Z",
65};
66​
67describe("mobile workspace projection", () => {
68 it("preserves explicit offline state without presenting it as a connection error", () => {
69 const projected = projectWorkspaceEnvironment(environment("offline"));
70​
71 expect(projected.connectionState).toBe("offline");
72 expect(projected.connectionError).toBeNull();
73 });
74​
75 it("reports offline before stale connected presentations", () => {
76 const environments = [projectWorkspaceEnvironment(environment("connected"))];
77 const state = projectWorkspaceState({
78 isReady: true,
79 networkStatus: "offline",
80 environments,
81 shellSummary: EMPTY_SHELL_SUMMARY,
82 });
83​
84 expect(state.connectionState).toBe("offline");
85 expect(state.networkStatus).toBe("offline");
86 expect(state.hasReadyEnvironment).toBe(false);
87 });
88​
89 it("projects reconnecting environments dynamically from active phases", () => {
90 const environments = [
91 projectWorkspaceEnvironment(environment("reconnecting")),
92 projectWorkspaceEnvironment({
93 ...environment("connected"),
94 environmentId: EnvironmentId.make("environment-2"),
95 }),
96 ];
97 const state = projectWorkspaceState({
98 isReady: true,
99 networkStatus: "online",
100 environments,
101 shellSummary: EMPTY_SHELL_SUMMARY,
102 });
103​
104 expect(state.connectingEnvironments).toHaveLength(1);
105 expect(state.connectingEnvironments[0]?.connectionState).toBe("reconnecting");
106 expect(state.hasConnectingEnvironment).toBe(true);
107 expect(state.hasReadyEnvironment).toBe(true);
108 });
109​
110 it("keeps retained snapshots visible while reconnecting without claiming readiness", () => {
111 const environments = [projectWorkspaceEnvironment(environment("reconnecting"))];
112 const state = projectWorkspaceState({
113 isReady: true,
114 networkStatus: "online",
115 environments,
116 shellSummary: CACHED_SHELL_SUMMARY,
117 });
118​
119 expect(state.hasLoadedShellSnapshot).toBe(true);
120 expect(state.hasPendingShellSnapshot).toBe(true);
121 expect(state.hasReadyEnvironment).toBe(false);
122 expect(state.connectionState).toBe("reconnecting");
123 });
124});