The best way to control Claude Code, Codex and more from any computer or phone

main
ts 153 lines 4.99 KB
Raw
1 import * as NodeServices from "@effect/platform-node/NodeServices";
2 import { assert, it } from "@effect/vitest";
3 import * as HostProcess from "@t3tools/shared/HostProcess";
4 import * as Effect from "effect/Effect";
5 import * as FileSystem from "effect/FileSystem";
6 import * as Path from "effect/Path";
7
8 import {
9 androidSdk,
10 buildRunTitle,
11 NativeClientError,
12 prebuildAndroid,
13 remoteBuildState,
14 } from "./mobile-native-client.ts";
15
16 const caches = [".gradle/history", "app/.cxx/object"];
17
18 const fixture = Effect.gen(function* () {
19 const fs = yield* FileSystem.FileSystem;
20 const path = yield* Path.Path;
21 const mobile = yield* fs.makeTempDirectoryScoped({ prefix: "mobile-native-client-" });
22 const android = path.join(mobile, "android");
23 for (const cache of caches) {
24 yield* fs.makeDirectory(path.dirname(path.join(android, cache)), { recursive: true });
25 yield* fs.writeFileString(path.join(android, cache), cache);
26 }
27 const cleanPrebuild = Effect.gen(function* () {
28 yield* fs.remove(android, { recursive: true });
29 yield* fs.makeDirectory(path.join(android, "app"), { recursive: true });
30 yield* fs.writeFileString(path.join(android, "app/build.gradle"), "regenerated");
31 });
32 const assertCachesRestored = Effect.gen(function* () {
33 for (const cache of caches) {
34 assert.strictEqual(yield* fs.readFileString(path.join(android, cache)), cache);
35 }
36 assert.deepStrictEqual(yield* fs.readDirectory(mobile), ["android"]);
37 });
38 return { fs, path, mobile, android, cleanPrebuild, assertCachesRestored };
39 });
40
41 it.layer(NodeServices.layer)("prebuildAndroid", (it) => {
42 it.effect("keeps Gradle and native caches through a clean prebuild", () =>
43 Effect.gen(function* () {
44 const { fs, path, mobile, android, cleanPrebuild, assertCachesRestored } = yield* fixture;
45
46 yield* prebuildAndroid(mobile, cleanPrebuild);
47
48 assert.strictEqual(
49 yield* fs.readFileString(path.join(android, "app/build.gradle")),
50 "regenerated",
51 );
52 yield* assertCachesRestored;
53 }),
54 );
55
56 it.effect("restores the caches and returns the error when prebuild fails", () =>
57 Effect.gen(function* () {
58 const { mobile, cleanPrebuild, assertCachesRestored } = yield* fixture;
59 const failure = new NativeClientError({ message: "prebuild failed" });
60
61 const error = yield* prebuildAndroid(
62 mobile,
63 cleanPrebuild.pipe(Effect.andThen(Effect.fail(failure))),
64 ).pipe(Effect.flip);
65
66 assert.strictEqual(error, failure);
67 yield* assertCachesRestored;
68 }),
69 );
70 });
71
72 it.layer(NodeServices.layer)("androidSdk", (it) => {
73 it.effect("skips a configured SDK path that holds no SDK", () =>
74 Effect.gen(function* () {
75 const fs = yield* FileSystem.FileSystem;
76 const path = yield* Path.Path;
77 const home = yield* fs.makeTempDirectoryScoped({ prefix: "android-sdk-home-" });
78 const sdk = path.join(home, "sdk");
79 yield* fs.makeDirectory(path.join(sdk, "platform-tools"), { recursive: true });
80
81 const resolved = yield* androidSdk.pipe(
82 Effect.provideService(HostProcess.Environment, {
83 HOME: home,
84 ANDROID_HOME: path.join(home, "stale"),
85 ANDROID_SDK_ROOT: sdk,
86 }),
87 );
88
89 assert.strictEqual(resolved, sdk);
90 }),
91 );
92 });
93
94 it("finds a Blacksmith build by its run title and reads its state", () => {
95 const fingerprint = "a".repeat(40);
96 const title = buildRunTitle("android", "x86_64", fingerprint);
97 assert.strictEqual(title, `Dev client android x86_64 ${fingerprint}`);
98 assert.strictEqual(buildRunTitle("ios", null, fingerprint), `Dev client ios ${fingerprint}`);
99 const run = (
100 createdAt: string,
101 status: string,
102 conclusion: string | null,
103 displayTitle = title,
104 ) => ({
105 displayTitle,
106 status,
107 conclusion,
108 createdAt,
109 });
110 const since = Date.parse("2026-11-09T12:00:00Z");
111 assert.strictEqual(remoteBuildState([], title, since), "none");
112 // Another client's run, and a run from before this wait, say nothing about this build.
113 assert.strictEqual(
114 remoteBuildState(
115 [
116 run(
117 "2026-11-09T12:05:00Z",
118 "in_progress",
119 null,
120 buildRunTitle("android", "arm64-v8a", fingerprint),
121 ),
122 run("2026-11-09T11:00:00Z", "completed", "failure"),
123 ],
124 title,
125 since,
126 ),
127 "none",
128 );
129 assert.strictEqual(
130 remoteBuildState([run("2026-11-09T12:01:00Z", "queued", null)], title, since),
131 "running",
132 );
133 assert.strictEqual(
134 remoteBuildState([run("2026-11-09T12:01:00Z", "completed", "success")], title, since),
135 "succeeded",
136 );
137 // The newest run decides: a retry that is running outranks the failure before it.
138 assert.strictEqual(
139 remoteBuildState(
140 [
141 run("2026-11-09T12:01:00Z", "completed", "failure"),
142 run("2026-11-09T12:09:00Z", "in_progress", null),
143 ],
144 title,
145 since,
146 ),
147 "running",
148 );
149 assert.strictEqual(
150 remoteBuildState([run("2026-11-09T12:01:00Z", "completed", "cancelled")], title, since),
151 "failed",
152 );
153 });