|
| 1 | +import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 2 | +import { describe, expect, it } from "@effect/vitest"; |
| 3 | +import { |
| 4 | + applyCollaborationUpdate, |
| 5 | + EnvironmentId, |
| 6 | + type OrchestrationV2ServerCommand, |
| 7 | + type OrchestrationV2ThreadShell, |
| 8 | + ProviderInstanceId, |
| 9 | + ThreadId, |
| 10 | + type ThreadMetadataMcpUpdateInput, |
| 11 | +} from "@t3tools/contracts"; |
| 12 | +import * as DateTime from "effect/DateTime"; |
| 13 | +import * as Effect from "effect/Effect"; |
| 14 | +import * as Layer from "effect/Layer"; |
| 15 | + |
| 16 | +import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts"; |
| 17 | +import type * as McpInvocationContext from "./McpInvocationContext.ts"; |
| 18 | +import * as ThreadMetadataMcpService from "./ThreadMetadataMcpService.ts"; |
| 19 | + |
| 20 | +const room = ThreadId.make("thread-room"); |
| 21 | +const growth = ThreadId.make("thread-growth"); |
| 22 | +const cfo = ThreadId.make("thread-cfo"); |
| 23 | +const gone = ThreadId.make("thread-gone"); |
| 24 | +const deleted = ThreadId.make("thread-deleted"); |
| 25 | +const NOW = "2026-11-24T15:00:00.000Z"; |
| 26 | + |
| 27 | +const scope: McpInvocationContext.McpInvocationScope = { |
| 28 | + environmentId: EnvironmentId.make("environment-room-test"), |
| 29 | + requestNamespace: "provider-session-room-test", |
| 30 | + thread: { |
| 31 | + threadId: room, |
| 32 | + providerSessionId: "provider-session-room-test", |
| 33 | + providerInstanceId: ProviderInstanceId.make("claudeAgent"), |
| 34 | + }, |
| 35 | + client: undefined, |
| 36 | + capabilities: new Set(["orchestration"]), |
| 37 | + issuedAt: 1, |
| 38 | +}; |
| 39 | + |
| 40 | +const shell = (id: ThreadId, overrides: Partial<OrchestrationV2ThreadShell> = {}) => |
| 41 | + ({ |
| 42 | + id, |
| 43 | + title: id, |
| 44 | + titleRegeneration: null, |
| 45 | + linkedPullRequest: null, |
| 46 | + updatedAt: DateTime.makeUnsafe(NOW), |
| 47 | + deletedAt: null, |
| 48 | + ...overrides, |
| 49 | + }) as OrchestrationV2ThreadShell; |
| 50 | + |
| 51 | +/** The service against an in-memory thread table whose dispatch applies the collaboration update. */ |
| 52 | +const makeHarness = () => { |
| 53 | + const threads = new Map<ThreadId, OrchestrationV2ThreadShell>([ |
| 54 | + [room, shell(room)], |
| 55 | + [growth, shell(growth)], |
| 56 | + [cfo, shell(cfo)], |
| 57 | + [deleted, shell(deleted, { deletedAt: DateTime.makeUnsafe(NOW) })], |
| 58 | + ]); |
| 59 | + const dispatched: OrchestrationV2ServerCommand[] = []; |
| 60 | + const layer = ThreadMetadataMcpService.layer.pipe( |
| 61 | + Layer.provide( |
| 62 | + Layer.mergeAll( |
| 63 | + Layer.mock(ThreadManagementService.ThreadManagementService)({ |
| 64 | + getThreadShell: (threadId) => Effect.succeed(threads.get(threadId) ?? null), |
| 65 | + dispatch: (command) => |
| 66 | + Effect.sync(() => { |
| 67 | + dispatched.push(command); |
| 68 | + if (command.type === "thread.metadata.update" && command.collaboration) { |
| 69 | + const current = threads.get(command.threadId)!; |
| 70 | + threads.set(command.threadId, { |
| 71 | + ...current, |
| 72 | + collaboration: applyCollaborationUpdate( |
| 73 | + current.collaboration, |
| 74 | + command.collaboration, |
| 75 | + NOW, |
| 76 | + ), |
| 77 | + }); |
| 78 | + } |
| 79 | + // No stored events: the service answers from the projection, as on a retry. |
| 80 | + return { sequence: dispatched.length, storedEvents: [] }; |
| 81 | + }), |
| 82 | + } satisfies Partial<ThreadManagementService.ThreadManagementService["Service"]>), |
| 83 | + NodeCrypto.layer, |
| 84 | + ), |
| 85 | + ), |
| 86 | + ); |
| 87 | + const update = (input: ThreadMetadataMcpUpdateInput) => |
| 88 | + Effect.gen(function* () { |
| 89 | + const service = yield* ThreadMetadataMcpService.ThreadMetadataMcpService; |
| 90 | + return yield* service.update(scope, input); |
| 91 | + }).pipe(Effect.provide(layer)); |
| 92 | + return { dispatched, update }; |
| 93 | +}; |
| 94 | + |
| 95 | +const openRoom = (members: ReadonlyArray<ThreadId>): ThreadMetadataMcpUpdateInput => ({ |
| 96 | + action: "collaborate", |
| 97 | + collaboration: { |
| 98 | + type: "open_room", |
| 99 | + room: { topic: "How we set up Growth", members, linearIssue: "CLO-1189" }, |
| 100 | + }, |
| 101 | +}); |
| 102 | + |
| 103 | +describe("t3_thread_update collaborate", () => { |
| 104 | + it.effect("refuses a room member that does not exist or was deleted, before dispatching", () => |
| 105 | + Effect.gen(function* () { |
| 106 | + const { dispatched, update } = makeHarness(); |
| 107 | + const missing = yield* Effect.flip(update(openRoom([growth, gone]))); |
| 108 | + expect(missing.code).toBe("invalid_request"); |
| 109 | + expect(missing.message).toBe(`Room member ${gone} was not found.`); |
| 110 | + const removed = yield* Effect.flip(update(openRoom([deleted, cfo]))); |
| 111 | + expect(removed.message).toBe(`Room member ${deleted} was not found.`); |
| 112 | + expect(dispatched).toEqual([]); |
| 113 | + }), |
| 114 | + ); |
| 115 | + |
| 116 | + it.effect("refuses the room thread as its own member", () => |
| 117 | + Effect.gen(function* () { |
| 118 | + const { dispatched, update } = makeHarness(); |
| 119 | + const error = yield* Effect.flip(update(openRoom([room, growth]))); |
| 120 | + expect(error.code).toBe("invalid_request"); |
| 121 | + expect(error.message).toMatch(/the room thread itself facilitates/); |
| 122 | + expect(dispatched).toEqual([]); |
| 123 | + }), |
| 124 | + ); |
| 125 | + |
| 126 | + it.effect("opens a room, then closes it once and returns the Linear and memory record", () => |
| 127 | + Effect.gen(function* () { |
| 128 | + const { dispatched, update } = makeHarness(); |
| 129 | + const opened = yield* update(openRoom([growth, cfo])); |
| 130 | + expect(opened.collaboration?.room?.members).toEqual([growth, cfo]); |
| 131 | + expect(opened.roomRecord).toBeUndefined(); |
| 132 | + |
| 133 | + const close: ThreadMetadataMcpUpdateInput = { |
| 134 | + action: "collaborate", |
| 135 | + collaboration: { |
| 136 | + type: "close_room", |
| 137 | + close: { |
| 138 | + summary: "Growth owns outbound.", |
| 139 | + decisions: [{ text: "Growth runs the list" }], |
| 140 | + }, |
| 141 | + }, |
| 142 | + }; |
| 143 | + const closed = yield* update(close); |
| 144 | + expect(closed.roomRecord?.linearIssue).toBe("CLO-1189"); |
| 145 | + expect(closed.roomRecord?.linearComment).toContain("- Growth runs the list"); |
| 146 | + expect(closed.roomRecord?.memoryEntry).toContain("How we set up Growth"); |
| 147 | + |
| 148 | + const again = yield* Effect.flip(update(close)); |
| 149 | + expect(again.message).toMatch(/no open room/); |
| 150 | + expect(dispatched).toHaveLength(2); |
| 151 | + }), |
| 152 | + ); |
| 153 | +}); |
0 commit comments