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

desktopCaffeinate.test.ts

125 lines4.3 KB

1import {
2 BearerConnectionTarget,
3 PrimaryConnectionTarget,
4 RelayConnectionTarget,
5 SshConnectionTarget,
6} from "@t3tools/client-runtime/connection";
7import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
8import { EnvironmentId, TurnId } from "@t3tools/contracts";
9import { describe, expect, it } from "vite-plus/test";
10​
11import {
12 desktopLocalConnectionId,
13 isDesktopHostedConnectionTarget,
14} from "../connection/desktopLocal";
15import {
16 computeKeepAwake,
17 isThreadShellWorking,
18 KEEP_AWAKE_BATTERY_MIN_LEVEL,
19} from "./desktopCaffeinate";
20​
21function shellWithSession(
22 session: Partial<NonNullable<EnvironmentThreadShell["session"]>> | null,
23): Pick<EnvironmentThreadShell, "session"> {
24 return { session: session as EnvironmentThreadShell["session"] };
26​
27describe("computeKeepAwake", () => {
28 const working = { enabled: true, anyLocalAgentWorking: true };
29​
30 it("stays off while the setting is disabled or no local agent is working", () => {
31 expect(computeKeepAwake({ enabled: false, anyLocalAgentWorking: true, battery: null })).toBe(
32 false,
33 );
34 expect(computeKeepAwake({ enabled: true, anyLocalAgentWorking: false, battery: null })).toBe(
35 false,
36 );
37 });
38​
39 it("releases when discharging below the battery cutoff", () => {
40 expect(computeKeepAwake({ ...working, battery: { charging: false, level: 0.09 } })).toBe(false);
41 });
42​
43 it("holds at exactly the battery cutoff", () => {
44 expect(
45 computeKeepAwake({
46 ...working,
47 battery: { charging: false, level: KEEP_AWAKE_BATTERY_MIN_LEVEL },
48 }),
49 ).toBe(true);
50 });
51​
52 it("holds while charging regardless of level", () => {
53 expect(computeKeepAwake({ ...working, battery: { charging: true, level: 0.05 } })).toBe(true);
54 });
55​
56 it("treats unknown battery state as OK", () => {
57 expect(computeKeepAwake({ ...working, battery: null })).toBe(true);
58 });
59});
60​
61describe("isThreadShellWorking", () => {
62 it("is idle for settled sessions and running sessions without an active turn", () => {
63 expect(isThreadShellWorking(shellWithSession({ status: "running", activeTurnId: null }))).toBe(
64 false,
65 );
66 expect(isThreadShellWorking(shellWithSession({ status: "ready" }))).toBe(false);
67 expect(isThreadShellWorking(shellWithSession({ status: "idle" }))).toBe(false);
68 expect(isThreadShellWorking(shellWithSession(null))).toBe(false);
69 });
70​
71 it("is working for a running session with an active turn", () => {
72 expect(
73 isThreadShellWorking(
74 shellWithSession({ status: "running", activeTurnId: TurnId.make("turn-1") }),
75 ),
76 ).toBe(true);
77 });
78​
79 it("is working while a session is starting, before its first turn begins", () => {
80 expect(isThreadShellWorking(shellWithSession({ status: "starting", activeTurnId: null }))).toBe(
81 true,
82 );
83 });
84});
85​
86describe("isDesktopHostedConnectionTarget", () => {
87 it("classifies the primary local backend as desktop-hosted", () => {
88 const target = new PrimaryConnectionTarget({
89 environmentId: EnvironmentId.make("environment-primary"),
90 httpBaseUrl: "http://127.0.0.1:3773",
91 label: "This device",
92 wsBaseUrl: "ws://127.0.0.1:3773",
93 });
94 expect(isDesktopHostedConnectionTarget(target)).toBe(true);
95 });
96​
97 it("classifies a desktop-local secondary backend as desktop-hosted", () => {
98 const target = new BearerConnectionTarget({
99 connectionId: desktopLocalConnectionId("wsl:Ubuntu"),
100 environmentId: EnvironmentId.make("environment-wsl"),
101 label: "WSL (Ubuntu)",
102 });
103 expect(isDesktopHostedConnectionTarget(target)).toBe(true);
104 });
105​
106 it("excludes saved remote, SSH, and relay environments", () => {
107 const saved = new BearerConnectionTarget({
108 connectionId: "saved-remote",
109 environmentId: EnvironmentId.make("environment-saved"),
110 label: "Home server",
111 });
112 const ssh = new SshConnectionTarget({
113 connectionId: "ssh:home",
114 environmentId: EnvironmentId.make("environment-ssh"),
115 label: "SSH (home)",
116 });
117 const relay = new RelayConnectionTarget({
118 environmentId: EnvironmentId.make("environment-relay"),
119 label: "Relay",
120 });
121 expect(isDesktopHostedConnectionTarget(saved)).toBe(false);
122 expect(isDesktopHostedConnectionTarget(ssh)).toBe(false);
123 expect(isDesktopHostedConnectionTarget(relay)).toBe(false);
124 });
125});