Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

projectCommands.test.ts

77 lines3.1 KB

1import { describe, expect, it } from "@effect/vitest";
2import { EnvironmentId, ProjectId, type ScopedProjectRef, WS_METHODS } from "@t3tools/contracts";
3import * as Crypto from "effect/Crypto";
4import * as Effect from "effect/Effect";
5import * as Fiber from "effect/Fiber";
6import * as Layer from "effect/Layer";
7import * as Option from "effect/Option";
8import * as SubscriptionRef from "effect/SubscriptionRef";
9import { Atom, AtomRegistry } from "effect/reactivity";
10​
11import * as EnvironmentRegistry from "../connection/registry.ts";
12import * as EnvironmentSupervisor from "../connection/supervisor.ts";
13import type { RpcSession } from "../rpc/session.ts";
14import type { EnvironmentProject } from "./models.ts";
15import { createProjectEnvironmentAtoms } from "./projectCommands.ts";
16​
17const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
18const PROJECT_ID = ProjectId.make("scratch");
19const PROJECT = {
20 id: PROJECT_ID,
21 environmentId: ENVIRONMENT_ID,
22 workspaceRoot: "/scratch",
23} as EnvironmentProject;
24​
25const makeHarness = Effect.fn("TestProjectCommands.makeHarness")(function* () {
26 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
27 target: { environmentId: ENVIRONMENT_ID },
28 session: yield* SubscriptionRef.make(
29 Option.some({
30 client: {
31 [WS_METHODS.projectsEnsureScratch]: () => Effect.succeed({ projectId: PROJECT_ID }),
32 },
33 } as unknown as RpcSession),
34 ),
35 } as EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
36 const runtime = Atom.runtime(
37 Layer.mergeAll(
38 Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, {
39 run: (_environmentId, effect) =>
40 Effect.provideService(effect, EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
41 } as EnvironmentRegistry.EnvironmentRegistry["Service"]),
42 Layer.succeed(
43 Crypto.Crypto,
44 Crypto.make({
45 randomBytes: (size) => new Uint8Array(size),
46 digest: (_algorithm, data) => Effect.succeed(data),
47 }),
48 ),
49 ),
50 );
51 const storedProject = Atom.make<EnvironmentProject | null>(null);
52 const commands = createProjectEnvironmentAtoms(runtime, {
53 projectAtom: (ref: ScopedProjectRef) =>
54 ref.environmentId === ENVIRONMENT_ID && ref.projectId === PROJECT_ID
55 ? storedProject
56 : Atom.make(null),
57 });
58 const registry = AtomRegistry.make();
59 yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose()));
60 const openScratch = Effect.promise(() =>
61 commands.openScratch.run(registry, { environmentId: ENVIRONMENT_ID, input: {} }),
62 );
63 return { registry, storedProject, openScratch };
64});
65​
66describe("openScratch", () => {
67 it.effect("resolves once the created project reaches the client store", () =>
68 Effect.gen(function* () {
69 const { registry, storedProject, openScratch } = yield* makeHarness();
70 const opening = yield* Effect.forkChild(openScratch);
71 yield* Effect.yieldNow;
72 registry.set(storedProject, PROJECT);
73 const result = yield* Fiber.join(opening);
74 expect(result).toMatchObject({ _tag: "Success", value: PROJECT });
75 }).pipe(Effect.scoped),
76 );
77});