The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { expect, it } from "@effect/vitest"; |
| 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as FileSystem from "effect/FileSystem"; |
| 5 | import * as Option from "effect/Option"; |
| 6 | import * as Path from "effect/Path"; |
| 7 | |
| 8 | import { findEasProjectId } from "./easProjectId.ts"; |
| 9 | |
| 10 | const ID_A = "d763fcb8-d37c-41ea-a773-b54a0ab4a454"; |
| 11 | const ID_B = "54d70eaf-b0a3-477a-a9cd-2cf81a5c84c9"; |
| 12 | |
| 13 | const workspace = (files: Record<string, string>) => |
| 14 | Effect.gen(function* () { |
| 15 | const fs = yield* FileSystem.FileSystem; |
| 16 | const path = yield* Path.Path; |
| 17 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-eas-project-" }); |
| 18 | for (const [file, content] of Object.entries(files)) { |
| 19 | yield* fs.makeDirectory(path.dirname(path.join(root, file)), { recursive: true }); |
| 20 | yield* fs.writeFileString(path.join(root, file), content); |
| 21 | } |
| 22 | return root; |
| 23 | }); |
| 24 | |
| 25 | const layer = NodeServices.layer; |
| 26 | |
| 27 | it.effect("reads a literal project id from an app config without running it", () => |
| 28 | Effect.gen(function* () { |
| 29 | const fs = yield* FileSystem.FileSystem; |
| 30 | const path = yield* Path.Path; |
| 31 | const root = yield* workspace({}); |
| 32 | const marker = path.join(root, "config-ran"); |
| 33 | yield* fs.makeDirectory(path.join(root, "apps/mobile"), { recursive: true }); |
| 34 | yield* fs.writeFileString( |
| 35 | path.join(root, "apps/mobile/app.config.ts"), |
| 36 | `import { writeFileSync } from "node:fs"; |
| 37 | writeFileSync(${JSON.stringify(marker)}, ""); |
| 38 | export default { extra: { eas: { projectId: "${ID_A}" } } };`, |
| 39 | ); |
| 40 | expect(yield* findEasProjectId(root)).toEqual(Option.some(ID_A)); |
| 41 | expect(yield* fs.exists(marker)).toBe(false); |
| 42 | }).pipe(Effect.scoped, Effect.provide(layer)), |
| 43 | ); |
| 44 | |
| 45 | it.effect("falls back to app.json, and does not compute a project id a config builds", () => |
| 46 | Effect.gen(function* () { |
| 47 | const staticOnly = yield* workspace({ |
| 48 | "app.json": JSON.stringify({ expo: { extra: { eas: { projectId: ID_B } } } }), |
| 49 | }); |
| 50 | expect(yield* findEasProjectId(staticOnly)).toEqual(Option.some(ID_B)); |
| 51 | const computed = yield* workspace({ |
| 52 | "app.config.ts": `const parts = ${JSON.stringify(ID_A.split("-"))}; |
| 53 | export default { extra: { eas: { projectId: parts.join("-") } } };`, |
| 54 | }); |
| 55 | expect(yield* findEasProjectId(computed)).toEqual(Option.none()); |
| 56 | }).pipe(Effect.scoped, Effect.provide(layer)), |
| 57 | ); |
| 58 | |
| 59 | it.effect.each([ |
| 60 | { |
| 61 | case: "reads the one project id a config names, whatever else it holds", |
| 62 | files: { |
| 63 | "app.config.js": `export default { |
| 64 | assetBundlePatterns: ["**/*"], |
| 65 | extra: { eas: { projectId: "${ID_A}" }, firebase: { projectId: "my-app-12345" } }, |
| 66 | }; |
| 67 | /** docs */`, |
| 68 | "app.json": JSON.stringify({ expo: { extra: { eas: { projectId: ID_A } } } }), |
| 69 | }, |
| 70 | expected: Option.some(ID_A), |
| 71 | }, |
| 72 | { |
| 73 | case: "gives no answer when a config names a project only some builds use", |
| 74 | files: { |
| 75 | "app.config.ts": `export default ({ config }) => |
| 76 | process.env.APP_VARIANT === "preview" |
| 77 | ? { ...config, extra: { ...config.extra, eas: { projectId: "${ID_B}" } } } |
| 78 | : config;`, |
| 79 | "app.json": JSON.stringify({ expo: { extra: { eas: { projectId: ID_A } } } }), |
| 80 | }, |
| 81 | expected: Option.none(), |
| 82 | }, |
| 83 | { |
| 84 | case: "gives no answer when a comment keeps an old project id", |
| 85 | files: { |
| 86 | "app.config.js": `// projectId: "${ID_B}" (the old production project) |
| 87 | export default { extra: { eas: { projectId: "${ID_A}" } } };`, |
| 88 | }, |
| 89 | expected: Option.none(), |
| 90 | }, |
| 91 | { |
| 92 | case: "gives no answer when one app sets its project from a variable", |
| 93 | files: { |
| 94 | "apps/mobile/app.config.ts": `export default { |
| 95 | extra: { eas: { projectId: process.env.EAS_PROJECT_ID } }, |
| 96 | };`, |
| 97 | "apps/kiosk/app.json": JSON.stringify({ expo: { extra: { eas: { projectId: ID_B } } } }), |
| 98 | }, |
| 99 | expected: Option.none(), |
| 100 | }, |
| 101 | { |
| 102 | case: "gives no answer when a config changes the project id after naming it", |
| 103 | files: { |
| 104 | "app.config.js": `export default ({ config }) => { |
| 105 | config.extra = { eas: { projectId: "${ID_A}" } }; |
| 106 | if (process.env.APP_ENV === "development") { |
| 107 | config.extra.eas.projectId = process.env.DEV_PROJECT_ID; |
| 108 | } |
| 109 | return config; |
| 110 | };`, |
| 111 | }, |
| 112 | expected: Option.none(), |
| 113 | }, |
| 114 | { |
| 115 | case: "gives no answer when a spread after the project id may replace it", |
| 116 | files: { |
| 117 | "apps/mobile/app.config.js": `import { eas } from "./app.shared.js"; |
| 118 | export default { extra: { eas: { projectId: "${ID_A}", ...eas } } };`, |
| 119 | }, |
| 120 | expected: Option.none(), |
| 121 | }, |
| 122 | { |
| 123 | case: "gives no answer when the config is assigned over after naming the project id", |
| 124 | files: { |
| 125 | "app.config.js": `export default ({ config }) => { |
| 126 | config.extra = { eas: { projectId: "${ID_A}" } }; |
| 127 | Object.assign(config.extra.eas, JSON.parse(process.env.EAS_OVERRIDES ?? "{}")); |
| 128 | return config; |
| 129 | };`, |
| 130 | }, |
| 131 | expected: Option.none(), |
| 132 | }, |
| 133 | { |
| 134 | case: "gives no answer when a config spreads another file over app.json's project id", |
| 135 | files: { |
| 136 | "app.config.js": `module.exports = ({ config }) => ({ ...config, ...require("./app.local.js") });`, |
| 137 | "app.json": JSON.stringify({ expo: { extra: { eas: { projectId: ID_A } } } }), |
| 138 | }, |
| 139 | expected: Option.none(), |
| 140 | }, |
| 141 | { |
| 142 | case: "gives no answer when a dynamic config spreads nothing, which may or may not use app.json", |
| 143 | files: { |
| 144 | "app.config.js": `export default { name: "Mobile", slug: "mobile" };`, |
| 145 | "app.json": JSON.stringify({ expo: { extra: { eas: { projectId: ID_A } } } }), |
| 146 | }, |
| 147 | expected: Option.none(), |
| 148 | }, |
| 149 | { |
| 150 | case: "gives no answer when a config has more extra entries than are read", |
| 151 | files: { |
| 152 | "app.config.js": `module.exports = ({ config }) => ({ ...config, extra: { ...config.extra, ${"extra: { ...config.extra, ".repeat(40)}${"}".repeat(40)} } });`, |
| 153 | "app.json": JSON.stringify({ expo: { extra: { eas: { projectId: ID_A } } } }), |
| 154 | }, |
| 155 | expected: Option.none(), |
| 156 | }, |
| 157 | { |
| 158 | case: "reads app.json's id through extra and eas entries that spread their own", |
| 159 | files: { |
| 160 | "app.config.js": `module.exports = ({ config }) => ({ |
| 161 | ...config, |
| 162 | extra: { ...config?.["extra"], eas: { ...config.extra?.eas, build: { experimental: true } }, other: 1 }, |
| 163 | });`, |
| 164 | "app.json": JSON.stringify({ expo: { extra: { eas: { projectId: ID_A } } } }), |
| 165 | }, |
| 166 | expected: Option.some(ID_A), |
| 167 | }, |
| 168 | { |
| 169 | case: "reads app.json's id through a typed variant config that spreads parts of config", |
| 170 | files: { |
| 171 | "app.config.js": `/** @param {import('expo/config').ConfigContext} context */ |
| 172 | module.exports = ({ config }) => ({ |
| 173 | ...config, |
| 174 | ios: { ...config.ios, infoPlist: { ...config?.ios.infoPlist, CFBundleDisplayName: "Dev" } }, |
| 175 | plugins: [...config.plugins, "expo-dev-client"], |
| 176 | });`, |
| 177 | "app.json": JSON.stringify({ expo: { extra: { eas: { projectId: ID_A } } } }), |
| 178 | }, |
| 179 | expected: Option.some(ID_A), |
| 180 | }, |
| 181 | { |
| 182 | case: "reads app.json's id through a config typed in a multi-line JSDoc or line comment", |
| 183 | files: { |
| 184 | "app.config.js": `// Types: import('expo/config').ExpoConfig |
| 185 | /** |
| 186 | * The dev variant of the app. |
| 187 | * @param {import('expo/config').ConfigContext} context |
| 188 | */ |
| 189 | module.exports = ({ config }) => ({ ...config, name: "Dev" });`, |
| 190 | "app.json": JSON.stringify({ expo: { extra: { eas: { projectId: ID_A } } } }), |
| 191 | }, |
| 192 | expected: Option.some(ID_A), |
| 193 | }, |
| 194 | { |
| 195 | case: "gives no answer when a config spreads a part of config over the top, which may bring its own extra", |
| 196 | files: { |
| 197 | "app.config.js": `module.exports = ({ config }) => ({ ...config, ...config.preview });`, |
| 198 | "app.json": JSON.stringify({ |
| 199 | expo: { |
| 200 | extra: { eas: { projectId: ID_A } }, |
| 201 | preview: { extra: { eas: { projectId: ID_B } } }, |
| 202 | }, |
| 203 | }), |
| 204 | }, |
| 205 | expected: Option.none(), |
| 206 | }, |
| 207 | { |
| 208 | case: "gives no answer when a config wraps a part of config under another key, as Expo reads expo", |
| 209 | files: { |
| 210 | "app.config.js": `module.exports = ({ config }) => |
| 211 | process.env.APP_VARIANT === "preview" ? { expo: { ...config.preview } } : { ...config };`, |
| 212 | "app.json": JSON.stringify({ |
| 213 | expo: { |
| 214 | extra: { eas: { projectId: ID_A } }, |
| 215 | preview: { extra: { eas: { projectId: ID_B } } }, |
| 216 | }, |
| 217 | }), |
| 218 | }, |
| 219 | expected: Option.none(), |
| 220 | }, |
| 221 | { |
| 222 | case: "gives no answer when a config spreads a part into an expo entry, which Expo reads as the config", |
| 223 | files: { |
| 224 | "app.config.js": `module.exports = ({ config }) => ({ ...config, expo: { ...config.preview.expo } });`, |
| 225 | "app.json": JSON.stringify({ |
| 226 | expo: { |
| 227 | extra: { eas: { projectId: ID_A } }, |
| 228 | preview: { expo: { extra: { eas: { projectId: ID_B } } } }, |
| 229 | }, |
| 230 | }), |
| 231 | }, |
| 232 | expected: Option.none(), |
| 233 | }, |
| 234 | { |
| 235 | case: "gives no answer when a config spreads a part into a computed key", |
| 236 | files: { |
| 237 | "app.config.js": `module.exports = ({ config }) => ({ ...config, [[...config.keys]]: config.preview });`, |
| 238 | "app.json": JSON.stringify({ |
| 239 | expo: { |
| 240 | keys: ["extra"], |
| 241 | extra: { eas: { projectId: ID_A } }, |
| 242 | preview: { eas: { projectId: ID_B } }, |
| 243 | }, |
| 244 | }), |
| 245 | }, |
| 246 | expected: Option.none(), |
| 247 | }, |
| 248 | { |
| 249 | case: "gives no answer when a template line that only looks like a comment replaces extra", |
| 250 | files: { |
| 251 | "app.config.js": `module.exports = ({ config }) => { |
| 252 | const banner = \` |
| 253 | /* \${(config.extra = config.preview.extra)} */\`; |
| 254 | return { ...config, name: banner }; |
| 255 | };`, |
| 256 | "app.json": JSON.stringify({ |
| 257 | expo: { |
| 258 | extra: { eas: { projectId: ID_A } }, |
| 259 | preview: { extra: { eas: { projectId: ID_B } } }, |
| 260 | }, |
| 261 | }), |
| 262 | }, |
| 263 | expected: Option.none(), |
| 264 | }, |
| 265 | { |
| 266 | case: "reads a project id that follows spreads", |
| 267 | files: { |
| 268 | "app.config.js": `export default ({ config }) => ({ |
| 269 | ...config, |
| 270 | extra: { ...config.extra, eas: { ...(config.extra?.eas ?? {}), projectId: "${ID_A}" } }, |
| 271 | });`, |
| 272 | }, |
| 273 | expected: Option.some(ID_A), |
| 274 | }, |
| 275 | { |
| 276 | case: "gives no answer when an app.json Expo reads as JSON5 cannot be decoded here", |
| 277 | files: { |
| 278 | "apps/mobile/app.json": `{ |
| 279 | // the mobile app |
| 280 | "expo": { "extra": { "eas": { "projectId": "${ID_B}" } }, }, |
| 281 | }`, |
| 282 | "apps/admin/app.json": JSON.stringify({ expo: { extra: { eas: { projectId: ID_A } } } }), |
| 283 | }, |
| 284 | expected: Option.none(), |
| 285 | }, |
| 286 | { |
| 287 | case: "reads only the first static config, like Expo", |
| 288 | files: { |
| 289 | "app.config.json": JSON.stringify({ expo: { name: "Mobile" } }), |
| 290 | "app.json": JSON.stringify({ expo: { extra: { eas: { projectId: ID_A } } } }), |
| 291 | }, |
| 292 | expected: Option.none(), |
| 293 | }, |
| 294 | { |
| 295 | case: "does not guess between an unwrapped static config and a wrapped one", |
| 296 | files: { |
| 297 | "apps/mobile/app.config.json": JSON.stringify({ extra: { eas: { projectId: ID_A } } }), |
| 298 | "apps/kiosk/app.json": JSON.stringify({ expo: { extra: { eas: { projectId: ID_B } } } }), |
| 299 | }, |
| 300 | expected: Option.none(), |
| 301 | }, |
| 302 | { |
| 303 | case: "uses app.json when the config never mentions a project id", |
| 304 | files: { |
| 305 | "app.config.js": `export default ({ config }) => ({ ...config, name: "Mobile" });`, |
| 306 | "app.json": JSON.stringify({ expo: { extra: { eas: { projectId: ID_B } } } }), |
| 307 | }, |
| 308 | expected: Option.some(ID_B), |
| 309 | }, |
| 310 | ])("$case", ({ files, expected }) => |
| 311 | Effect.gen(function* () { |
| 312 | expect(yield* findEasProjectId(yield* workspace(files))).toEqual(expected); |
| 313 | }).pipe(Effect.scoped, Effect.provide(layer)), |
| 314 | ); |
| 315 | |
| 316 | it.effect( |
| 317 | "gives no answer when a comment ending in a non-LF line terminator precedes the rest of a joined id", |
| 318 | () => |
| 319 | Effect.gen(function* () { |
| 320 | // A line comment ends at any ECMAScript line terminator, not only LF. |
| 321 | const root = yield* workspace({ |
| 322 | "apps/mobile/app.config.ts": |
| 323 | `export default { extra: { eas: { projectId: "${ID_A.slice(0, 9)}" // prefix\u2028` + |
| 324 | `+ "${ID_A.slice(9)}" } } };`, |
| 325 | "apps/kiosk/app.json": JSON.stringify({ expo: { extra: { eas: { projectId: ID_B } } } }), |
| 326 | }); |
| 327 | expect(yield* findEasProjectId(root)).toEqual(Option.none()); |
| 328 | }).pipe(Effect.scoped, Effect.provide(layer)), |
| 329 | ); |
| 330 | |
| 331 | it.effect("gives no answer when an app's config is there but cannot be read", () => |
| 332 | Effect.gen(function* () { |
| 333 | const fs = yield* FileSystem.FileSystem; |
| 334 | const path = yield* Path.Path; |
| 335 | for (const file of ["app.json", "app.config.json"]) { |
| 336 | const root = yield* workspace({ |
| 337 | "apps/two/app.json": JSON.stringify({ expo: { extra: { eas: { projectId: ID_B } } } }), |
| 338 | }); |
| 339 | // A directory where the file should be: it exists but cannot be read. |
| 340 | yield* fs.makeDirectory(path.join(root, "apps/one", file), { recursive: true }); |
| 341 | expect(yield* findEasProjectId(root)).toEqual(Option.none()); |
| 342 | } |
| 343 | }).pipe(Effect.scoped, Effect.provide(layer)), |
| 344 | ); |
| 345 | |
| 346 | it.effect("does not guess between apps that name different Expo projects", () => |
| 347 | Effect.gen(function* () { |
| 348 | const root = yield* workspace({ |
| 349 | "apps/one/app.json": JSON.stringify({ expo: { extra: { eas: { projectId: ID_A } } } }), |
| 350 | "apps/two/app.json": JSON.stringify({ expo: { extra: { eas: { projectId: ID_B } } } }), |
| 351 | }); |
| 352 | expect(yield* findEasProjectId(root)).toEqual(Option.none()); |
| 353 | expect(yield* findEasProjectId(yield* workspace({ "README.md": "no app" }))).toEqual( |
| 354 | Option.none(), |
| 355 | ); |
| 356 | }).pipe(Effect.scoped, Effect.provide(layer)), |
| 357 | ); |