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 * as Arr from "effect/Array"; |
| 2 | import * as Context from "effect/Context"; |
| 3 | import * as Queue from "effect/Queue"; |
| 4 | import * as Tracer from "effect/Tracer"; |
| 5 | import { assert, it } from "@effect/vitest"; |
| 6 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 7 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 8 | import { CommandId, type Project, ProjectId, ProviderInstanceId } from "@t3tools/contracts"; |
| 9 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 10 | import * as Deferred from "effect/Deferred"; |
| 11 | import * as Effect from "effect/Effect"; |
| 12 | import * as Fiber from "effect/Fiber"; |
| 13 | import * as FileSystem from "effect/FileSystem"; |
| 14 | import * as Layer from "effect/Layer"; |
| 15 | import * as Metric from "effect/Metric"; |
| 16 | import * as Option from "effect/Option"; |
| 17 | import * as PubSub from "effect/PubSub"; |
| 18 | import * as Ref from "effect/Ref"; |
| 19 | import { TestClock } from "effect/testing"; |
| 20 | import * as SqlClient from "effect/sql/SqlClient"; |
| 21 | |
| 22 | import * as ServerConfig from "../config.ts"; |
| 23 | import { |
| 24 | orchestrationCommandDuration, |
| 25 | orchestrationCommandsTotal, |
| 26 | } from "../observability/Metrics.ts"; |
| 27 | import * as EventSink from "../orchestration-v2/EventSink.ts"; |
| 28 | import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; |
| 29 | import * as LegacyV1ThreadImporter from "../orchestration-v2/legacy/LegacyV1ThreadImporter.ts"; |
| 30 | import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts"; |
| 31 | import * as ProjectStore from "../orchestration-v2/ProjectStore.ts"; |
| 32 | import * as RuntimeLayer from "../orchestration-v2/runtimeLayer.ts"; |
| 33 | import * as ThreadCommandExecutor from "../orchestration-v2/ThreadCommandExecutor.ts"; |
| 34 | import * as SqlitePersistence from "../persistence/Sqlite.ts"; |
| 35 | import * as ProcessRunner from "../processRunner.ts"; |
| 36 | import * as ServerSettings from "../serverSettings.ts"; |
| 37 | import * as WorkspacePaths from "../workspace/WorkspacePaths.ts"; |
| 38 | import * as ProjectEnrichmentService from "./ProjectEnrichmentService.ts"; |
| 39 | import * as ProjectFaviconResolver from "./ProjectFaviconResolver.ts"; |
| 40 | import * as ProjectService from "./ProjectService.ts"; |
| 41 | import * as RepositoryIdentityResolver from "./RepositoryIdentityResolver.ts"; |
| 42 | import * as T3ProjectFileLoader from "./T3ProjectFileLoader.ts"; |
| 43 | |
| 44 | const layerWorkspacePaths = Layer.succeed(WorkspacePaths.WorkspacePaths, { |
| 45 | normalizeWorkspaceRoot: (workspaceRoot) => Effect.succeed(workspaceRoot.replace(/\/$/, "")), |
| 46 | resolveRelativePathWithinRoot: ({ workspaceRoot, relativePath }) => |
| 47 | Effect.succeed({ absolutePath: `${workspaceRoot}/${relativePath}`, relativePath }), |
| 48 | }); |
| 49 | |
| 50 | const layerMetadata = Layer.merge( |
| 51 | Layer.succeed(RepositoryIdentityResolver.RepositoryIdentityResolver, { |
| 52 | resolve: (workspaceRoot) => |
| 53 | Effect.succeed({ |
| 54 | canonicalKey: `github.com/t3tools/${workspaceRoot.split("/").at(-1)}`, |
| 55 | locator: { |
| 56 | source: "git-remote" as const, |
| 57 | remoteName: "origin", |
| 58 | remoteUrl: `git@github.com:t3tools/${workspaceRoot.split("/").at(-1)}.git`, |
| 59 | }, |
| 60 | rootPath: workspaceRoot, |
| 61 | }), |
| 62 | }), |
| 63 | Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, { |
| 64 | invalidate: () => Effect.void, |
| 65 | resolvePath: (workspaceRoot) => Effect.succeed(`${workspaceRoot}/favicon.svg`), |
| 66 | }), |
| 67 | ); |
| 68 | |
| 69 | class EnrichmentCompletions extends Context.Service<EnrichmentCompletions, Queue.Queue<void>>()( |
| 70 | "t3/project/ProjectService.test/EnrichmentCompletions", |
| 71 | ) {} |
| 72 | |
| 73 | const layerEnrichmentCompletions = Layer.effectContext( |
| 74 | Effect.gen(function* () { |
| 75 | const completions = yield* Queue.unbounded<void>(); |
| 76 | const tracer = Tracer.make({ |
| 77 | span: (options) => { |
| 78 | const span = new Tracer.NativeSpan(options); |
| 79 | const end = span.end.bind(span); |
| 80 | span.end = (...args) => { |
| 81 | end(...args); |
| 82 | if ( |
| 83 | span.name === "ProjectEnrichmentService.resolveRepositoryIdentity" || |
| 84 | span.name === "ProjectEnrichmentService.resolveFavicon" |
| 85 | ) { |
| 86 | Queue.offerUnsafe(completions, undefined); |
| 87 | } |
| 88 | }; |
| 89 | return span; |
| 90 | }, |
| 91 | }); |
| 92 | return Context.make(EnrichmentCompletions, completions).pipe( |
| 93 | Context.add(Tracer.Tracer, tracer), |
| 94 | ); |
| 95 | }), |
| 96 | ); |
| 97 | |
| 98 | const layerTestFor = ( |
| 99 | projectMetadataLayer: Layer.Layer< |
| 100 | | ProjectFaviconResolver.ProjectFaviconResolver |
| 101 | | RepositoryIdentityResolver.RepositoryIdentityResolver |
| 102 | >, |
| 103 | ) => |
| 104 | RuntimeLayer.layerProjectService.pipe( |
| 105 | Layer.provideMerge(ProjectEnrichmentService.layer), |
| 106 | Layer.provideMerge(layerWorkspacePaths), |
| 107 | Layer.provideMerge(projectMetadataLayer), |
| 108 | Layer.provideMerge(SqlitePersistence.layerMemory), |
| 109 | Layer.provideMerge(ServerSettings.layerTest()), |
| 110 | Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "project-service-test-" })), |
| 111 | Layer.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), |
| 112 | Layer.provideMerge(layerEnrichmentCompletions), |
| 113 | ); |
| 114 | |
| 115 | const layerTest = layerTestFor(layerMetadata); |
| 116 | |
| 117 | it.effect("resolves only the requested active project page in creation order", () => |
| 118 | Effect.gen(function* () { |
| 119 | const inspected = yield* Ref.make<ReadonlySet<string>>(new Set()); |
| 120 | const note = (workspaceRoot: string) => |
| 121 | Ref.update(inspected, (roots) => new Set([...roots, workspaceRoot])); |
| 122 | const metadata = Layer.merge( |
| 123 | Layer.succeed(RepositoryIdentityResolver.RepositoryIdentityResolver, { |
| 124 | resolve: (workspaceRoot) => |
| 125 | note(workspaceRoot).pipe( |
| 126 | Effect.as({ |
| 127 | canonicalKey: `example.test${workspaceRoot}`, |
| 128 | locator: { |
| 129 | source: "git-remote" as const, |
| 130 | remoteName: "origin", |
| 131 | remoteUrl: `https://example.test${workspaceRoot}.git`, |
| 132 | }, |
| 133 | rootPath: workspaceRoot, |
| 134 | }), |
| 135 | ), |
| 136 | }), |
| 137 | Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, { |
| 138 | invalidate: () => Effect.void, |
| 139 | resolvePath: (workspaceRoot) => note(workspaceRoot).pipe(Effect.as(null)), |
| 140 | }), |
| 141 | ); |
| 142 | yield* Effect.gen(function* () { |
| 143 | const sql = yield* SqlClient.SqlClient; |
| 144 | const service = yield* ProjectService.ProjectService; |
| 145 | const enrichment = yield* ProjectEnrichmentService.ProjectEnrichmentService; |
| 146 | const changes = yield* enrichment.subscribeChanges; |
| 147 | const rows = Arr.makeBy(6, (index) => ({ |
| 148 | project_id: `page:${index}`, |
| 149 | title: `Project ${index}`, |
| 150 | workspace_root: `/work/page-${index}`, |
| 151 | scripts_json: "[]", |
| 152 | created_at: "2026-10-01T00:00:00.000Z", |
| 153 | updated_at: "2026-10-01T00:00:00.000Z", |
| 154 | deleted_at: index === 1 ? "2026-10-02T00:00:00.000Z" : null, |
| 155 | auto_pull: 0, |
| 156 | })); |
| 157 | yield* sql`INSERT INTO projection_projects ${sql.insert(rows)}`; |
| 158 | const first = yield* service.listPage({ cursor: 0, limit: 2 }); |
| 159 | assert.deepEqual( |
| 160 | first.projects.map((project) => project.id), |
| 161 | ["page:0", "page:2"], |
| 162 | ); |
| 163 | assert.equal(first.nextCursor, 2); |
| 164 | assert.deepEqual( |
| 165 | first.projects.map((project) => project.repositoryIdentity?.canonicalKey), |
| 166 | ["example.test/work/page-0", "example.test/work/page-2"], |
| 167 | ); |
| 168 | // A sentinel queued after the page lets the enrichment workers finish |
| 169 | // earlier requests before checking which workspaces were inspected. |
| 170 | yield* enrichment.request("/work/page-sentinel"); |
| 171 | for (;;) { |
| 172 | if ((yield* PubSub.take(changes)).workspaceRoot === "/work/page-sentinel") break; |
| 173 | } |
| 174 | assert.deepEqual( |
| 175 | new Set(yield* Ref.get(inspected)), |
| 176 | new Set(["/work/page-0", "/work/page-2", "/work/page-sentinel"]), |
| 177 | ); |
| 178 | const second = yield* service.listPage({ cursor: first.nextCursor!, limit: 2 }); |
| 179 | assert.deepEqual( |
| 180 | second.projects.map((project) => project.id), |
| 181 | ["page:3", "page:4"], |
| 182 | ); |
| 183 | assert.equal(second.nextCursor, 4); |
| 184 | const last = yield* service.listPage({ cursor: second.nextCursor!, limit: 2 }); |
| 185 | assert.deepEqual( |
| 186 | last.projects.map((project) => project.id), |
| 187 | ["page:5"], |
| 188 | ); |
| 189 | assert.isNull(last.nextCursor); |
| 190 | assert.isFalse((yield* Ref.get(inspected)).has("/work/page-1")); |
| 191 | assert.deepEqual(yield* service.listPage({ cursor: 99, limit: 2 }), { |
| 192 | projects: [], |
| 193 | nextCursor: null, |
| 194 | }); |
| 195 | }).pipe(Effect.scoped, Effect.provide(layerTestFor(metadata))); |
| 196 | }), |
| 197 | ); |
| 198 | |
| 199 | const layerProjectServiceDependencies = Layer.mergeAll( |
| 200 | RuntimeLayer.layerEventSink, |
| 201 | ProjectStore.layer, |
| 202 | ProjectionStore.layer, |
| 203 | IdAllocator.layer, |
| 204 | ThreadCommandExecutor.layer, |
| 205 | ).pipe( |
| 206 | Layer.provideMerge(LegacyV1ThreadImporter.layer.pipe(Layer.provide(RuntimeLayer.layerEventSink))), |
| 207 | Layer.provideMerge(ProjectEnrichmentService.layer), |
| 208 | Layer.provideMerge(layerWorkspacePaths), |
| 209 | Layer.provideMerge(layerMetadata), |
| 210 | Layer.provideMerge(SqlitePersistence.layerMemory), |
| 211 | Layer.provideMerge(ServerSettings.layerTest()), |
| 212 | Layer.provideMerge(ServerConfig.layerTest(process.cwd(), { prefix: "project-service-race-" })), |
| 213 | Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), |
| 214 | ); |
| 215 | |
| 216 | const waitForProject = Effect.fn("ProjectServiceTest.waitForProject")(function* ( |
| 217 | service: ProjectService.ProjectService["Service"], |
| 218 | projectId: ProjectId, |
| 219 | predicate: (project: Project) => boolean, |
| 220 | ) { |
| 221 | return yield* Effect.scoped( |
| 222 | Effect.gen(function* () { |
| 223 | const completions = yield* EnrichmentCompletions; |
| 224 | for (;;) { |
| 225 | const project = Option.getOrThrow(yield* service.getById(projectId)); |
| 226 | if (predicate(project)) return project; |
| 227 | yield* Queue.take(completions); |
| 228 | } |
| 229 | }), |
| 230 | ); |
| 231 | }); |
| 232 | |
| 233 | it.layer(layerTest)("ProjectService", (it) => { |
| 234 | it.effect("creates, updates, resolves, snapshots, and soft-deletes projects", () => |
| 235 | Effect.gen(function* () { |
| 236 | const service = yield* ProjectService.ProjectService; |
| 237 | const projectId = ProjectId.make("project:service-test"); |
| 238 | const modelSelection = { |
| 239 | instanceId: ProviderInstanceId.make("codex_custom"), |
| 240 | model: "gpt-5.1-codex", |
| 241 | } as const; |
| 242 | yield* TestClock.setTime(Date.parse("2026-06-20T10:00:00.000Z")); |
| 243 | |
| 244 | const created = yield* service.create({ |
| 245 | commandId: CommandId.make("command:project:create"), |
| 246 | projectId, |
| 247 | title: "Project", |
| 248 | workspaceRoot: "/work/project/", |
| 249 | defaultModelSelection: modelSelection, |
| 250 | scripts: [ |
| 251 | { |
| 252 | id: "setup", |
| 253 | name: "Setup", |
| 254 | command: "vp install", |
| 255 | icon: "configure", |
| 256 | runOnWorktreeCreate: true, |
| 257 | }, |
| 258 | ], |
| 259 | }); |
| 260 | assert.equal(created.workspaceRoot, "/work/project"); |
| 261 | assert.isNull(created.repositoryIdentity); |
| 262 | assert.isNull(created.faviconPath); |
| 263 | |
| 264 | const hydratedCreated = yield* waitForProject( |
| 265 | service, |
| 266 | projectId, |
| 267 | (project) => project.repositoryIdentity !== null && project.faviconPath !== null, |
| 268 | ); |
| 269 | assert.equal(hydratedCreated?.repositoryIdentity?.canonicalKey, "github.com/t3tools/project"); |
| 270 | assert.equal(hydratedCreated?.faviconPath, "/work/project/favicon.svg"); |
| 271 | |
| 272 | const updated = yield* service.update({ |
| 273 | commandId: CommandId.make("command:project:update"), |
| 274 | projectId, |
| 275 | title: "Renamed", |
| 276 | autoPull: true, |
| 277 | projectIcon: { kind: "emoji", emoji: "🦊" }, |
| 278 | faviconPath: "/work/project/custom.svg", |
| 279 | defaultThreadEnvMode: "worktree", |
| 280 | }); |
| 281 | assert.equal(updated.title, "Renamed"); |
| 282 | assert.equal(updated.createdAt, created.createdAt); |
| 283 | assert.isTrue(updated.autoPull); |
| 284 | assert.deepEqual(updated.projectIcon, { kind: "emoji", emoji: "🦊" }); |
| 285 | assert.equal(updated.faviconPath, "/work/project/custom.svg"); |
| 286 | assert.equal(updated.defaultThreadEnvMode, "worktree"); |
| 287 | |
| 288 | const byId = yield* service.getById(projectId); |
| 289 | const byWorkspace = yield* service.getByWorkspaceRoot("/work/project/"); |
| 290 | assert.isTrue(Option.isSome(byId)); |
| 291 | assert.isTrue(Option.isSome(byWorkspace)); |
| 292 | assert.equal(Option.getOrThrow(byWorkspace).id, projectId); |
| 293 | assert.isTrue(Option.getOrThrow(byId).autoPull); |
| 294 | assert.deepEqual(Option.getOrThrow(byId).projectIcon, updated.projectIcon); |
| 295 | assert.equal(Option.getOrThrow(byId).faviconPath, updated.faviconPath); |
| 296 | assert.equal(Option.getOrThrow(byId).defaultThreadEnvMode, "worktree"); |
| 297 | const reset = yield* service.update({ |
| 298 | commandId: CommandId.make("command:project:reset-appearance"), |
| 299 | projectId, |
| 300 | autoPull: false, |
| 301 | projectIcon: null, |
| 302 | faviconPath: null, |
| 303 | defaultThreadEnvMode: null, |
| 304 | }); |
| 305 | assert.isFalse(reset.autoPull); |
| 306 | assert.isNull(reset.projectIcon); |
| 307 | assert.equal( |
| 308 | (yield* waitForProject( |
| 309 | service, |
| 310 | projectId, |
| 311 | (project) => project.faviconPath === hydratedCreated.faviconPath, |
| 312 | )).faviconPath, |
| 313 | hydratedCreated.faviconPath, |
| 314 | ); |
| 315 | assert.isNull(reset.defaultThreadEnvMode); |
| 316 | assert.deepEqual( |
| 317 | (yield* service.snapshot).projects.map((project) => project.id), |
| 318 | [projectId], |
| 319 | ); |
| 320 | |
| 321 | const deleted = yield* service.delete({ |
| 322 | commandId: CommandId.make("command:project:delete"), |
| 323 | projectId, |
| 324 | }); |
| 325 | assert.isNotNull(deleted.deletedAt); |
| 326 | assert.isTrue(Option.isNone(yield* service.getById(projectId))); |
| 327 | assert.isTrue(Option.isSome(yield* service.getById(projectId, { includeDeleted: true }))); |
| 328 | assert.deepEqual((yield* service.snapshot).projects, []); |
| 329 | |
| 330 | const sql = yield* SqlClient.SqlClient; |
| 331 | const changes = yield* sql<{ readonly event_type: string }>` |
| 332 | SELECT event_type |
| 333 | FROM orchestration_events |
| 334 | WHERE aggregate_kind = 'project' AND stream_id = ${projectId} |
| 335 | ORDER BY sequence ASC |
| 336 | `; |
| 337 | assert.deepEqual( |
| 338 | changes.map((change) => change.event_type), |
| 339 | ["project.created", "project.meta-updated", "project.meta-updated", "project.deleted"], |
| 340 | ); |
| 341 | }), |
| 342 | ); |
| 343 | |
| 344 | it.effect("records the outcome and duration of a committed project command", () => |
| 345 | Effect.gen(function* () { |
| 346 | const service = yield* ProjectService.ProjectService; |
| 347 | const successes = Metric.value( |
| 348 | Metric.withAttributes(orchestrationCommandsTotal, { |
| 349 | commandType: "project.create", |
| 350 | outcome: "success", |
| 351 | }), |
| 352 | ); |
| 353 | const durations = Metric.value( |
| 354 | Metric.withAttributes(orchestrationCommandDuration, { commandType: "project.create" }), |
| 355 | ); |
| 356 | const successesBefore = (yield* successes).count; |
| 357 | const durationsBefore = (yield* durations).count; |
| 358 | |
| 359 | yield* service.create({ |
| 360 | commandId: CommandId.make("command:metrics:create"), |
| 361 | projectId: ProjectId.make("project:metrics"), |
| 362 | title: "Metrics", |
| 363 | workspaceRoot: "/work/metrics", |
| 364 | }); |
| 365 | |
| 366 | assert.strictEqual((yield* successes).count - successesBefore, 1); |
| 367 | assert.strictEqual((yield* durations).count - durationsBefore, 1); |
| 368 | }), |
| 369 | ); |
| 370 | |
| 371 | it.effect("auto-bootstraps a workspace exactly once", () => |
| 372 | Effect.gen(function* () { |
| 373 | const service = yield* ProjectService.ProjectService; |
| 374 | yield* TestClock.setTime(Date.parse("2026-06-20T10:00:00.000Z")); |
| 375 | const input = { |
| 376 | commandId: CommandId.make("command:bootstrap:first"), |
| 377 | projectId: ProjectId.make("project:bootstrap"), |
| 378 | title: "Bootstrap", |
| 379 | workspaceRoot: "/work/bootstrap/", |
| 380 | }; |
| 381 | const first = yield* service.bootstrap(input); |
| 382 | const second = yield* service.bootstrap({ |
| 383 | ...input, |
| 384 | commandId: CommandId.make("command:bootstrap:second"), |
| 385 | projectId: ProjectId.make("project:bootstrap:unused"), |
| 386 | }); |
| 387 | assert.isTrue(first.created); |
| 388 | assert.isFalse(second.created); |
| 389 | assert.equal(second.project.id, first.project.id); |
| 390 | }), |
| 391 | ); |
| 392 | |
| 393 | it.effect("commits the event, the row and the receipt together, once per command id", () => |
| 394 | Effect.gen(function* () { |
| 395 | const service = yield* ProjectService.ProjectService; |
| 396 | const sql = yield* SqlClient.SqlClient; |
| 397 | const projectId = ProjectId.make("project:retry"); |
| 398 | const input = { |
| 399 | commandId: CommandId.make("command:retry:create"), |
| 400 | projectId, |
| 401 | title: "Retry", |
| 402 | workspaceRoot: "/work/retry", |
| 403 | }; |
| 404 | const first = yield* service.create(input); |
| 405 | const retried = yield* service.create(input); |
| 406 | assert.deepEqual(retried, first); |
| 407 | const updateInput = { |
| 408 | commandId: CommandId.make("command:retry:update"), |
| 409 | projectId, |
| 410 | autoPull: true, |
| 411 | }; |
| 412 | yield* service.update(updateInput); |
| 413 | yield* service.update(updateInput); |
| 414 | |
| 415 | const committed = yield* sql<{ |
| 416 | readonly sequence: number; |
| 417 | readonly event_type: string; |
| 418 | readonly receipt_sequence: number; |
| 419 | readonly status: string; |
| 420 | readonly aggregate_kind: string; |
| 421 | }>` |
| 422 | SELECT events.sequence, events.event_type, |
| 423 | receipts.result_sequence AS receipt_sequence, receipts.status, receipts.aggregate_kind |
| 424 | FROM orchestration_events AS events |
| 425 | JOIN orchestration_command_receipts AS receipts ON receipts.command_id = events.command_id |
| 426 | WHERE events.stream_id = ${projectId} |
| 427 | ORDER BY events.sequence |
| 428 | `; |
| 429 | assert.deepEqual( |
| 430 | committed.map((row) => [row.event_type, row.status, row.aggregate_kind]), |
| 431 | [ |
| 432 | ["project.created", "accepted", "project"], |
| 433 | ["project.meta-updated", "accepted", "project"], |
| 434 | ], |
| 435 | ); |
| 436 | for (const row of committed) assert.equal(row.receipt_sequence, row.sequence); |
| 437 | assert.isTrue(Option.getOrThrow(yield* service.getById(projectId)).autoPull); |
| 438 | }), |
| 439 | ); |
| 440 | |
| 441 | it.effect("replays a rejected receipt instead of re-planning the command", () => |
| 442 | Effect.gen(function* () { |
| 443 | const service = yield* ProjectService.ProjectService; |
| 444 | const sql = yield* SqlClient.SqlClient; |
| 445 | const projectId = ProjectId.make("project:rejected"); |
| 446 | yield* service.create({ |
| 447 | commandId: CommandId.make("command:rejected:create"), |
| 448 | projectId, |
| 449 | title: "Rejected", |
| 450 | workspaceRoot: "/work/rejected", |
| 451 | }); |
| 452 | const invalid = { |
| 453 | commandId: CommandId.make("command:rejected:script"), |
| 454 | projectId, |
| 455 | scripts: [ |
| 456 | { |
| 457 | id: "Not.Valid", |
| 458 | name: "Invalid", |
| 459 | command: "true", |
| 460 | icon: "play" as const, |
| 461 | runOnWorktreeCreate: false, |
| 462 | }, |
| 463 | ], |
| 464 | }; |
| 465 | const first = yield* service.update(invalid).pipe(Effect.flip); |
| 466 | assert.equal(first._tag, "ProjectInvalidError"); |
| 467 | assert.include(first.message, "Script IDs must be 1-24 lowercase letters"); |
| 468 | const replayed = yield* service |
| 469 | .update({ ...invalid, scripts: [{ ...invalid.scripts[0]!, id: "valid" }] }) |
| 470 | .pipe(Effect.flip); |
| 471 | assert.deepEqual(replayed, first); |
| 472 | const receipts = yield* sql<{ readonly status: string; readonly aggregate_id: string }>` |
| 473 | SELECT status, aggregate_id FROM orchestration_command_receipts |
| 474 | WHERE command_id = ${invalid.commandId} |
| 475 | `; |
| 476 | assert.deepEqual(receipts, [{ status: "rejected", aggregate_id: projectId }]); |
| 477 | const events = yield* sql` |
| 478 | SELECT sequence FROM orchestration_events WHERE command_id = ${invalid.commandId} |
| 479 | `; |
| 480 | assert.deepEqual(events, []); |
| 481 | assert.deepEqual(Option.getOrThrow(yield* service.getById(projectId)).scripts, []); |
| 482 | }), |
| 483 | ); |
| 484 | |
| 485 | it.effect("rejects a workspace another active project holds", () => |
| 486 | Effect.gen(function* () { |
| 487 | const service = yield* ProjectService.ProjectService; |
| 488 | const other = ProjectId.make("project:conflict:mover"); |
| 489 | yield* service.create({ |
| 490 | commandId: CommandId.make("command:conflict:holder"), |
| 491 | projectId: ProjectId.make("project:conflict:holder"), |
| 492 | title: "Holder", |
| 493 | workspaceRoot: "/work/conflict", |
| 494 | }); |
| 495 | yield* service.create({ |
| 496 | commandId: CommandId.make("command:conflict:mover"), |
| 497 | projectId: other, |
| 498 | title: "Mover", |
| 499 | workspaceRoot: "/work/conflict-mover", |
| 500 | }); |
| 501 | const move = yield* service |
| 502 | .update({ |
| 503 | commandId: CommandId.make("command:conflict:move"), |
| 504 | projectId: other, |
| 505 | workspaceRoot: "/work/conflict", |
| 506 | }) |
| 507 | .pipe(Effect.flip); |
| 508 | assert.equal(move._tag, "ProjectConflictError"); |
| 509 | assert.equal( |
| 510 | Option.getOrThrow(yield* service.getById(other)).workspaceRoot, |
| 511 | "/work/conflict-mover", |
| 512 | ); |
| 513 | }), |
| 514 | ); |
| 515 | |
| 516 | it.effect("refuses a WSL share path on Windows and allows it elsewhere", () => |
| 517 | Effect.gen(function* () { |
| 518 | const service = yield* ProjectService.ProjectService; |
| 519 | const onWindows = <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 520 | effect.pipe(Effect.provideService(HostProcess.Platform, "win32")); |
| 521 | const projectId = ProjectId.make("project:wsl-share"); |
| 522 | const refused = yield* onWindows( |
| 523 | service.create({ |
| 524 | commandId: CommandId.make("command:wsl-share"), |
| 525 | projectId, |
| 526 | title: "WSL", |
| 527 | workspaceRoot: "\\\\wsl$\\Ubuntu\\home\\ubuntu\\repo", |
| 528 | }), |
| 529 | ).pipe(Effect.flip); |
| 530 | assert.equal(refused._tag, "ProjectInvalidError"); |
| 531 | assert.isTrue(Option.isNone(yield* service.getById(projectId))); |
| 532 | |
| 533 | const mover = ProjectId.make("project:wsl-share:mover"); |
| 534 | yield* onWindows( |
| 535 | service.create({ |
| 536 | commandId: CommandId.make("command:wsl-share:mover"), |
| 537 | projectId: mover, |
| 538 | title: "Mover", |
| 539 | workspaceRoot: "C:\\work\\wsl-share-mover", |
| 540 | }), |
| 541 | ); |
| 542 | const move = yield* onWindows( |
| 543 | service.update({ |
| 544 | commandId: CommandId.make("command:wsl-share:move"), |
| 545 | projectId: mover, |
| 546 | workspaceRoot: "\\\\wsl.localhost\\Ubuntu\\home\\ubuntu\\repo", |
| 547 | }), |
| 548 | ).pipe(Effect.flip); |
| 549 | assert.equal(move._tag, "ProjectInvalidError"); |
| 550 | |
| 551 | const linux = yield* service |
| 552 | .create({ |
| 553 | commandId: CommandId.make("command:wsl-share:linux"), |
| 554 | projectId: ProjectId.make("project:wsl-share:linux"), |
| 555 | title: "Linux", |
| 556 | workspaceRoot: "\\\\wsl.localhost\\Ubuntu\\home\\ubuntu\\repo", |
| 557 | }) |
| 558 | .pipe(Effect.provideService(HostProcess.Platform, "linux")); |
| 559 | assert.equal(linux.title, "Linux"); |
| 560 | }), |
| 561 | ); |
| 562 | |
| 563 | it.effect("replays a workspace conflict after the workspace frees up", () => |
| 564 | Effect.gen(function* () { |
| 565 | const service = yield* ProjectService.ProjectService; |
| 566 | const holderId = ProjectId.make("project:freed:holder"); |
| 567 | yield* service.create({ |
| 568 | commandId: CommandId.make("command:freed:holder"), |
| 569 | projectId: holderId, |
| 570 | title: "Holder", |
| 571 | workspaceRoot: "/work/freed", |
| 572 | }); |
| 573 | const claim = { |
| 574 | commandId: CommandId.make("command:freed:claim"), |
| 575 | projectId: ProjectId.make("project:freed:claim"), |
| 576 | title: "Claim", |
| 577 | workspaceRoot: "/work/freed", |
| 578 | }; |
| 579 | const first = yield* service.create(claim).pipe(Effect.flip); |
| 580 | yield* service.delete({ |
| 581 | commandId: CommandId.make("command:freed:delete"), |
| 582 | projectId: holderId, |
| 583 | }); |
| 584 | const replayed = yield* service.create(claim).pipe(Effect.flip); |
| 585 | assert.deepEqual(replayed, first); |
| 586 | assert.instanceOf(replayed, ProjectService.ProjectConflictError); |
| 587 | assert.isTrue( |
| 588 | Option.isNone(yield* service.getById(claim.projectId, { includeDeleted: true })), |
| 589 | ); |
| 590 | }), |
| 591 | ); |
| 592 | |
| 593 | it.effect("returns the deleted project when a completed delete is retried", () => |
| 594 | Effect.gen(function* () { |
| 595 | const service = yield* ProjectService.ProjectService; |
| 596 | const sql = yield* SqlClient.SqlClient; |
| 597 | const projectId = ProjectId.make("project:delete-retry"); |
| 598 | yield* service.create({ |
| 599 | commandId: CommandId.make("command:delete-retry:create"), |
| 600 | projectId, |
| 601 | title: "Delete retry", |
| 602 | workspaceRoot: "/work/delete-retry", |
| 603 | }); |
| 604 | // Enrichment landing between the two deletes would change the project the retry reads. |
| 605 | yield* waitForProject( |
| 606 | service, |
| 607 | projectId, |
| 608 | (project) => project.faviconPath !== null && project.repositoryIdentity !== null, |
| 609 | ); |
| 610 | const input = { commandId: CommandId.make("command:delete-retry:delete"), projectId }; |
| 611 | const deleted = yield* service.delete(input); |
| 612 | assert.deepEqual(yield* service.delete(input), deleted); |
| 613 | const events = yield* sql<{ readonly command_id: string }>` |
| 614 | SELECT command_id FROM orchestration_events |
| 615 | WHERE stream_id = ${projectId} AND event_type = 'project.deleted' |
| 616 | `; |
| 617 | assert.deepEqual(events, [{ command_id: input.commandId }]); |
| 618 | }), |
| 619 | ); |
| 620 | |
| 621 | it.effect("treats a deleted project as missing for updates and new deletes", () => |
| 622 | Effect.gen(function* () { |
| 623 | const service = yield* ProjectService.ProjectService; |
| 624 | const sql = yield* SqlClient.SqlClient; |
| 625 | const projectId = ProjectId.make("project:deleted-target"); |
| 626 | yield* service.create({ |
| 627 | commandId: CommandId.make("command:deleted-target:create"), |
| 628 | projectId, |
| 629 | title: "Deleted target", |
| 630 | workspaceRoot: "/work/deleted-target", |
| 631 | }); |
| 632 | yield* service.delete({ |
| 633 | commandId: CommandId.make("command:deleted-target:delete"), |
| 634 | projectId, |
| 635 | }); |
| 636 | const again = yield* service |
| 637 | .delete({ commandId: CommandId.make("command:deleted-target:delete-again"), projectId }) |
| 638 | .pipe(Effect.flip); |
| 639 | assert.instanceOf(again, ProjectService.ProjectNotFoundError); |
| 640 | const events = yield* sql<{ readonly event_type: string }>` |
| 641 | SELECT event_type FROM orchestration_events |
| 642 | WHERE stream_id = ${projectId} ORDER BY sequence ASC |
| 643 | `; |
| 644 | assert.deepEqual( |
| 645 | events.map((event) => event.event_type), |
| 646 | ["project.created", "project.deleted"], |
| 647 | ); |
| 648 | }), |
| 649 | ); |
| 650 | |
| 651 | it.effect("rolls back the event and receipt when the row write fails", () => |
| 652 | Effect.gen(function* () { |
| 653 | const service = yield* ProjectService.ProjectService; |
| 654 | const sql = yield* SqlClient.SqlClient; |
| 655 | const projectId = ProjectId.make("project:atomic"); |
| 656 | yield* sql` |
| 657 | CREATE TRIGGER fail_project_row BEFORE INSERT ON projection_projects |
| 658 | WHEN NEW.project_id = 'project:atomic' |
| 659 | BEGIN SELECT RAISE(ABORT, 'injected row failure'); END |
| 660 | `; |
| 661 | const input = { |
| 662 | commandId: CommandId.make("command:atomic:create"), |
| 663 | projectId, |
| 664 | title: "Atomic", |
| 665 | workspaceRoot: "/work/atomic", |
| 666 | }; |
| 667 | const failure = yield* service.create(input).pipe(Effect.flip); |
| 668 | assert.equal(failure._tag, "ProjectOperationError"); |
| 669 | const leftovers = yield* sql<{ readonly count: number }>` |
| 670 | SELECT |
| 671 | (SELECT COUNT(*) FROM orchestration_events WHERE command_id = ${input.commandId}) + |
| 672 | (SELECT COUNT(*) FROM orchestration_command_receipts WHERE command_id = ${input.commandId}) + |
| 673 | (SELECT COUNT(*) FROM projection_projects WHERE project_id = ${projectId}) AS count |
| 674 | `; |
| 675 | assert.equal(leftovers[0]?.count, 0); |
| 676 | yield* sql`DROP TRIGGER fail_project_row`; |
| 677 | assert.equal((yield* service.create(input)).id, projectId); |
| 678 | }), |
| 679 | ); |
| 680 | }); |
| 681 | |
| 682 | it.effect("keeps project snapshots available when optional metadata enrichment fails", () => |
| 683 | Effect.gen(function* () { |
| 684 | const layerFailingMetadata = Layer.merge( |
| 685 | Layer.succeed(RepositoryIdentityResolver.RepositoryIdentityResolver, { |
| 686 | resolve: () => Effect.succeed(null), |
| 687 | }), |
| 688 | Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, { |
| 689 | invalidate: () => Effect.void, |
| 690 | resolvePath: (workspaceRoot) => |
| 691 | Effect.fail( |
| 692 | new ProjectFaviconResolver.ProjectFaviconResolutionError({ |
| 693 | operation: "stat-candidate", |
| 694 | workspaceRoot, |
| 695 | cause: "permission denied", |
| 696 | }), |
| 697 | ), |
| 698 | }), |
| 699 | ); |
| 700 | |
| 701 | yield* Effect.gen(function* () { |
| 702 | const service = yield* ProjectService.ProjectService; |
| 703 | const projectId = ProjectId.make("project:failed-enrichment"); |
| 704 | yield* service.create({ |
| 705 | commandId: CommandId.make("command:failed-enrichment:create"), |
| 706 | projectId, |
| 707 | title: "Still visible", |
| 708 | workspaceRoot: "/work/failed-enrichment", |
| 709 | }); |
| 710 | |
| 711 | const snapshot = yield* service.snapshot; |
| 712 | assert.equal(snapshot.projects.length, 1); |
| 713 | assert.equal(snapshot.projects[0]?.id, projectId); |
| 714 | assert.equal(snapshot.projects[0]?.title, "Still visible"); |
| 715 | assert.isNull(snapshot.projects[0]?.repositoryIdentity ?? null); |
| 716 | assert.isNull(snapshot.projects[0]?.faviconPath ?? null); |
| 717 | }).pipe(Effect.provide(layerTestFor(layerFailingMetadata))); |
| 718 | }), |
| 719 | ); |
| 720 | |
| 721 | it.effect("invalidates workspace-derived metadata when a project moves", () => |
| 722 | Effect.gen(function* () { |
| 723 | const metadataVersion = yield* Ref.make(1); |
| 724 | const layerVersionedMetadata = Layer.merge( |
| 725 | Layer.succeed(RepositoryIdentityResolver.RepositoryIdentityResolver, { |
| 726 | resolve: (workspaceRoot) => |
| 727 | Ref.get(metadataVersion).pipe( |
| 728 | Effect.map((version) => ({ |
| 729 | canonicalKey: `example.test/v${version}${workspaceRoot}`, |
| 730 | locator: { |
| 731 | source: "git-remote" as const, |
| 732 | remoteName: "origin", |
| 733 | remoteUrl: `https://example.test/v${version}${workspaceRoot}.git`, |
| 734 | }, |
| 735 | rootPath: workspaceRoot, |
| 736 | })), |
| 737 | ), |
| 738 | }), |
| 739 | Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, { |
| 740 | invalidate: () => Effect.void, |
| 741 | resolvePath: (workspaceRoot) => |
| 742 | Ref.get(metadataVersion).pipe( |
| 743 | Effect.map((version) => `${workspaceRoot}/favicon-v${version}.svg`), |
| 744 | ), |
| 745 | }), |
| 746 | ); |
| 747 | |
| 748 | yield* Effect.gen(function* () { |
| 749 | const service = yield* ProjectService.ProjectService; |
| 750 | const projectId = ProjectId.make("project:moved-enrichment"); |
| 751 | yield* service.create({ |
| 752 | commandId: CommandId.make("command:moved-enrichment:create"), |
| 753 | projectId, |
| 754 | title: "Moved", |
| 755 | workspaceRoot: "/work/original", |
| 756 | }); |
| 757 | assert.equal( |
| 758 | (yield* waitForProject( |
| 759 | service, |
| 760 | projectId, |
| 761 | (project) => project.faviconPath === "/work/original/favicon-v1.svg", |
| 762 | )).faviconPath, |
| 763 | "/work/original/favicon-v1.svg", |
| 764 | ); |
| 765 | |
| 766 | yield* Ref.set(metadataVersion, 2); |
| 767 | yield* service.update({ |
| 768 | commandId: CommandId.make("command:moved-enrichment:away"), |
| 769 | projectId, |
| 770 | workspaceRoot: "/work/temporary", |
| 771 | }); |
| 772 | yield* service.snapshot; |
| 773 | |
| 774 | yield* Ref.set(metadataVersion, 3); |
| 775 | yield* service.update({ |
| 776 | commandId: CommandId.make("command:moved-enrichment:return"), |
| 777 | projectId, |
| 778 | workspaceRoot: "/work/original", |
| 779 | }); |
| 780 | assert.equal( |
| 781 | (yield* waitForProject( |
| 782 | service, |
| 783 | projectId, |
| 784 | (project) => project.faviconPath === "/work/original/favicon-v3.svg", |
| 785 | )).faviconPath, |
| 786 | "/work/original/favicon-v3.svg", |
| 787 | ); |
| 788 | }).pipe(Effect.provide(layerTestFor(layerVersionedMetadata))); |
| 789 | }), |
| 790 | ); |
| 791 | |
| 792 | it.effect("resolves a restored icon immediately after reselecting its saved path", () => |
| 793 | Effect.gen(function* () { |
| 794 | const fileSystem = yield* FileSystem.FileSystem; |
| 795 | const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "project-icon-reselect-" }); |
| 796 | const iconPath = `${cwd}/icon.svg`; |
| 797 | const metadata = Layer.merge( |
| 798 | Layer.succeed(RepositoryIdentityResolver.RepositoryIdentityResolver, { |
| 799 | resolve: () => Effect.succeed(null), |
| 800 | }), |
| 801 | ProjectFaviconResolver.layer.pipe( |
| 802 | Layer.provide(layerWorkspacePaths), |
| 803 | Layer.provide(T3ProjectFileLoader.layer), |
| 804 | Layer.provide(NodeServices.layer), |
| 805 | ), |
| 806 | ); |
| 807 | yield* Effect.gen(function* () { |
| 808 | const service = yield* ProjectService.ProjectService; |
| 809 | const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver; |
| 810 | const projectId = ProjectId.make("project:icon-reselect"); |
| 811 | yield* service.create({ |
| 812 | commandId: CommandId.make("command:icon-reselect:create"), |
| 813 | projectId, |
| 814 | title: "Icon reselection", |
| 815 | workspaceRoot: cwd, |
| 816 | }); |
| 817 | yield* service.update({ |
| 818 | commandId: CommandId.make("command:icon-reselect:missing"), |
| 819 | projectId, |
| 820 | faviconPath: "icon.svg", |
| 821 | }); |
| 822 | assert.isNull(yield* resolver.resolvePath(cwd, "icon.svg")); |
| 823 | yield* fileSystem.writeFileString(iconPath, '<svg xmlns="http://www.w3.org/2000/svg"/>'); |
| 824 | assert.isNull(yield* resolver.resolvePath(cwd, "icon.svg")); |
| 825 | yield* service.update({ |
| 826 | commandId: CommandId.make("command:icon-reselect:restored"), |
| 827 | projectId, |
| 828 | faviconPath: "icon.svg", |
| 829 | }); |
| 830 | assert.equal(yield* resolver.resolvePath(cwd, "icon.svg"), iconPath); |
| 831 | }).pipe(Effect.provide(layerTestFor(metadata))); |
| 832 | }).pipe(Effect.provide(NodeServices.layer)), |
| 833 | ); |
| 834 | |
| 835 | it.effect("refuses a worktree container as a new project and as a new workspace root", () => |
| 836 | Effect.gen(function* () { |
| 837 | const fileSystem = yield* FileSystem.FileSystem; |
| 838 | const processRunner = yield* ProcessRunner.ProcessRunner; |
| 839 | const fixture = yield* fileSystem.makeTempDirectoryScoped({ prefix: "project-container-" }); |
| 840 | const git = (cwd: string, ...args: ReadonlyArray<string>) => |
| 841 | processRunner.run({ |
| 842 | command: "git", |
| 843 | args: ["-C", cwd, "-c", "user.email=t@t", "-c", "user.name=t", ...args], |
| 844 | }); |
| 845 | const source = `${fixture}/source`; |
| 846 | const container = `${fixture}/container`; |
| 847 | yield* fileSystem.makeDirectory(source); |
| 848 | yield* fileSystem.makeDirectory(container); |
| 849 | yield* git(source, "init", "-b", "develop"); |
| 850 | yield* fileSystem.writeFileString(`${source}/README.md`, "readme\n"); |
| 851 | yield* git(source, "add", "README.md"); |
| 852 | yield* git(source, "commit", "-m", "init"); |
| 853 | yield* git(fixture, "clone", "--bare", source, `${container}/.bare`); |
| 854 | yield* fileSystem.writeFileString(`${container}/.git`, "gitdir: ./.bare\n"); |
| 855 | yield* git(container, "worktree", "add", "develop", "develop"); |
| 856 | |
| 857 | yield* Effect.gen(function* () { |
| 858 | const service = yield* ProjectService.ProjectService; |
| 859 | // As cloned the container is bare; Initialize Git or a config edit clears core.bare. |
| 860 | for (const coreBare of ["true", "false"]) { |
| 861 | yield* git(container, "config", "core.bare", coreBare); |
| 862 | const rejected = yield* service |
| 863 | .create({ |
| 864 | commandId: CommandId.make(`command:container:create:${coreBare}`), |
| 865 | projectId: ProjectId.make(`project:container:${coreBare}`), |
| 866 | title: "Container", |
| 867 | workspaceRoot: container, |
| 868 | }) |
| 869 | .pipe(Effect.flip); |
| 870 | assert.equal(rejected._tag, "ProjectInvalidError", `core.bare=${coreBare}`); |
| 871 | assert.include(rejected.message, "Add one of its worktrees"); |
| 872 | assert.isTrue(Option.isNone(yield* service.getByWorkspaceRoot(container))); |
| 873 | } |
| 874 | |
| 875 | const projectId = ProjectId.make("project:container:worktree"); |
| 876 | const worktree = yield* service.create({ |
| 877 | commandId: CommandId.make("command:container:worktree"), |
| 878 | projectId, |
| 879 | title: "Worktree", |
| 880 | workspaceRoot: `${container}/develop`, |
| 881 | }); |
| 882 | const moved = yield* service |
| 883 | .update({ |
| 884 | commandId: CommandId.make("command:container:move"), |
| 885 | projectId, |
| 886 | workspaceRoot: container, |
| 887 | }) |
| 888 | .pipe(Effect.flip); |
| 889 | assert.equal(moved._tag, "ProjectInvalidError"); |
| 890 | assert.equal( |
| 891 | Option.getOrThrow(yield* service.getById(projectId)).workspaceRoot, |
| 892 | worktree.workspaceRoot, |
| 893 | ); |
| 894 | }).pipe(Effect.provide(layerTest)); |
| 895 | }).pipe(Effect.provide(ProcessRunner.layer.pipe(Layer.provideMerge(NodeServices.layer)))), |
| 896 | ); |
| 897 | |
| 898 | it.effect("serializes two projects claiming the same workspace root", () => |
| 899 | Effect.gen(function* () { |
| 900 | const eventSink = yield* EventSink.EventSinkV2; |
| 901 | const firstReachedCommit = yield* Deferred.make<void>(); |
| 902 | const releaseFirst = yield* Deferred.make<void>(); |
| 903 | const gatedSink = EventSink.EventSinkV2.of({ |
| 904 | ...eventSink, |
| 905 | commitProjectCommand: (input) => |
| 906 | input.projectId === "project:race:first" |
| 907 | ? Deferred.succeed(firstReachedCommit, undefined).pipe( |
| 908 | Effect.andThen(Deferred.await(releaseFirst)), |
| 909 | Effect.andThen(eventSink.commitProjectCommand(input)), |
| 910 | ) |
| 911 | : eventSink.commitProjectCommand(input), |
| 912 | }); |
| 913 | const service = yield* ProjectService.make.pipe( |
| 914 | Effect.provideService(EventSink.EventSinkV2, gatedSink), |
| 915 | ); |
| 916 | const claim = (name: string) => |
| 917 | service.create({ |
| 918 | commandId: CommandId.make(`command:race:${name}`), |
| 919 | projectId: ProjectId.make(`project:race:${name}`), |
| 920 | title: name, |
| 921 | workspaceRoot: "/work/race", |
| 922 | }); |
| 923 | const first = yield* claim("first").pipe(Effect.forkChild({ startImmediately: true })); |
| 924 | yield* Deferred.await(firstReachedCommit); |
| 925 | const second = yield* claim("second").pipe( |
| 926 | Effect.flip, |
| 927 | Effect.forkChild({ startImmediately: true }), |
| 928 | ); |
| 929 | yield* Effect.yieldNow; |
| 930 | assert.isUndefined(second.pollUnsafe()); |
| 931 | yield* Deferred.succeed(releaseFirst, undefined); |
| 932 | assert.equal((yield* Fiber.join(first)).id, "project:race:first"); |
| 933 | assert.equal((yield* Fiber.join(second))._tag, "ProjectConflictError"); |
| 934 | }).pipe(Effect.provide(layerProjectServiceDependencies)), |
| 935 | ); |
| 936 | |
| 937 | it.effect("rejects an update that waited on the lock while its project was deleted", () => |
| 938 | Effect.gen(function* () { |
| 939 | const eventSink = yield* EventSink.EventSinkV2; |
| 940 | const deleteReachedCommit = yield* Deferred.make<void>(); |
| 941 | const releaseDelete = yield* Deferred.make<void>(); |
| 942 | const gatedSink = EventSink.EventSinkV2.of({ |
| 943 | ...eventSink, |
| 944 | commitProjectCommand: (input) => |
| 945 | input.commandType === "project.delete" |
| 946 | ? Deferred.succeed(deleteReachedCommit, undefined).pipe( |
| 947 | Effect.andThen(Deferred.await(releaseDelete)), |
| 948 | Effect.andThen(eventSink.commitProjectCommand(input)), |
| 949 | ) |
| 950 | : eventSink.commitProjectCommand(input), |
| 951 | }); |
| 952 | const service = yield* ProjectService.make.pipe( |
| 953 | Effect.provideService(EventSink.EventSinkV2, gatedSink), |
| 954 | ); |
| 955 | const sql = yield* SqlClient.SqlClient; |
| 956 | const projectId = ProjectId.make("project:update-race"); |
| 957 | yield* service.create({ |
| 958 | commandId: CommandId.make("command:update-race:create"), |
| 959 | projectId, |
| 960 | title: "Update race", |
| 961 | workspaceRoot: "/work/update-race", |
| 962 | }); |
| 963 | const deletion = yield* service |
| 964 | .delete({ commandId: CommandId.make("command:update-race:delete"), projectId }) |
| 965 | .pipe(Effect.forkChild({ startImmediately: true })); |
| 966 | yield* Deferred.await(deleteReachedCommit); |
| 967 | const updateCommandId = CommandId.make("command:update-race:update"); |
| 968 | const update = yield* service |
| 969 | .update({ commandId: updateCommandId, projectId, title: "Too late" }) |
| 970 | .pipe(Effect.flip, Effect.forkChild({ startImmediately: true })); |
| 971 | yield* Effect.yieldNow; |
| 972 | yield* Deferred.succeed(releaseDelete, undefined); |
| 973 | assert.isNotNull((yield* Fiber.join(deletion)).deletedAt); |
| 974 | assert.instanceOf(yield* Fiber.join(update), ProjectService.ProjectNotFoundError); |
| 975 | const receipts = yield* sql<{ readonly status: string }>` |
| 976 | SELECT status FROM orchestration_command_receipts WHERE command_id = ${updateCommandId} |
| 977 | `; |
| 978 | assert.deepEqual(receipts, [{ status: "rejected" }]); |
| 979 | const events = yield* sql<{ readonly event_type: string }>` |
| 980 | SELECT event_type FROM orchestration_events |
| 981 | WHERE stream_id = ${projectId} ORDER BY sequence ASC |
| 982 | `; |
| 983 | assert.deepEqual( |
| 984 | events.map((event) => event.event_type), |
| 985 | ["project.created", "project.deleted"], |
| 986 | ); |
| 987 | assert.equal( |
| 988 | Option.getOrThrow(yield* service.getById(projectId, { includeDeleted: true })).title, |
| 989 | "Update race", |
| 990 | ); |
| 991 | }).pipe(Effect.provide(layerProjectServiceDependencies)), |
| 992 | ); |