Open History to see the last commit
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 },
40 connection: {
41 phase,
42 error: phase === "error" ? "Connection failed." : null,
43 traceId: phase === "error" ? "trace-1" : null,
44 },
45 serverConfig: null,
46 };
47}
48
49const EMPTY_SHELL_SUMMARY: EnvironmentShellSummary = {
50 hasSnapshot: false,
51 hasSynchronizingShell: false,
52 hasCachedShell: false,
53 hasLiveShell: false,
54 firstError: null,
55 latestSnapshotUpdatedAt: null,
56};
57
58const CACHED_SHELL_SUMMARY: EnvironmentShellSummary = {
59 ...EMPTY_SHELL_SUMMARY,
60 hasSnapshot: true,
61 hasSynchronizingShell: true,
62 hasCachedShell: true,
63 latestSnapshotUpdatedAt: "2026-06-07T00:00:00.000Z",
64};
65
66describe("mobile workspace projection", () => {
67 it("preserves explicit offline state without presenting it as a connection error", () => {
68 const projected = projectWorkspaceEnvironment(environment("offline"));
69
70 expect(projected.connectionState).toBe("offline");
71 expect(projected.connectionError).toBeNull();
72 });
73
74 it("reports offline before stale connected presentations", () => {
75 const environments = [projectWorkspaceEnvironment(environment("connected"))];
76 const state = projectWorkspaceState({
77 isReady: true,
78 networkStatus: "offline",
79 environments,
80 shellSummary: EMPTY_SHELL_SUMMARY,
81 });
82
83 expect(state.connectionState).toBe("offline");
84 expect(state.networkStatus).toBe("offline");
85 expect(state.hasReadyEnvironment).toBe(false);
86 });
87
88 it("projects reconnecting environments dynamically from active phases", () => {
89 const environments = [
90 projectWorkspaceEnvironment(environment("reconnecting")),
91 projectWorkspaceEnvironment({
92 ...environment("connected"),
93 environmentId: EnvironmentId.make("environment-2"),
94 }),
95 ];
96 const state = projectWorkspaceState({
97 isReady: true,
98 networkStatus: "online",
99 environments,
100 shellSummary: EMPTY_SHELL_SUMMARY,
101 });
102
103 expect(state.connectingEnvironments).toHaveLength(1);
104 expect(state.connectingEnvironments[0]?.connectionState).toBe("reconnecting");
105 expect(state.hasConnectingEnvironment).toBe(true);
106 expect(state.hasReadyEnvironment).toBe(true);
107 });
108
109 it("keeps retained snapshots visible while reconnecting without claiming readiness", () => {
110 const environments = [projectWorkspaceEnvironment(environment("reconnecting"))];
111 const state = projectWorkspaceState({
112 isReady: true,
113 networkStatus: "online",
114 environments,
115 shellSummary: CACHED_SHELL_SUMMARY,
116 });
117
118 expect(state.hasLoadedShellSnapshot).toBe(true);
119 expect(state.hasPendingShellSnapshot).toBe(true);
120 expect(state.hasReadyEnvironment).toBe(false);
121 expect(state.connectionState).toBe("reconnecting");
122 });
123});