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 { describe, expect, it } from "@effect/vitest"; |
| 2 | import { |
| 3 | AuthFilesystemWriteScope, |
| 4 | EnvironmentId, |
| 5 | ProjectId, |
| 6 | ProjectReadFileError, |
| 7 | ProjectReadFileInput, |
| 8 | ProjectReadFileResult, |
| 9 | ThreadId, |
| 10 | type AuthSessionState, |
| 11 | type ScopedProjectRef, |
| 12 | WS_METHODS, |
| 13 | } from "@t3tools/contracts"; |
| 14 | import { vi } from "vite-plus/test"; |
| 15 | import * as SecureRandom from "effect/crypto/SecureRandom"; |
| 16 | import * as Effect from "effect/Effect"; |
| 17 | import * as Deferred from "effect/Deferred"; |
| 18 | import * as Fiber from "effect/Fiber"; |
| 19 | import * as Layer from "effect/Layer"; |
| 20 | import * as Option from "effect/Option"; |
| 21 | import * as Schema from "effect/Schema"; |
| 22 | import * as SubscriptionRef from "effect/SubscriptionRef"; |
| 23 | import * as Stream from "effect/Stream"; |
| 24 | import { Atom, AtomRegistry, AsyncResult } from "effect/reactivity"; |
| 25 | import { AVAILABLE_CONNECTION_STATE, BearerConnectionTarget } from "../connection/model.ts"; |
| 26 | |
| 27 | import * as EnvironmentRegistry from "../connection/registry.ts"; |
| 28 | import * as EnvironmentSupervisor from "../connection/supervisor.ts"; |
| 29 | import type { RpcSession } from "../rpc/session.ts"; |
| 30 | import type { EnvironmentProject } from "./models.ts"; |
| 31 | import { createProjectEnvironmentAtoms } from "./projectCommands.ts"; |
| 32 | |
| 33 | const decodeReadFileInput = Schema.decodeUnknownSync(ProjectReadFileInput); |
| 34 | const decodeReadFileResult = Schema.decodeUnknownSync(ProjectReadFileResult); |
| 35 | const ENVIRONMENT_ID = EnvironmentId.make("environment-1"); |
| 36 | vi.mock("./session.ts", () => ({ |
| 37 | createEnvironmentSessionAtoms: () => ({ sessionStateAtom: sessions }), |
| 38 | })); |
| 39 | const sessions = Atom.family((_id: EnvironmentId) => |
| 40 | Atom.make<AsyncResult.AsyncResult<AuthSessionState>>(AsyncResult.initial()), |
| 41 | ); |
| 42 | const PROJECT_ID = ProjectId.make("scratch"); |
| 43 | const PROJECT = { |
| 44 | id: PROJECT_ID, |
| 45 | environmentId: ENVIRONMENT_ID, |
| 46 | workspaceRoot: "/scratch", |
| 47 | } as EnvironmentProject; |
| 48 | |
| 49 | const makeHarness = Effect.fn("TestProjectCommands.makeHarness")(function* () { |
| 50 | const metadataRequests: string[][] = []; |
| 51 | const file = { byteLength: 5 }; |
| 52 | const readResponse = { |
| 53 | wait: Effect.void as Effect.Effect<void, ProjectReadFileError>, |
| 54 | missing: false, |
| 55 | }; |
| 56 | const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ |
| 57 | target: new BearerConnectionTarget({ |
| 58 | environmentId: ENVIRONMENT_ID, |
| 59 | label: "Test", |
| 60 | connectionId: "test", |
| 61 | }), |
| 62 | session: yield* SubscriptionRef.make( |
| 63 | Option.some({ |
| 64 | client: { |
| 65 | [WS_METHODS.projectsEnsureScratch]: () => Effect.succeed({ projectId: PROJECT_ID }), |
| 66 | [WS_METHODS.projectsReadFile]: () => |
| 67 | Effect.gen(function* () { |
| 68 | const byteLength = file.byteLength; |
| 69 | yield* readResponse.wait; |
| 70 | return decodeReadFileResult( |
| 71 | readResponse.missing |
| 72 | ? { |
| 73 | relativePath: "foo", |
| 74 | byteLength: 0, |
| 75 | contents: "", |
| 76 | truncated: false, |
| 77 | exists: false, |
| 78 | } |
| 79 | : { relativePath: "foo", byteLength, contents: "old", truncated: false }, |
| 80 | ); |
| 81 | }), |
| 82 | [WS_METHODS.projectsWriteFile]: () => |
| 83 | Effect.sync(() => { |
| 84 | file.byteLength = 10; |
| 85 | return { relativePath: "foo" }; |
| 86 | }), |
| 87 | [WS_METHODS.filesystemGetMetadata]: (input: { paths: string[] }) => |
| 88 | Effect.sync(() => { |
| 89 | metadataRequests.push(input.paths); |
| 90 | return { |
| 91 | entries: input.paths.map(() => |
| 92 | readResponse.missing |
| 93 | ? null |
| 94 | : { kind: "file" as const, byteLength: file.byteLength }, |
| 95 | ), |
| 96 | }; |
| 97 | }), |
| 98 | }, |
| 99 | } as unknown as RpcSession), |
| 100 | ), |
| 101 | state: yield* SubscriptionRef.make({ |
| 102 | ...AVAILABLE_CONNECTION_STATE, |
| 103 | phase: "connected" as const, |
| 104 | }), |
| 105 | } as EnvironmentSupervisor.EnvironmentSupervisor["Service"]); |
| 106 | const runtime = Atom.runtime( |
| 107 | Layer.mergeAll( |
| 108 | Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, { |
| 109 | run: (_environmentId, effect) => |
| 110 | Effect.provideService(effect, EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 111 | followStream: (_environmentId, stream) => |
| 112 | Stream.provideService(stream, EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 113 | } as EnvironmentRegistry.EnvironmentRegistry["Service"]), |
| 114 | Layer.succeed( |
| 115 | SecureRandom.SecureRandom, |
| 116 | SecureRandom.make((size) => new Uint8Array(size)), |
| 117 | ), |
| 118 | ), |
| 119 | ); |
| 120 | const storedProject = Atom.make<EnvironmentProject | null>(null); |
| 121 | const commands = createProjectEnvironmentAtoms(runtime, { |
| 122 | projectAtom: (ref: ScopedProjectRef) => |
| 123 | ref.environmentId === ENVIRONMENT_ID && ref.projectId === PROJECT_ID |
| 124 | ? storedProject |
| 125 | : Atom.make(null), |
| 126 | }); |
| 127 | const registry = AtomRegistry.make(); |
| 128 | yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose())); |
| 129 | registry.set( |
| 130 | sessions(ENVIRONMENT_ID), |
| 131 | AsyncResult.success({ |
| 132 | authenticated: true, |
| 133 | auth: { |
| 134 | policy: "remote-reachable" as const, |
| 135 | bootstrapMethods: [], |
| 136 | sessionMethods: [], |
| 137 | sessionCookieName: "test", |
| 138 | }, |
| 139 | scopes: [AuthFilesystemWriteScope], |
| 140 | permissions: [AuthFilesystemWriteScope], |
| 141 | }), |
| 142 | ); |
| 143 | const openScratch = Effect.exit( |
| 144 | commands.openScratch.run(registry, { environmentId: ENVIRONMENT_ID, input: {} }), |
| 145 | ); |
| 146 | return { registry, storedProject, openScratch, commands, metadataRequests, readResponse }; |
| 147 | }); |
| 148 | |
| 149 | describe("writeFile metadata", () => { |
| 150 | it.effect("retires a saved overlay after an equal read succeeds, preserving newer drafts", () => |
| 151 | Effect.gen(function* () { |
| 152 | const h = yield* makeHarness(); |
| 153 | const target = { environmentId: ENVIRONMENT_ID, cwd: "/repo", relativePath: "foo" }; |
| 154 | const query = h.commands.readFile({ |
| 155 | environmentId: ENVIRONMENT_ID, |
| 156 | input: { cwd: target.cwd, relativePath: target.relativePath }, |
| 157 | }); |
| 158 | h.registry.mount(query); |
| 159 | const read = () => AtomRegistry.getResult(h.registry, query, { suspendOnWaiting: true }); |
| 160 | const original = yield* read(); |
| 161 | const overlay = h.commands.optimisticFile(target); |
| 162 | h.registry.mount(overlay); |
| 163 | const saved = { ...original, contents: "saved" }; |
| 164 | h.registry.set(overlay, { data: saved, saved: true, confirmingQuery: query }); |
| 165 | h.readResponse.wait = Effect.fail( |
| 166 | new ProjectReadFileError({ |
| 167 | cwd: "/repo", |
| 168 | relativePath: "foo", |
| 169 | failure: "operation_failed", |
| 170 | }), |
| 171 | ); |
| 172 | h.registry.refresh(query); |
| 173 | yield* Effect.exit(read()); |
| 174 | expect(h.registry.get(overlay)?.data.contents).toBe("saved"); |
| 175 | |
| 176 | h.readResponse.wait = Effect.void; |
| 177 | h.registry.refresh(query); |
| 178 | const refreshed = yield* read(); |
| 179 | // The production query shares equal file values across this failed confirmation. |
| 180 | expect(refreshed).toBe(original); |
| 181 | expect(h.registry.get(overlay)).toBeNull(); |
| 182 | |
| 183 | h.registry.set(overlay, { data: { ...saved, contents: "newer draft" }, saved: false }); |
| 184 | h.registry.refresh(query); |
| 185 | yield* read(); |
| 186 | expect(h.registry.get(overlay)?.data.contents).toBe("newer draft"); |
| 187 | }), |
| 188 | ); |
| 189 | |
| 190 | it.effect.each([true, false])( |
| 191 | "keeps a saved overlay when an older read uses different allowMissing options (confirmation: %s)", |
| 192 | (allowMissing) => |
| 193 | Effect.gen(function* () { |
| 194 | const h = yield* makeHarness(); |
| 195 | const target = { environmentId: ENVIRONMENT_ID, cwd: "/repo", relativePath: "foo" }; |
| 196 | const query = h.commands.readFile({ |
| 197 | environmentId: ENVIRONMENT_ID, |
| 198 | input: { |
| 199 | cwd: target.cwd, |
| 200 | relativePath: target.relativePath, |
| 201 | ...(allowMissing ? { allowMissing: true } : {}), |
| 202 | }, |
| 203 | }); |
| 204 | const otherQuery = h.commands.readFile({ |
| 205 | environmentId: ENVIRONMENT_ID, |
| 206 | input: { |
| 207 | cwd: target.cwd, |
| 208 | relativePath: target.relativePath, |
| 209 | ...(!allowMissing ? { allowMissing: true } : {}), |
| 210 | }, |
| 211 | }); |
| 212 | const read = (atom: typeof query) => |
| 213 | AtomRegistry.getResult(h.registry, atom, { suspendOnWaiting: true }); |
| 214 | h.registry.mount(query); |
| 215 | const original = yield* read(query); |
| 216 | const overlay = h.commands.optimisticFile(target); |
| 217 | h.registry.mount(overlay); |
| 218 | const started = yield* Deferred.make<void>(); |
| 219 | const release = yield* Deferred.make<void>(); |
| 220 | h.readResponse.wait = Deferred.succeed(started, undefined).pipe( |
| 221 | Effect.andThen(Deferred.await(release)), |
| 222 | ); |
| 223 | h.registry.mount(otherQuery); |
| 224 | const olderRead = yield* read(otherQuery).pipe(Effect.forkChild); |
| 225 | yield* Deferred.await(started); |
| 226 | h.registry.set(overlay, { |
| 227 | data: { ...original, contents: "saved" }, |
| 228 | saved: true, |
| 229 | confirmingQuery: query, |
| 230 | }); |
| 231 | h.readResponse.wait = Effect.fail( |
| 232 | new ProjectReadFileError({ |
| 233 | cwd: target.cwd, |
| 234 | relativePath: target.relativePath, |
| 235 | failure: "operation_failed", |
| 236 | }), |
| 237 | ); |
| 238 | h.registry.refresh(query); |
| 239 | yield* Effect.exit(read(query)); |
| 240 | expect(h.registry.get(overlay)?.data.contents).toBe("saved"); |
| 241 | |
| 242 | yield* Deferred.succeed(release, undefined); |
| 243 | yield* Fiber.join(olderRead); |
| 244 | expect(h.registry.get(overlay)?.data.contents).toBe("saved"); |
| 245 | expect(Option.getOrNull(AsyncResult.value(h.registry.get(query)))).toBe(original); |
| 246 | |
| 247 | h.readResponse.wait = Effect.void; |
| 248 | h.registry.refresh(query); |
| 249 | expect(yield* read(query)).toBe(original); |
| 250 | expect(h.registry.get(overlay)).toBeNull(); |
| 251 | }), |
| 252 | ); |
| 253 | |
| 254 | it.effect("preserves optional read results without seeding metadata for an absent file", () => |
| 255 | Effect.gen(function* () { |
| 256 | const h = yield* makeHarness(); |
| 257 | h.readResponse.missing = true; |
| 258 | const input = { cwd: "/repo", relativePath: "foo", allowMissing: true }; |
| 259 | expect.soft(decodeReadFileInput(input)).toEqual(input); |
| 260 | const result = yield* AtomRegistry.getResult( |
| 261 | h.registry, |
| 262 | h.commands.readFile({ environmentId: ENVIRONMENT_ID, input }), |
| 263 | { suspendOnWaiting: true }, |
| 264 | ); |
| 265 | expect.soft(result).toEqual({ |
| 266 | relativePath: "foo", |
| 267 | byteLength: 0, |
| 268 | contents: "", |
| 269 | truncated: false, |
| 270 | exists: false, |
| 271 | }); |
| 272 | const metadata = h.commands.fileMetadata({ |
| 273 | environmentId: ENVIRONMENT_ID, |
| 274 | input: { path: "/repo/foo" }, |
| 275 | }); |
| 276 | expect( |
| 277 | yield* AtomRegistry.getResult(h.registry, metadata, { suspendOnWaiting: true }), |
| 278 | ).toBeNull(); |
| 279 | expect(h.metadataRequests).toEqual([["/repo/foo"]]); |
| 280 | }), |
| 281 | ); |
| 282 | |
| 283 | it.effect("does not restore old metadata from a read completed after a write", () => |
| 284 | Effect.gen(function* () { |
| 285 | const h = yield* makeHarness(); |
| 286 | const threadId = ThreadId.make("stale-read-thread"); |
| 287 | h.registry.mount(h.commands.fileMetadataThread({ environmentId: ENVIRONMENT_ID, threadId })); |
| 288 | const metadata = h.commands.fileMetadata({ |
| 289 | environmentId: ENVIRONMENT_ID, |
| 290 | input: { path: "/repo/foo", threadId }, |
| 291 | }); |
| 292 | h.registry.mount(metadata); |
| 293 | const readMetadata = () => |
| 294 | AtomRegistry.getResult(h.registry, metadata, { suspendOnWaiting: true }); |
| 295 | yield* readMetadata(); |
| 296 | const started = yield* Deferred.make<void>(); |
| 297 | const release = yield* Deferred.make<void>(); |
| 298 | h.readResponse.wait = Deferred.succeed(started, undefined).pipe( |
| 299 | Effect.andThen(Deferred.await(release)), |
| 300 | ); |
| 301 | const fileRead = yield* AtomRegistry.getResult( |
| 302 | h.registry, |
| 303 | h.commands.readFile({ |
| 304 | environmentId: ENVIRONMENT_ID, |
| 305 | input: { cwd: "/repo", relativePath: "foo" }, |
| 306 | }), |
| 307 | { suspendOnWaiting: true }, |
| 308 | ).pipe(Effect.forkChild); |
| 309 | yield* Deferred.await(started); |
| 310 | const write = yield* Effect.exit( |
| 311 | h.commands.writeFile.run(h.registry, { |
| 312 | environmentId: ENVIRONMENT_ID, |
| 313 | input: { cwd: "/repo", relativePath: "foo", contents: "new" }, |
| 314 | }), |
| 315 | ); |
| 316 | expect(write._tag).toBe("Success"); |
| 317 | expect(yield* readMetadata()).toEqual({ kind: "file", byteLength: 10 }); |
| 318 | yield* Deferred.succeed(release, undefined); |
| 319 | expect((yield* Fiber.join(fileRead)).byteLength).toBe(5); |
| 320 | h.registry.refresh(metadata); |
| 321 | expect(yield* readMetadata()).toEqual({ kind: "file", byteLength: 10 }); |
| 322 | expect(h.metadataRequests).toHaveLength(2); |
| 323 | }), |
| 324 | ); |
| 325 | |
| 326 | it.effect("refreshes retained canonical and original paths after an aliased write", () => |
| 327 | Effect.gen(function* () { |
| 328 | const h = yield* makeHarness(); |
| 329 | const threadId = ThreadId.make("write-metadata-thread"); |
| 330 | h.registry.mount(h.commands.fileMetadataThread({ environmentId: ENVIRONMENT_ID, threadId })); |
| 331 | const atoms = ["/repo/foo", "/repo/./foo"].map((path) => |
| 332 | h.commands.fileMetadata({ environmentId: ENVIRONMENT_ID, input: { path, threadId } }), |
| 333 | ); |
| 334 | for (const atom of atoms) h.registry.mount(atom); |
| 335 | const read = () => |
| 336 | Effect.forEach( |
| 337 | atoms, |
| 338 | (atom) => AtomRegistry.getResult(h.registry, atom, { suspendOnWaiting: true }), |
| 339 | { concurrency: "unbounded" }, |
| 340 | ); |
| 341 | expect(yield* read()).toEqual([ |
| 342 | { kind: "file", byteLength: 5 }, |
| 343 | { kind: "file", byteLength: 5 }, |
| 344 | ]); |
| 345 | const result = yield* Effect.exit( |
| 346 | h.commands.writeFile.run(h.registry, { |
| 347 | environmentId: ENVIRONMENT_ID, |
| 348 | input: { cwd: "/repo", relativePath: "./foo", contents: "new" }, |
| 349 | }), |
| 350 | ); |
| 351 | expect(result).toMatchObject({ _tag: "Success", value: { relativePath: "foo" } }); |
| 352 | expect(yield* read()).toEqual([ |
| 353 | { kind: "file", byteLength: 10 }, |
| 354 | { kind: "file", byteLength: 10 }, |
| 355 | ]); |
| 356 | expect(h.metadataRequests.flat()).toEqual([ |
| 357 | "/repo/foo", |
| 358 | "/repo/./foo", |
| 359 | "/repo/foo", |
| 360 | "/repo/./foo", |
| 361 | ]); |
| 362 | }), |
| 363 | ); |
| 364 | }); |
| 365 | |
| 366 | describe("openScratch", () => { |
| 367 | it.effect("resolves once the created project reaches the client store", () => |
| 368 | Effect.gen(function* () { |
| 369 | const { registry, storedProject, openScratch } = yield* makeHarness(); |
| 370 | const opening = yield* Effect.forkChild(openScratch); |
| 371 | yield* Effect.yieldNow; |
| 372 | registry.set(storedProject, PROJECT); |
| 373 | const result = yield* Fiber.join(opening); |
| 374 | expect(result).toMatchObject({ _tag: "Success", value: PROJECT }); |
| 375 | }), |
| 376 | ); |
| 377 | }); |