The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { assert, it } from "@effect/vitest"; |
| 2 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | import { |
| 5 | CommandId, |
| 6 | ProjectId, |
| 7 | ThreadId, |
| 8 | VcsProcessSpawnError, |
| 9 | VcsProcessTimeoutError, |
| 10 | } from "@t3tools/contracts"; |
| 11 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 12 | import * as Deferred from "effect/Deferred"; |
| 13 | import * as Effect from "effect/Effect"; |
| 14 | import * as Fiber from "effect/Fiber"; |
| 15 | import * as FileSystem from "effect/FileSystem"; |
| 16 | import * as Layer from "effect/Layer"; |
| 17 | import * as Logger from "effect/Logger"; |
| 18 | import * as Option from "effect/Option"; |
| 19 | import * as Path from "effect/Path"; |
| 20 | import * as PlatformError from "effect/PlatformError"; |
| 21 | import { ChildProcessSpawner } from "effect/process"; |
| 22 | import * as Scope from "effect/Scope"; |
| 23 | |
| 24 | import * as ServerConfig from "../config.ts"; |
| 25 | import * as GitManager from "../git/GitManager.ts"; |
| 26 | import * as GitWorkflow from "../git/GitWorkflowService.ts"; |
| 27 | import * as RuntimeLayer from "../orchestration-v2/runtimeLayer.ts"; |
| 28 | import * as SqlitePersistence from "../persistence/Sqlite.ts"; |
| 29 | import * as ProcessRunner from "../processRunner.ts"; |
| 30 | import * as ServerSettings from "../serverSettings.ts"; |
| 31 | import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; |
| 32 | import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts"; |
| 33 | import * as VcsProcess from "../vcs/VcsProcess.ts"; |
| 34 | import * as WorkspacePaths from "../workspace/WorkspacePaths.ts"; |
| 35 | import * as ProjectEnrichmentService from "./ProjectEnrichmentService.ts"; |
| 36 | import * as ProjectFaviconResolver from "./ProjectFaviconResolver.ts"; |
| 37 | import * as ProjectService from "./ProjectService.ts"; |
| 38 | import * as RepositoryIdentityResolver from "./RepositoryIdentityResolver.ts"; |
| 39 | import * as ManagedProjectFolders from "./ManagedProjectFolders.ts"; |
| 40 | |
| 41 | const layerGitWorkflow = (vcsProcess?: Layer.Layer<VcsProcess.VcsProcess>) => |
| 42 | GitWorkflow.layer.pipe( |
| 43 | Layer.provide(VcsDriverRegistry.layer.pipe(Layer.provide(vcsProcess ?? VcsProcess.layer))), |
| 44 | Layer.provide(Layer.mock(GitManager.GitManager)({})), |
| 45 | ); |
| 46 | |
| 47 | const layerEnrichment = ProjectEnrichmentService.layer.pipe( |
| 48 | Layer.provide( |
| 49 | Layer.mergeAll( |
| 50 | Layer.succeed(RepositoryIdentityResolver.RepositoryIdentityResolver, { |
| 51 | resolve: () => Effect.succeed(null), |
| 52 | }), |
| 53 | Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, { |
| 54 | invalidate: () => Effect.void, |
| 55 | resolvePath: () => Effect.succeed(null), |
| 56 | }), |
| 57 | ), |
| 58 | ), |
| 59 | ); |
| 60 | |
| 61 | const layerRealGit = GitVcsDriver.layer.pipe(Layer.provide(VcsProcess.layer)); |
| 62 | |
| 63 | interface HarnessOptions { |
| 64 | readonly vcsProcess?: Layer.Layer<VcsProcess.VcsProcess>; |
| 65 | readonly git?: typeof layerRealGit; |
| 66 | readonly projects?: ( |
| 67 | real: ProjectService.ProjectService["Service"], |
| 68 | ) => ProjectService.ProjectService["Service"]; |
| 69 | } |
| 70 | |
| 71 | const layer = (baseDir: string, options?: HarnessOptions) => |
| 72 | ManagedProjectFolders.layer.pipe( |
| 73 | Layer.provide( |
| 74 | options?.projects === undefined |
| 75 | ? Layer.empty |
| 76 | : Layer.effect( |
| 77 | ProjectService.ProjectService, |
| 78 | ProjectService.ProjectService.pipe(Effect.map(options.projects)), |
| 79 | ), |
| 80 | ), |
| 81 | Layer.provideMerge(RuntimeLayer.layerProjectService), |
| 82 | Layer.provideMerge(layerEnrichment), |
| 83 | Layer.provideMerge(WorkspacePaths.layer), |
| 84 | Layer.provideMerge(layerGitWorkflow(options?.vcsProcess)), |
| 85 | Layer.provideMerge(options?.git ?? layerRealGit), |
| 86 | Layer.provideMerge(SqlitePersistence.layerMemory), |
| 87 | Layer.provideMerge(ServerSettings.layerTest()), |
| 88 | Layer.provideMerge(ServerConfig.layerTest(baseDir, baseDir)), |
| 89 | Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), |
| 90 | ); |
| 91 | |
| 92 | const withScratch = <A, E>( |
| 93 | body: (input: { |
| 94 | readonly baseDir: string; |
| 95 | }) => Effect.Effect< |
| 96 | A, |
| 97 | E, |
| 98 | | ManagedProjectFolders.ManagedProjectFolders |
| 99 | | ProjectService.ProjectService |
| 100 | | NodeServices.NodeServices |
| 101 | | Scope.Scope |
| 102 | >, |
| 103 | options?: HarnessOptions, |
| 104 | ) => |
| 105 | Effect.gen(function* () { |
| 106 | const fileSystem = yield* FileSystem.FileSystem; |
| 107 | const baseDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-managed-folders-" }); |
| 108 | return yield* body({ baseDir }).pipe(Effect.provide(layer(baseDir, options))); |
| 109 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))); |
| 110 | |
| 111 | const git = (cwd: string, args: ReadonlyArray<string>) => |
| 112 | ProcessRunner.ProcessRunner.pipe( |
| 113 | Effect.flatMap((runner) => runner.run({ command: "git", args: ["-C", cwd, ...args] })), |
| 114 | Effect.provide(ProcessRunner.layer), |
| 115 | ); |
| 116 | |
| 117 | const requireRoot = Effect.gen(function* () { |
| 118 | const scratch = yield* ManagedProjectFolders.ManagedProjectFolders; |
| 119 | return Option.getOrThrow(yield* scratch.scratchRoot); |
| 120 | }); |
| 121 | |
| 122 | it.effect("offers nothing, and says why once, when the data dir sits inside a Git checkout", () => { |
| 123 | const warnings: string[] = []; |
| 124 | const logger = Logger.make(({ logLevel, message }) => { |
| 125 | if (logLevel === "Warn") warnings.push(String(message)); |
| 126 | }); |
| 127 | return Effect.gen(function* () { |
| 128 | const fileSystem = yield* FileSystem.FileSystem; |
| 129 | const path = yield* Path.Path; |
| 130 | const checkout = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-scratch-repo-" }); |
| 131 | yield* git(checkout, ["init", "--quiet"]); |
| 132 | const baseDir = path.join(checkout, ".t3"); |
| 133 | yield* fileSystem.makeDirectory(baseDir); |
| 134 | const repositoryRoot = (yield* git(baseDir, ["rev-parse", "--show-toplevel"])).stdout.trim(); |
| 135 | yield* Effect.gen(function* () { |
| 136 | const scratch = yield* ManagedProjectFolders.ManagedProjectFolders; |
| 137 | assert.isTrue(Option.isNone(yield* scratch.scratchRoot)); |
| 138 | assert.isFalse(yield* scratch.isScratchProject(ProjectId.make("project:any"))); |
| 139 | const failure = yield* Effect.flip(scratch.ensureScratchProject); |
| 140 | assert.equal(failure._tag, "ScratchUnavailableError"); |
| 141 | assert.include(failure.message, baseDir); |
| 142 | // The root prefixes the data dir, so it must appear on its own. |
| 143 | assert.include(failure.message.replaceAll(baseDir, ""), repositoryRoot); |
| 144 | yield* Effect.flip(scratch.ensureScratchProject); |
| 145 | }).pipe( |
| 146 | Effect.provide( |
| 147 | Layer.mergeAll(layer(baseDir), Logger.layer([logger], { mergeWithExisting: false })), |
| 148 | ), |
| 149 | ); |
| 150 | assert.equal(warnings.length, 1); |
| 151 | assert.include(warnings[0], baseDir); |
| 152 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))); |
| 153 | }); |
| 154 | |
| 155 | it.effect("offers a Scratch folder when Git is not installed", () => |
| 156 | withScratch( |
| 157 | ({ baseDir }) => |
| 158 | Effect.gen(function* () { |
| 159 | const path = yield* Path.Path; |
| 160 | assert.equal(yield* requireRoot, path.resolve(baseDir, "scratch")); |
| 161 | }), |
| 162 | { |
| 163 | vcsProcess: Layer.succeed(VcsProcess.VcsProcess, { |
| 164 | run: (input) => |
| 165 | Effect.fail( |
| 166 | new VcsProcessSpawnError({ |
| 167 | operation: input.operation, |
| 168 | command: input.command, |
| 169 | cwd: input.cwd, |
| 170 | cause: PlatformError.systemError({ |
| 171 | _tag: "NotFound", |
| 172 | module: "ChildProcess", |
| 173 | method: "spawn", |
| 174 | syscall: `spawn ${input.command}`, |
| 175 | }), |
| 176 | }), |
| 177 | ), |
| 178 | }), |
| 179 | }, |
| 180 | ), |
| 181 | ); |
| 182 | |
| 183 | it.effect("offers the Scratch folder once a Git check that timed out is asked again", () => |
| 184 | withScratch( |
| 185 | ({ baseDir }) => |
| 186 | Effect.gen(function* () { |
| 187 | const path = yield* Path.Path; |
| 188 | const scratch = yield* ManagedProjectFolders.ManagedProjectFolders; |
| 189 | assert.isTrue(Option.isNone(yield* scratch.scratchRoot)); |
| 190 | assert.equal(yield* requireRoot, path.resolve(baseDir, "scratch")); |
| 191 | }), |
| 192 | { |
| 193 | vcsProcess: Layer.effect( |
| 194 | VcsProcess.VcsProcess, |
| 195 | Effect.map(VcsProcess.make, (real) => { |
| 196 | let timedOut = false; |
| 197 | return VcsProcess.VcsProcess.of({ |
| 198 | run: (input) => |
| 199 | Effect.suspend(() => { |
| 200 | if (timedOut) return real.run(input); |
| 201 | timedOut = true; |
| 202 | return Effect.fail( |
| 203 | new VcsProcessTimeoutError({ |
| 204 | operation: input.operation, |
| 205 | command: input.command, |
| 206 | cwd: input.cwd, |
| 207 | timeoutMs: 5_000, |
| 208 | }), |
| 209 | ); |
| 210 | }), |
| 211 | }); |
| 212 | }), |
| 213 | ).pipe( |
| 214 | Layer.provide(ProcessRunner.layer), |
| 215 | Layer.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), |
| 216 | ), |
| 217 | }, |
| 218 | ), |
| 219 | ); |
| 220 | |
| 221 | it.effect("creates one Scratch project, even for concurrent first requests", () => |
| 222 | withScratch(() => |
| 223 | Effect.gen(function* () { |
| 224 | const scratch = yield* ManagedProjectFolders.ManagedProjectFolders; |
| 225 | const projects = yield* ProjectService.ProjectService; |
| 226 | const racers = yield* Effect.all( |
| 227 | Array.from({ length: 8 }, () => scratch.ensureScratchProject), |
| 228 | { concurrency: "unbounded" }, |
| 229 | ); |
| 230 | const again = yield* scratch.ensureScratchProject; |
| 231 | assert.deepEqual( |
| 232 | new Set(racers.map((result) => result.projectId)), |
| 233 | new Set([again.projectId]), |
| 234 | ); |
| 235 | |
| 236 | const snapshot = yield* projects.snapshot; |
| 237 | const root = yield* requireRoot; |
| 238 | const scratchProjects = snapshot.projects.filter((project) => project.workspaceRoot === root); |
| 239 | assert.lengthOf(scratchProjects, 1); |
| 240 | assert.equal(scratchProjects[0]?.title, "No project"); |
| 241 | assert.deepEqual(scratchProjects[0]?.projectIcon, { |
| 242 | kind: "lucide", |
| 243 | name: "message-square-dashed", |
| 244 | color: "gray", |
| 245 | }); |
| 246 | }), |
| 247 | ), |
| 248 | ); |
| 249 | |
| 250 | it.effect("recreates the Scratch folder after it is deleted", () => |
| 251 | withScratch(() => |
| 252 | Effect.gen(function* () { |
| 253 | const scratch = yield* ManagedProjectFolders.ManagedProjectFolders; |
| 254 | const fileSystem = yield* FileSystem.FileSystem; |
| 255 | const { projectId } = yield* scratch.ensureScratchProject; |
| 256 | const root = yield* requireRoot; |
| 257 | yield* fileSystem.remove(root, { recursive: true }); |
| 258 | |
| 259 | assert.equal((yield* scratch.ensureScratchProject).projectId, projectId); |
| 260 | assert.isTrue(yield* fileSystem.exists(root)); |
| 261 | |
| 262 | yield* fileSystem.remove(root, { recursive: true }); |
| 263 | const folder = yield* scratch.folderForThread({ |
| 264 | projectId, |
| 265 | threadId: ThreadId.make("thread-after-delete"), |
| 266 | text: "Still works", |
| 267 | }); |
| 268 | assert.isTrue(yield* fileSystem.exists(Option.getOrThrow(folder))); |
| 269 | }), |
| 270 | ), |
| 271 | ); |
| 272 | |
| 273 | it.effect("gives each Scratch thread its own folder, named from its message", () => |
| 274 | withScratch(() => |
| 275 | Effect.gen(function* () { |
| 276 | const scratch = yield* ManagedProjectFolders.ManagedProjectFolders; |
| 277 | const fileSystem = yield* FileSystem.FileSystem; |
| 278 | const path = yield* Path.Path; |
| 279 | const { projectId } = yield* scratch.ensureScratchProject; |
| 280 | const root = yield* requireRoot; |
| 281 | const text = "Convert these PNGs to WebP, please!"; |
| 282 | const first = Option.getOrThrow( |
| 283 | yield* scratch.folderForThread({ |
| 284 | projectId, |
| 285 | threadId: ThreadId.make("thread:a:0123456789abcdef"), |
| 286 | text, |
| 287 | }), |
| 288 | ); |
| 289 | const second = Option.getOrThrow( |
| 290 | yield* scratch.folderForThread({ |
| 291 | projectId, |
| 292 | threadId: ThreadId.make("thread:b:0123456789abcdef"), |
| 293 | text, |
| 294 | }), |
| 295 | ); |
| 296 | |
| 297 | const third = Option.getOrThrow( |
| 298 | yield* scratch.folderForThread({ |
| 299 | projectId, |
| 300 | threadId: ThreadId.make("thread:b0123456789abcdef"), |
| 301 | text, |
| 302 | }), |
| 303 | ); |
| 304 | |
| 305 | assert.equal(new Set([first, second, third]).size, 3); |
| 306 | for (const folder of [first, second, third]) { |
| 307 | assert.equal(path.dirname(folder), root); |
| 308 | assert.match(path.basename(folder), /^\d{4}-\d{2}-\d{2}-convert-these-pngs-to-webp-/); |
| 309 | assert.isTrue(yield* fileSystem.exists(folder)); |
| 310 | } |
| 311 | |
| 312 | const pasted = Option.getOrThrow( |
| 313 | yield* scratch.folderForThread({ |
| 314 | projectId, |
| 315 | threadId: ThreadId.make("thread-pasted"), |
| 316 | text: `${"word ".repeat(10_000)}../../etc`, |
| 317 | }), |
| 318 | ); |
| 319 | assert.equal(path.dirname(pasted), root); |
| 320 | assert.isAtMost(path.basename(pasted).length, 80); |
| 321 | |
| 322 | const accented = Option.getOrThrow( |
| 323 | yield* scratch.folderForThread({ |
| 324 | projectId, |
| 325 | threadId: ThreadId.make("thread-diacritics"), |
| 326 | text: "tìm kiếm skill", |
| 327 | }), |
| 328 | ); |
| 329 | assert.match(path.basename(accented), /^\d{4}-\d{2}-\d{2}-tim-kiem-skill-/); |
| 330 | }), |
| 331 | ), |
| 332 | ); |
| 333 | |
| 334 | it.effect("leaves threads in other projects alone", () => |
| 335 | withScratch(({ baseDir }) => |
| 336 | Effect.gen(function* () { |
| 337 | const scratch = yield* ManagedProjectFolders.ManagedProjectFolders; |
| 338 | const projects = yield* ProjectService.ProjectService; |
| 339 | const fileSystem = yield* FileSystem.FileSystem; |
| 340 | const path = yield* Path.Path; |
| 341 | const { projectId: scratchId } = yield* scratch.ensureScratchProject; |
| 342 | const other = yield* projects.create({ |
| 343 | commandId: CommandId.make("command:other"), |
| 344 | projectId: ProjectId.make("project:other"), |
| 345 | title: "Other", |
| 346 | workspaceRoot: path.join(baseDir, "other"), |
| 347 | createWorkspaceRootIfMissing: true, |
| 348 | }); |
| 349 | |
| 350 | const folder = yield* scratch.folderForThread({ |
| 351 | projectId: other.id, |
| 352 | threadId: ThreadId.make("thread-other"), |
| 353 | text: "Hello", |
| 354 | }); |
| 355 | assert.isTrue(Option.isNone(folder)); |
| 356 | assert.deepEqual(yield* fileSystem.readDirectory(yield* requireRoot), []); |
| 357 | assert.isTrue(yield* scratch.isScratchProject(scratchId)); |
| 358 | assert.isFalse(yield* scratch.isScratchProject(other.id)); |
| 359 | }), |
| 360 | ), |
| 361 | ); |
| 362 | |
| 363 | it.effect("still knows No project when its folder is the target of a symlinked Scratch", () => |
| 364 | withScratch(({ baseDir }) => |
| 365 | Effect.gen(function* () { |
| 366 | const scratch = yield* ManagedProjectFolders.ManagedProjectFolders; |
| 367 | const projects = yield* ProjectService.ProjectService; |
| 368 | const fileSystem = yield* FileSystem.FileSystem; |
| 369 | const path = yield* Path.Path; |
| 370 | const root = yield* requireRoot; |
| 371 | const target = path.join(baseDir, "visible-chats"); |
| 372 | yield* fileSystem.makeDirectory(target); |
| 373 | yield* fileSystem.symlink(target, root); |
| 374 | const { projectId } = yield* scratch.ensureScratchProject; |
| 375 | yield* projects.update({ |
| 376 | commandId: CommandId.make("command:point-at-target"), |
| 377 | projectId, |
| 378 | workspaceRoot: target, |
| 379 | }); |
| 380 | |
| 381 | assert.isTrue(yield* scratch.isScratchProject(projectId)); |
| 382 | assert.equal((yield* scratch.ensureScratchProject).projectId, projectId); |
| 383 | assert.deepStrictEqual(yield* scratch.scratchRoot, Option.some(target)); |
| 384 | }), |
| 385 | ), |
| 386 | ); |
| 387 | |
| 388 | const GIT_IDENTITY_KEYS = [ |
| 389 | "GIT_AUTHOR_NAME", |
| 390 | "GIT_AUTHOR_EMAIL", |
| 391 | "GIT_COMMITTER_NAME", |
| 392 | "GIT_COMMITTER_EMAIL", |
| 393 | "EMAIL", |
| 394 | "GIT_CONFIG_GLOBAL", |
| 395 | "GIT_CONFIG_NOSYSTEM", |
| 396 | ] as const; |
| 397 | |
| 398 | const withGitEnv = <A, E, R>( |
| 399 | env: Partial<Record<(typeof GIT_IDENTITY_KEYS)[number], string>>, |
| 400 | effect: Effect.Effect<A, E, R>, |
| 401 | ) => |
| 402 | Effect.gen(function* () { |
| 403 | const fileSystem = yield* FileSystem.FileSystem; |
| 404 | const emptyConfig = yield* fileSystem.makeTempFileScoped({ prefix: "t3-gitconfig-" }); |
| 405 | const hostEnv: NodeJS.ProcessEnv = { ...(yield* HostProcess.Environment) }; |
| 406 | for (const key of GIT_IDENTITY_KEYS) delete hostEnv[key]; |
| 407 | return yield* effect.pipe( |
| 408 | Effect.provideService(HostProcess.Environment, { |
| 409 | ...hostEnv, |
| 410 | GIT_CONFIG_GLOBAL: emptyConfig, |
| 411 | GIT_CONFIG_NOSYSTEM: "1", |
| 412 | ...env, |
| 413 | }), |
| 414 | ); |
| 415 | }); |
| 416 | |
| 417 | const TEST_IDENTITY = { |
| 418 | GIT_AUTHOR_NAME: "Test", |
| 419 | GIT_AUTHOR_EMAIL: "test@test.com", |
| 420 | GIT_COMMITTER_NAME: "Test", |
| 421 | GIT_COMMITTER_EMAIL: "test@test.com", |
| 422 | }; |
| 423 | |
| 424 | const gitOutput = (cwd: string, args: ReadonlyArray<string>) => |
| 425 | git(cwd, args).pipe(Effect.map((result) => result.stdout.trim())); |
| 426 | |
| 427 | it.effect("starts a named project as a committed repository, and suffixes a taken name", () => |
| 428 | withScratch(({ baseDir }) => |
| 429 | withGitEnv( |
| 430 | TEST_IDENTITY, |
| 431 | Effect.gen(function* () { |
| 432 | const folders = yield* ManagedProjectFolders.ManagedProjectFolders; |
| 433 | const projects = yield* ProjectService.ProjectService; |
| 434 | const fileSystem = yield* FileSystem.FileSystem; |
| 435 | const path = yield* Path.Path; |
| 436 | const root = path.resolve(baseDir, "projects"); |
| 437 | assert.equal(folders.namedProjectsRoot, root); |
| 438 | const attributes = path.join(baseDir, "crlf.gitattributes"); |
| 439 | const crlfConfig = path.join(baseDir, "crlf.gitconfig"); |
| 440 | yield* fileSystem.writeFileString(attributes, "* text=auto eol=crlf\n"); |
| 441 | yield* git(baseDir, ["config", "--file", crlfConfig, "core.safecrlf", "true"]); |
| 442 | yield* git(baseDir, ["config", "--file", crlfConfig, "core.attributesFile", attributes]); |
| 443 | (yield* HostProcess.Environment).GIT_CONFIG_GLOBAL = crlfConfig; |
| 444 | |
| 445 | const first = yield* folders.createNamedProject({ name: "Pinball Stats" }); |
| 446 | const second = yield* folders.createNamedProject({ name: "pinball stats" }); |
| 447 | |
| 448 | assert.equal(first.workspaceRoot, path.join(root, "pinball-stats")); |
| 449 | assert.equal(second.workspaceRoot, path.join(root, "pinball-stats-2")); |
| 450 | assert.isUndefined(first.commitError); |
| 451 | const project = Option.getOrThrow(yield* projects.getById(first.projectId)); |
| 452 | assert.equal(project.title, "Pinball Stats"); |
| 453 | assert.equal(project.workspaceRoot, first.workspaceRoot); |
| 454 | |
| 455 | const readme = yield* fileSystem.readFileString( |
| 456 | path.join(first.workspaceRoot, "README.md"), |
| 457 | ); |
| 458 | assert.include(readme, "# Pinball Stats"); |
| 459 | assert.include(readme, `src="assets/icon.svg"`); |
| 460 | const icon = yield* fileSystem.readFileString( |
| 461 | path.join(first.workspaceRoot, "assets", "icon.svg"), |
| 462 | ); |
| 463 | assert.include(icon, ">PS</text>"); |
| 464 | assert.equal( |
| 465 | yield* gitOutput(first.workspaceRoot, ["log", "--format=%s"]), |
| 466 | "Initial commit", |
| 467 | ); |
| 468 | assert.equal( |
| 469 | yield* gitOutput(first.workspaceRoot, ["rev-parse", "--abbrev-ref", "HEAD"]), |
| 470 | "main", |
| 471 | ); |
| 472 | assert.equal(yield* gitOutput(first.workspaceRoot, ["status", "--porcelain"]), ""); |
| 473 | }), |
| 474 | ), |
| 475 | ), |
| 476 | ); |
| 477 | |
| 478 | it.effect("gives concurrent named projects with the same name distinct folders", () => |
| 479 | withScratch(() => |
| 480 | withGitEnv( |
| 481 | TEST_IDENTITY, |
| 482 | Effect.gen(function* () { |
| 483 | const folders = yield* ManagedProjectFolders.ManagedProjectFolders; |
| 484 | const projects = yield* ProjectService.ProjectService; |
| 485 | const created = yield* Effect.all( |
| 486 | Array.from({ length: 4 }, () => folders.createNamedProject({ name: "Race" })), |
| 487 | { concurrency: "unbounded" }, |
| 488 | ); |
| 489 | |
| 490 | assert.equal(new Set(created.map((result) => result.workspaceRoot)).size, 4); |
| 491 | assert.equal(new Set(created.map((result) => result.projectId)).size, 4); |
| 492 | const snapshot = yield* projects.snapshot; |
| 493 | for (const result of created) { |
| 494 | assert.isTrue( |
| 495 | snapshot.projects.some((project) => project.workspaceRoot === result.workspaceRoot), |
| 496 | ); |
| 497 | } |
| 498 | }), |
| 499 | ), |
| 500 | ), |
| 501 | ); |
| 502 | |
| 503 | it.effect("keeps a named project and reports why when Git cannot commit", () => |
| 504 | withScratch(({ baseDir }) => |
| 505 | withGitEnv( |
| 506 | {}, |
| 507 | Effect.gen(function* () { |
| 508 | const folders = yield* ManagedProjectFolders.ManagedProjectFolders; |
| 509 | const projects = yield* ProjectService.ProjectService; |
| 510 | const fileSystem = yield* FileSystem.FileSystem; |
| 511 | const path = yield* Path.Path; |
| 512 | yield* fileSystem.writeFileString( |
| 513 | path.join(baseDir, "no-identity.gitconfig"), |
| 514 | "[user]\n\tuseConfigOnly = true\n", |
| 515 | ); |
| 516 | (yield* HostProcess.Environment).GIT_CONFIG_GLOBAL = path.join( |
| 517 | baseDir, |
| 518 | "no-identity.gitconfig", |
| 519 | ); |
| 520 | |
| 521 | const result = yield* folders.createNamedProject({ name: "No Identity" }); |
| 522 | |
| 523 | assert.include(result.commitError ?? "", "no name or email"); |
| 524 | assert.isTrue(Option.isSome(yield* projects.getById(result.projectId))); |
| 525 | assert.isTrue(yield* fileSystem.exists(path.join(result.workspaceRoot, "README.md"))); |
| 526 | assert.isTrue(yield* fileSystem.exists(path.join(result.workspaceRoot, ".git"))); |
| 527 | }), |
| 528 | ), |
| 529 | ), |
| 530 | ); |
| 531 | |
| 532 | it.effect("says Git is not installed when a named project cannot start Git", () => |
| 533 | withScratch( |
| 534 | () => |
| 535 | Effect.gen(function* () { |
| 536 | const folders = yield* ManagedProjectFolders.ManagedProjectFolders; |
| 537 | |
| 538 | const failure = yield* Effect.flip(folders.createNamedProject({ name: "T3Code Test" })); |
| 539 | |
| 540 | assert.strictEqual( |
| 541 | failure.message, |
| 542 | "Git isn't installed on this machine. Install Git to create a project.", |
| 543 | ); |
| 544 | }), |
| 545 | { |
| 546 | git: GitVcsDriver.layer.pipe( |
| 547 | Layer.provide( |
| 548 | Layer.succeed( |
| 549 | ChildProcessSpawner.ChildProcessSpawner, |
| 550 | ChildProcessSpawner.make(() => |
| 551 | Effect.fail( |
| 552 | PlatformError.systemError({ |
| 553 | _tag: "NotFound", |
| 554 | module: "ChildProcess", |
| 555 | method: "spawn", |
| 556 | syscall: "spawn git", |
| 557 | }), |
| 558 | ), |
| 559 | ), |
| 560 | ), |
| 561 | ), |
| 562 | ), |
| 563 | }, |
| 564 | ), |
| 565 | ); |
| 566 | |
| 567 | it.effect("keeps a folder that another project owns when the create conflicts", () => |
| 568 | withScratch(({ baseDir }) => |
| 569 | withGitEnv( |
| 570 | TEST_IDENTITY, |
| 571 | Effect.gen(function* () { |
| 572 | const folders = yield* ManagedProjectFolders.ManagedProjectFolders; |
| 573 | const projects = yield* ProjectService.ProjectService; |
| 574 | const fileSystem = yield* FileSystem.FileSystem; |
| 575 | const path = yield* Path.Path; |
| 576 | const taken = path.join(folders.namedProjectsRoot, "taken"); |
| 577 | yield* projects.create({ |
| 578 | commandId: CommandId.make("command:owner"), |
| 579 | projectId: ProjectId.make("project:owner"), |
| 580 | title: "Owner", |
| 581 | workspaceRoot: taken, |
| 582 | createWorkspaceRootIfMissing: true, |
| 583 | }); |
| 584 | yield* fileSystem.remove(taken, { recursive: true }); |
| 585 | |
| 586 | const failure = yield* Effect.flip(folders.createNamedProject({ name: "Taken" })); |
| 587 | |
| 588 | assert.equal(failure._tag, "NamedProjectCreateError"); |
| 589 | assert.equal(failure.message, "Failed to create the project."); |
| 590 | assert.isTrue(yield* fileSystem.exists(path.join(taken, "README.md"))); |
| 591 | const owner = yield* projects.getByWorkspaceRoot(taken); |
| 592 | assert.equal(Option.getOrThrow(owner).id, ProjectId.make("project:owner")); |
| 593 | assert.deepEqual(yield* fileSystem.readDirectory(path.resolve(baseDir, "projects")), [ |
| 594 | "taken", |
| 595 | ]); |
| 596 | }), |
| 597 | ), |
| 598 | ), |
| 599 | ); |
| 600 | |
| 601 | it.effect("removes the folder when the project create is rejected for another reason", () => |
| 602 | withScratch( |
| 603 | ({ baseDir }) => |
| 604 | withGitEnv( |
| 605 | TEST_IDENTITY, |
| 606 | Effect.gen(function* () { |
| 607 | const folders = yield* ManagedProjectFolders.ManagedProjectFolders; |
| 608 | const fileSystem = yield* FileSystem.FileSystem; |
| 609 | const path = yield* Path.Path; |
| 610 | |
| 611 | const failure = yield* Effect.flip(folders.createNamedProject({ name: "Rejected" })); |
| 612 | |
| 613 | assert.equal(failure._tag, "NamedProjectCreateError"); |
| 614 | assert.deepEqual(yield* fileSystem.readDirectory(path.resolve(baseDir, "projects")), []); |
| 615 | }), |
| 616 | ), |
| 617 | { |
| 618 | projects: (real) => |
| 619 | ProjectService.ProjectService.of({ |
| 620 | ...real, |
| 621 | create: (input) => |
| 622 | Effect.fail( |
| 623 | new ProjectService.ProjectOperationError({ |
| 624 | operation: "dispatch-project-command", |
| 625 | projectId: input.projectId, |
| 626 | cause: "store unavailable", |
| 627 | }), |
| 628 | ), |
| 629 | }), |
| 630 | }, |
| 631 | ), |
| 632 | ); |
| 633 | |
| 634 | it.effect("removes the folder when the create is cancelled before the project exists", () => |
| 635 | Effect.gen(function* () { |
| 636 | const scaffolding = yield* Deferred.make<void>(); |
| 637 | yield* withScratch( |
| 638 | ({ baseDir }) => |
| 639 | Effect.gen(function* () { |
| 640 | const folders = yield* ManagedProjectFolders.ManagedProjectFolders; |
| 641 | const projects = yield* ProjectService.ProjectService; |
| 642 | const fileSystem = yield* FileSystem.FileSystem; |
| 643 | const path = yield* Path.Path; |
| 644 | |
| 645 | const fiber = yield* folders |
| 646 | .createNamedProject({ name: "Cancelled" }) |
| 647 | .pipe(Effect.forkChild); |
| 648 | yield* Deferred.await(scaffolding); |
| 649 | assert.deepEqual(yield* fileSystem.readDirectory(path.resolve(baseDir, "projects")), [ |
| 650 | "cancelled", |
| 651 | ]); |
| 652 | yield* Fiber.interrupt(fiber); |
| 653 | |
| 654 | assert.deepEqual(yield* fileSystem.readDirectory(path.resolve(baseDir, "projects")), []); |
| 655 | assert.deepEqual((yield* projects.snapshot).projects, []); |
| 656 | }), |
| 657 | { |
| 658 | git: Layer.mock(GitVcsDriver.GitVcsDriver)({ |
| 659 | readConfigValue: () => Effect.succeed(null), |
| 660 | execute: () => |
| 661 | Deferred.succeed(scaffolding, undefined).pipe(Effect.andThen(Effect.never)), |
| 662 | }), |
| 663 | }, |
| 664 | ); |
| 665 | }), |
| 666 | ); |