The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as OtelEnvironment from "@t3tools/shared/otelEnvironment"; |
| 2 | import { DEFAULT_SIGNAL_EXPORT } from "@t3tools/shared/observability"; |
| 3 | // @effect-diagnostics nodeBuiltinImport:off - CLI integration uses temporary Node paths. |
| 4 | import * as NodeFS from "node:fs"; |
| 5 | import * as NodeOS from "node:os"; |
| 6 | import * as NodePath from "node:path"; |
| 7 | |
| 8 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 9 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 10 | import { assert, it } from "@effect/vitest"; |
| 11 | import { |
| 12 | CommandId, |
| 13 | EventId, |
| 14 | ProviderInstanceId, |
| 15 | ThreadId, |
| 16 | type OrchestrationV2AppThread, |
| 17 | type ProjectId, |
| 18 | } from "@t3tools/contracts"; |
| 19 | import * as NetService from "@t3tools/shared/Net"; |
| 20 | import * as DateTime from "effect/DateTime"; |
| 21 | import * as Effect from "effect/Effect"; |
| 22 | import * as Fiber from "effect/Fiber"; |
| 23 | import * as FileSystem from "effect/FileSystem"; |
| 24 | import * as Layer from "effect/Layer"; |
| 25 | import * as References from "effect/References"; |
| 26 | import * as Stream from "effect/Stream"; |
| 27 | import { Command } from "effect/cli"; |
| 28 | import { FetchHttpClient } from "effect/http"; |
| 29 | import * as TestClock from "effect/testing/TestClock"; |
| 30 | |
| 31 | import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; |
| 32 | import { cli } from "../binCli.ts"; |
| 33 | import * as ServerConfig from "../config.ts"; |
| 34 | import * as EventSink from "../orchestration-v2/EventSink.ts"; |
| 35 | import * as EventStore from "../orchestration-v2/EventStore.ts"; |
| 36 | import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts"; |
| 37 | import * as RuntimeLayer from "../orchestration-v2/runtimeLayer.ts"; |
| 38 | import * as SqlitePersistence from "../persistence/Sqlite.ts"; |
| 39 | import * as ProjectEnrichmentService from "../project/ProjectEnrichmentService.ts"; |
| 40 | import * as ProjectFaviconResolver from "../project/ProjectFaviconResolver.ts"; |
| 41 | import * as ProjectService from "../project/ProjectService.ts"; |
| 42 | import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts"; |
| 43 | import * as T3ProjectFileLoader from "../project/T3ProjectFileLoader.ts"; |
| 44 | import { |
| 45 | makePersistedServerRuntimeState, |
| 46 | persistServerRuntimeState, |
| 47 | } from "../serverRuntimeState.ts"; |
| 48 | import * as ServerSettings from "../serverSettings.ts"; |
| 49 | import * as ManagedPolicy from "../policy/ManagedPolicy.ts"; |
| 50 | import * as WorkspacePaths from "../workspace/WorkspacePaths.ts"; |
| 51 | |
| 52 | const layerCliRuntime = Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer); |
| 53 | const runCli = (args: ReadonlyArray<string>) => |
| 54 | Command.runWith(cli, { version: "0.0.0" })(args).pipe(Effect.provide(layerCliRuntime)); |
| 55 | |
| 56 | const makeConfig = (baseDir: string) => |
| 57 | Effect.gen(function* () { |
| 58 | const derivedPaths = yield* ServerConfig.deriveServerPaths(baseDir, undefined); |
| 59 | return { |
| 60 | logLevel: "Info", |
| 61 | traceMinLevel: "Info", |
| 62 | traceTimingEnabled: true, |
| 63 | traceBatchWindowMs: 200, |
| 64 | traceMaxBytes: 10 * 1024 * 1024, |
| 65 | traceMaxFiles: 10, |
| 66 | otelEnvironment: OtelEnvironment.none, |
| 67 | otlpTracesUrl: undefined, |
| 68 | otlpMetricsUrl: undefined, |
| 69 | otlpLogsUrl: undefined, |
| 70 | otlpTracesExport: DEFAULT_SIGNAL_EXPORT, |
| 71 | otlpMetricsExport: DEFAULT_SIGNAL_EXPORT, |
| 72 | otlpLogsExport: DEFAULT_SIGNAL_EXPORT, |
| 73 | mode: "web", |
| 74 | port: 0, |
| 75 | host: "127.0.0.1", |
| 76 | cwd: process.cwd(), |
| 77 | baseDir, |
| 78 | ...derivedPaths, |
| 79 | staticDir: undefined, |
| 80 | devUrl: undefined, |
| 81 | devAllowedOrigins: [], |
| 82 | noBrowser: true, |
| 83 | startupPresentation: "browser", |
| 84 | desktopBootstrapToken: undefined, |
| 85 | autoBootstrapProjectFromCwd: false, |
| 86 | logWebSocketEvents: false, |
| 87 | tailscaleServeEnabled: false, |
| 88 | tailscaleServePort: 443, |
| 89 | } satisfies ServerConfig.ServerConfig["Service"]; |
| 90 | }); |
| 91 | |
| 92 | const withProjectService = <A, E>( |
| 93 | baseDir: string, |
| 94 | use: (projects: ProjectService.ProjectService["Service"]) => Effect.Effect<A, E>, |
| 95 | ) => |
| 96 | Effect.gen(function* () { |
| 97 | const config = yield* makeConfig(baseDir); |
| 98 | const layer = RuntimeLayer.layerProjectService.pipe( |
| 99 | Layer.provideMerge(ProjectEnrichmentService.layer), |
| 100 | Layer.provideMerge(RepositoryIdentityResolver.layer), |
| 101 | Layer.provideMerge(ProjectFaviconResolver.layer), |
| 102 | Layer.provideMerge(T3ProjectFileLoader.layer), |
| 103 | Layer.provideMerge(WorkspacePaths.layer), |
| 104 | Layer.provideMerge( |
| 105 | ServerSettings.layer.pipe( |
| 106 | Layer.provide(Layer.merge(ServerSecretStore.layer, ManagedPolicy.layerTest())), |
| 107 | ), |
| 108 | ), |
| 109 | Layer.provideMerge(SqlitePersistence.layerConfig), |
| 110 | Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), |
| 111 | Layer.provide(ServerConfig.layer(config)), |
| 112 | Layer.provide(Layer.succeed(References.MinimumLogLevel, config.logLevel)), |
| 113 | ); |
| 114 | return yield* ProjectService.ProjectService.pipe(Effect.flatMap(use), Effect.provide(layer)); |
| 115 | }); |
| 116 | |
| 117 | const readProjects = (baseDir: string) => |
| 118 | withProjectService(baseDir, (projects) => projects.snapshot); |
| 119 | |
| 120 | it.effect("adds, renames, and removes projects through the V2 project CLI domain", () => |
| 121 | Effect.gen(function* () { |
| 122 | const baseDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-v2-project-cli-")); |
| 123 | const workspaceRoot = NodeFS.mkdtempSync( |
| 124 | NodePath.join(NodeOS.tmpdir(), "t3-v2-project-workspace-"), |
| 125 | ); |
| 126 | |
| 127 | yield* runCli(["project", "add", workspaceRoot, "--title", "Alpha", "--base-dir", baseDir]); |
| 128 | const added = (yield* readProjects(baseDir)).projects[0]; |
| 129 | assert.equal(added?.title, "Alpha"); |
| 130 | assert.equal(added?.workspaceRoot, workspaceRoot); |
| 131 | |
| 132 | yield* runCli(["project", "rename", workspaceRoot, "Beta", "--base-dir", baseDir]); |
| 133 | assert.equal((yield* readProjects(baseDir)).projects[0]?.title, "Beta"); |
| 134 | |
| 135 | yield* runCli(["project", "remove", added?.id ?? "", "--base-dir", baseDir]); |
| 136 | assert.deepEqual((yield* readProjects(baseDir)).projects, []); |
| 137 | }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 138 | ); |
| 139 | |
| 140 | it.effect.each(["snapshot", "mutation"] as const)( |
| 141 | "waits for a busy live server's %s response", |
| 142 | (slowRequest) => |
| 143 | Effect.gen(function* () { |
| 144 | const fs = yield* FileSystem.FileSystem; |
| 145 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-live-project-cli-" }); |
| 146 | const baseDir = NodePath.join(root, "state"); |
| 147 | const workspaceRoot = NodePath.join(root, "workspace"); |
| 148 | yield* fs.makeDirectory(workspaceRoot); |
| 149 | yield* persistServerRuntimeState({ |
| 150 | path: NodePath.join(baseDir, "userdata", "server-runtime.json"), |
| 151 | state: yield* makePersistedServerRuntimeState({ |
| 152 | config: { host: "127.0.0.1", devUrl: undefined }, |
| 153 | port: 3_773, |
| 154 | }), |
| 155 | }); |
| 156 | const now = "2026-01-01T00:00:00.000Z"; |
| 157 | const requestReceived = Promise.withResolvers<void>(); |
| 158 | const releaseResponse = Promise.withResolvers<void>(); |
| 159 | let snapshotRequests = 0; |
| 160 | let mutations = 0; |
| 161 | const fetch: typeof globalThis.fetch = async (_input, init) => { |
| 162 | const isMutation = init?.method === "POST"; |
| 163 | if (isMutation) mutations++; |
| 164 | else snapshotRequests++; |
| 165 | if (slowRequest === "mutation" ? isMutation : !isMutation) { |
| 166 | requestReceived.resolve(); |
| 167 | await releaseResponse.promise; |
| 168 | } |
| 169 | return Response.json( |
| 170 | isMutation |
| 171 | ? { |
| 172 | id: "live-project", |
| 173 | title: NodePath.basename(workspaceRoot), |
| 174 | workspaceRoot, |
| 175 | defaultModelSelection: null, |
| 176 | scripts: [], |
| 177 | createdAt: now, |
| 178 | updatedAt: now, |
| 179 | deletedAt: null, |
| 180 | } |
| 181 | : { projects: [], updatedAt: now }, |
| 182 | ); |
| 183 | }; |
| 184 | |
| 185 | const add = yield* runCli(["project", "add", workspaceRoot, "--base-dir", baseDir]).pipe( |
| 186 | Effect.provideService(FetchHttpClient.Fetch, fetch), |
| 187 | Effect.forkChild, |
| 188 | ); |
| 189 | yield* Effect.promise(() => requestReceived.promise); |
| 190 | yield* TestClock.adjust("2 seconds"); |
| 191 | releaseResponse.resolve(); |
| 192 | yield* Fiber.join(add); |
| 193 | |
| 194 | assert.equal(snapshotRequests, 1); |
| 195 | assert.equal(mutations, 1); |
| 196 | assert.deepEqual((yield* readProjects(baseDir)).projects, []); |
| 197 | }).pipe(Effect.provide(NodeServices.layer)), |
| 198 | ); |
| 199 | |
| 200 | it.effect("does not send a token to whatever took over a dead server's port", () => |
| 201 | Effect.gen(function* () { |
| 202 | const fs = yield* FileSystem.FileSystem; |
| 203 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-dead-project-cli-" }); |
| 204 | const baseDir = NodePath.join(root, "state"); |
| 205 | const workspaceRoot = NodePath.join(root, "workspace"); |
| 206 | yield* fs.makeDirectory(workspaceRoot); |
| 207 | const state = yield* makePersistedServerRuntimeState({ |
| 208 | config: { host: "127.0.0.1", devUrl: undefined }, |
| 209 | port: 3_773, |
| 210 | }); |
| 211 | yield* persistServerRuntimeState({ |
| 212 | path: NodePath.join(baseDir, "userdata", "server-runtime.json"), |
| 213 | // Above Linux's PID ceiling, as in pair.test.ts: no process can hold it. |
| 214 | state: { ...state, pid: 4_194_305 }, |
| 215 | }); |
| 216 | let requests = 0; |
| 217 | const fetch: typeof globalThis.fetch = async () => { |
| 218 | requests++; |
| 219 | return Response.json({ projects: [], updatedAt: "2026-01-01T00:00:00.000Z" }); |
| 220 | }; |
| 221 | |
| 222 | yield* runCli(["project", "add", workspaceRoot, "--base-dir", baseDir]).pipe( |
| 223 | Effect.provideService(FetchHttpClient.Fetch, fetch), |
| 224 | ); |
| 225 | |
| 226 | assert.equal(requests, 0); |
| 227 | assert.equal((yield* readProjects(baseDir)).projects[0]?.workspaceRoot, workspaceRoot); |
| 228 | }).pipe(Effect.provide(NodeServices.layer)), |
| 229 | ); |
| 230 | |
| 231 | const makeProjectLookupFixture = Effect.fn("ProjectCliTest.makeProjectLookupFixture")(function* () { |
| 232 | const fs = yield* FileSystem.FileSystem; |
| 233 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-v2-project-lookup-" }); |
| 234 | const baseDir = NodePath.join(root, "state"); |
| 235 | const workspaceRoot = NodePath.join(root, "workspace"); |
| 236 | yield* fs.makeDirectory(workspaceRoot); |
| 237 | yield* runCli(["project", "add", workspaceRoot, "--base-dir", baseDir]); |
| 238 | const project = (yield* readProjects(baseDir)).projects[0]; |
| 239 | assert.isDefined(project); |
| 240 | return { baseDir, workspaceRoot, project: project! }; |
| 241 | }); |
| 242 | |
| 243 | it.effect.each([ |
| 244 | { label: "keeps its title", titleArgs: [], expectedTitle: undefined }, |
| 245 | { label: "applies --title", titleArgs: ["--title", " Back again "], expectedTitle: "Back again" }, |
| 246 | ])( |
| 247 | "adding an archived project's folder again unarchives it and $label", |
| 248 | ({ titleArgs, expectedTitle }) => |
| 249 | Effect.gen(function* () { |
| 250 | const { baseDir, workspaceRoot, project } = yield* makeProjectLookupFixture(); |
| 251 | yield* withProjectService(baseDir, (projects) => |
| 252 | projects.update({ |
| 253 | commandId: CommandId.make("project-cli-archive"), |
| 254 | projectId: project.id, |
| 255 | archived: true, |
| 256 | }), |
| 257 | ); |
| 258 | |
| 259 | yield* runCli(["project", "add", workspaceRoot, ...titleArgs, "--base-dir", baseDir]); |
| 260 | |
| 261 | const projects = (yield* readProjects(baseDir)).projects; |
| 262 | assert.deepEqual( |
| 263 | projects.map((entry) => ({ |
| 264 | id: entry.id, |
| 265 | title: entry.title, |
| 266 | archivedAt: entry.archivedAt, |
| 267 | })), |
| 268 | [{ id: project.id, title: expectedTitle ?? project.title, archivedAt: null }], |
| 269 | ); |
| 270 | }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), Effect.scoped), |
| 271 | ); |
| 272 | |
| 273 | const makeThreadPersistenceLayer = Effect.fn("ProjectCliTest.makeThreadPersistenceLayer")( |
| 274 | function* (baseDir: string) { |
| 275 | const config = yield* makeConfig(baseDir); |
| 276 | return Layer.mergeAll( |
| 277 | RuntimeLayer.layerEventSink, |
| 278 | ProjectionStore.layer, |
| 279 | EventStore.layer, |
| 280 | ).pipe( |
| 281 | Layer.provideMerge(SqlitePersistence.layerConfig), |
| 282 | Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), |
| 283 | Layer.provide(ServerConfig.layer(config)), |
| 284 | Layer.provide(Layer.succeed(References.MinimumLogLevel, config.logLevel)), |
| 285 | ); |
| 286 | }, |
| 287 | ); |
| 288 | |
| 289 | const seedNativeThreads = Effect.fn("ProjectCliTest.seedNativeThreads")(function* ( |
| 290 | baseDir: string, |
| 291 | threads: ReadonlyArray<{ |
| 292 | readonly id: ThreadId; |
| 293 | readonly projectId: ProjectId; |
| 294 | readonly archived: boolean; |
| 295 | }>, |
| 296 | ) { |
| 297 | const layer = yield* makeThreadPersistenceLayer(baseDir); |
| 298 | const createdAt = DateTime.makeUnsafe("2026-09-04T12:00:00.000Z"); |
| 299 | const providerInstanceId = ProviderInstanceId.make("codex"); |
| 300 | yield* Effect.gen(function* () { |
| 301 | const eventSink = yield* EventSink.EventSinkV2; |
| 302 | yield* eventSink.write({ |
| 303 | commandId: CommandId.make("project-cli-seed-threads"), |
| 304 | events: threads.map(({ id, projectId, archived }) => { |
| 305 | const payload: OrchestrationV2AppThread = { |
| 306 | createdBy: "user", |
| 307 | creationSource: "web", |
| 308 | id, |
| 309 | projectId, |
| 310 | title: id, |
| 311 | providerInstanceId, |
| 312 | modelSelection: { instanceId: providerInstanceId, model: "gpt-5" }, |
| 313 | runtimeMode: "full-access", |
| 314 | interactionMode: "default", |
| 315 | branch: null, |
| 316 | worktreePath: null, |
| 317 | activeProviderThreadId: null, |
| 318 | lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: id }, |
| 319 | forkedFrom: null, |
| 320 | createdAt, |
| 321 | updatedAt: createdAt, |
| 322 | archivedAt: archived ? createdAt : null, |
| 323 | settledOverride: null, |
| 324 | settledAt: null, |
| 325 | lastVisitedAt: null, |
| 326 | deletedAt: null, |
| 327 | }; |
| 328 | return { |
| 329 | id: EventId.make(`project-cli-create-${id}`), |
| 330 | type: "thread.created" as const, |
| 331 | threadId: id, |
| 332 | providerInstanceId, |
| 333 | occurredAt: createdAt, |
| 334 | payload, |
| 335 | }; |
| 336 | }), |
| 337 | }); |
| 338 | }).pipe(Effect.provide(layer)); |
| 339 | }); |
| 340 | |
| 341 | const readNativeThreadState = Effect.fn("ProjectCliTest.readNativeThreadState")(function* ( |
| 342 | baseDir: string, |
| 343 | threadId: ThreadId, |
| 344 | ) { |
| 345 | const layer = yield* makeThreadPersistenceLayer(baseDir); |
| 346 | return yield* Effect.gen(function* () { |
| 347 | const projections = yield* ProjectionStore.ProjectionStoreV2; |
| 348 | const events = yield* EventStore.EventStoreV2; |
| 349 | return { |
| 350 | thread: yield* projections.getThread(threadId), |
| 351 | events: yield* events.read({ threadId }).pipe(Stream.runCollect), |
| 352 | }; |
| 353 | }).pipe(Effect.provide(layer)); |
| 354 | }); |
| 355 | |
| 356 | it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))( |
| 357 | "project deletion with native V2 threads", |
| 358 | (it) => { |
| 359 | it.effect.each([ |
| 360 | { label: "an active thread", archived: false, missing: false }, |
| 361 | { |
| 362 | label: "an archived thread after its workspace disappears", |
| 363 | archived: true, |
| 364 | missing: true, |
| 365 | }, |
| 366 | ])("rejects unforced removal of a project with $label", ({ archived, missing }) => |
| 367 | Effect.gen(function* () { |
| 368 | const fs = yield* FileSystem.FileSystem; |
| 369 | const { baseDir, workspaceRoot, project } = yield* makeProjectLookupFixture(); |
| 370 | const threadId = ThreadId.make("project-cli-preserved-thread"); |
| 371 | yield* seedNativeThreads(baseDir, [{ id: threadId, projectId: project.id, archived }]); |
| 372 | const before = yield* readNativeThreadState(baseDir, threadId); |
| 373 | if (missing) yield* fs.rename(workspaceRoot, `${workspaceRoot}-removed`); |
| 374 | |
| 375 | const error = yield* runCli([ |
| 376 | "project", |
| 377 | "remove", |
| 378 | missing ? workspaceRoot : project.id, |
| 379 | "--base-dir", |
| 380 | baseDir, |
| 381 | ]).pipe( |
| 382 | Effect.match({ |
| 383 | onFailure: (error) => error, |
| 384 | onSuccess: () => assert.fail("Removing a nonempty project must require --force."), |
| 385 | }), |
| 386 | ); |
| 387 | |
| 388 | assert.include(error.message, "not empty"); |
| 389 | assert.deepEqual( |
| 390 | (yield* readProjects(baseDir)).projects.map((entry) => entry.id), |
| 391 | [project.id], |
| 392 | ); |
| 393 | assert.deepEqual(yield* readNativeThreadState(baseDir, threadId), before); |
| 394 | assert.equal(yield* fs.exists(workspaceRoot), !missing); |
| 395 | }), |
| 396 | ); |
| 397 | |
| 398 | it.effect.each(["present", "missing"] as const)( |
| 399 | "force-removes active and archived V2 threads with the workspace %s, preserving unrelated projects", |
| 400 | (workspace) => |
| 401 | Effect.gen(function* () { |
| 402 | const fs = yield* FileSystem.FileSystem; |
| 403 | const { baseDir, workspaceRoot, project } = yield* makeProjectLookupFixture(); |
| 404 | const otherWorkspace = `${workspaceRoot}-other`; |
| 405 | yield* fs.makeDirectory(otherWorkspace); |
| 406 | yield* runCli(["project", "add", otherWorkspace, "--base-dir", baseDir]); |
| 407 | const otherProject = (yield* readProjects(baseDir)).projects.find( |
| 408 | (entry) => entry.workspaceRoot === otherWorkspace, |
| 409 | ); |
| 410 | assert.isDefined(otherProject); |
| 411 | const activeId = ThreadId.make("project-cli-deleted-active"); |
| 412 | const archivedId = ThreadId.make("project-cli-deleted-archived"); |
| 413 | const unrelatedId = ThreadId.make("project-cli-unrelated-thread"); |
| 414 | yield* seedNativeThreads(baseDir, [ |
| 415 | { id: activeId, projectId: project.id, archived: false }, |
| 416 | { id: archivedId, projectId: project.id, archived: true }, |
| 417 | { id: unrelatedId, projectId: otherProject!.id, archived: false }, |
| 418 | ]); |
| 419 | const unrelatedBefore = yield* readNativeThreadState(baseDir, unrelatedId); |
| 420 | if (workspace === "missing") { |
| 421 | yield* fs.rename(workspaceRoot, `${workspaceRoot}-removed`); |
| 422 | } |
| 423 | |
| 424 | yield* runCli([ |
| 425 | "project", |
| 426 | "remove", |
| 427 | workspace === "missing" ? workspaceRoot : project.id, |
| 428 | "--force", |
| 429 | "--base-dir", |
| 430 | baseDir, |
| 431 | ]); |
| 432 | |
| 433 | assert.deepEqual( |
| 434 | (yield* readProjects(baseDir)).projects.map((entry) => entry.id), |
| 435 | [otherProject!.id], |
| 436 | ); |
| 437 | for (const threadId of [activeId, archivedId]) { |
| 438 | const state = yield* readNativeThreadState(baseDir, threadId); |
| 439 | assert.isNotNull(state.thread.deletedAt); |
| 440 | assert.lengthOf( |
| 441 | state.events.filter((record) => record.event.type === "thread.deleted"), |
| 442 | 1, |
| 443 | ); |
| 444 | } |
| 445 | assert.deepEqual(yield* readNativeThreadState(baseDir, unrelatedId), unrelatedBefore); |
| 446 | assert.equal(yield* fs.exists(workspaceRoot), workspace === "present"); |
| 447 | assert.isTrue(yield* fs.exists(otherWorkspace)); |
| 448 | }), |
| 449 | ); |
| 450 | }, |
| 451 | ); |
| 452 | |
| 453 | it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))( |
| 454 | "project lookup with unavailable workspaces", |
| 455 | (it) => { |
| 456 | it.effect.each([ |
| 457 | "id", |
| 458 | "stored path", |
| 459 | "relative path", |
| 460 | "path with a trailing separator", |
| 461 | ] as const)("removes an empty project by %s after its directory is gone", (identifier) => |
| 462 | Effect.gen(function* () { |
| 463 | const fs = yield* FileSystem.FileSystem; |
| 464 | const { baseDir, workspaceRoot, project } = yield* makeProjectLookupFixture(); |
| 465 | yield* fs.rename(workspaceRoot, `${workspaceRoot}-removed`); |
| 466 | const target = { |
| 467 | id: project.id, |
| 468 | "stored path": workspaceRoot, |
| 469 | "relative path": NodePath.relative(process.cwd(), workspaceRoot), |
| 470 | "path with a trailing separator": `${workspaceRoot}${NodePath.sep}`, |
| 471 | }[identifier]; |
| 472 | yield* runCli(["project", "remove", target, "--base-dir", baseDir]); |
| 473 | assert.deepEqual((yield* readProjects(baseDir)).projects, []); |
| 474 | assert.isFalse(yield* fs.exists(workspaceRoot)); |
| 475 | }), |
| 476 | ); |
| 477 | |
| 478 | it.effect("does not resolve another environment's project ID in an empty database", () => |
| 479 | Effect.gen(function* () { |
| 480 | const fs = yield* FileSystem.FileSystem; |
| 481 | const { baseDir, workspaceRoot, project } = yield* makeProjectLookupFixture(); |
| 482 | yield* fs.rename(workspaceRoot, `${workspaceRoot}-removed`); |
| 483 | const replacementDir = yield* fs.makeTempDirectoryScoped({ |
| 484 | prefix: "t3-v2-project-empty-", |
| 485 | }); |
| 486 | const error = yield* runCli([ |
| 487 | "project", |
| 488 | "remove", |
| 489 | project.id, |
| 490 | "--force", |
| 491 | "--base-dir", |
| 492 | replacementDir, |
| 493 | ]).pipe(Effect.flip); |
| 494 | assert.include(error.message, "No active project found"); |
| 495 | assert.deepEqual( |
| 496 | (yield* readProjects(baseDir)).projects.map((entry) => entry.id), |
| 497 | [project.id], |
| 498 | ); |
| 499 | assert.deepEqual((yield* readProjects(replacementDir)).projects, []); |
| 500 | }), |
| 501 | ); |
| 502 | |
| 503 | it.effect("normalizes existing paths without conflating separately registered symlinks", () => |
| 504 | Effect.gen(function* () { |
| 505 | const { baseDir, workspaceRoot, project } = yield* makeProjectLookupFixture(); |
| 506 | yield* runCli([ |
| 507 | "project", |
| 508 | "rename", |
| 509 | `${workspaceRoot}${NodePath.sep}.`, |
| 510 | "Normalized", |
| 511 | "--base-dir", |
| 512 | baseDir, |
| 513 | ]); |
| 514 | assert.equal((yield* readProjects(baseDir)).projects[0]?.title, "Normalized"); |
| 515 | const aliasPath = `${workspaceRoot}-alias`; |
| 516 | NodeFS.symlinkSync(workspaceRoot, aliasPath, "junction"); |
| 517 | const unknownAlias = yield* runCli([ |
| 518 | "project", |
| 519 | "remove", |
| 520 | aliasPath, |
| 521 | "--base-dir", |
| 522 | baseDir, |
| 523 | ]).pipe(Effect.flip); |
| 524 | assert.include(unknownAlias.message, "No active project found"); |
| 525 | yield* runCli(["project", "add", aliasPath, "--base-dir", baseDir]); |
| 526 | const added = (yield* readProjects(baseDir)).projects; |
| 527 | assert.equal(added.length, 2); |
| 528 | const aliasProject = added.find((entry) => entry.workspaceRoot === aliasPath); |
| 529 | assert.isDefined(aliasProject); |
| 530 | assert.notEqual(aliasProject?.id, project.id); |
| 531 | yield* runCli(["project", "remove", `${aliasPath}${NodePath.sep}.`, "--base-dir", baseDir]); |
| 532 | assert.deepEqual( |
| 533 | (yield* readProjects(baseDir)).projects.map((entry) => entry.id), |
| 534 | [project.id], |
| 535 | ); |
| 536 | assert.isTrue(NodeFS.existsSync(workspaceRoot)); |
| 537 | }), |
| 538 | ); |
| 539 | }, |
| 540 | ); |