Open History to see the last commit
1import {
2 EnvironmentId,
3 WS_METHODS,
4 type SourceControlPublishRepositoryResult,
5} from "@t3tools/contracts";
6import { describe, expect, it } from "@effect/vitest";
7import * as Effect from "effect/Effect";
8import * as Layer from "effect/Layer";
9import * as Option from "effect/Option";
10import * as SubscriptionRef from "effect/SubscriptionRef";
11import { AsyncResult, Atom, AtomRegistry } from "effect/unstable/reactivity";
12
13import {
14 AVAILABLE_CONNECTION_STATE,
15 PrimaryConnectionTarget,
16 type PreparedConnection,
17 type SupervisorConnectionState,
18} from "../connection/model.ts";
19import * as EnvironmentRegistry from "../connection/registry.ts";
20import * as EnvironmentSupervisor from "../connection/supervisor.ts";
21import * as Persistence from "../platform/persistence.ts";
22import { EnvironmentRpcUnavailableError } from "../rpc/client.ts";
23import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
24import type { RpcSession } from "../rpc/session.ts";
25import { createSourceControlEnvironmentAtoms } from "./sourceControl.ts";
26import { vcsRefsCacheStateAtom } from "./vcsRefInvalidation.ts";
27
28const TARGET = new PrimaryConnectionTarget({
29 environmentId: EnvironmentId.make("environment-1"),
30 label: "Test environment",
31 httpBaseUrl: "https://environment.example.test",
32 wsBaseUrl: "wss://environment.example.test",
33});
34
35const PUBLISH_RESULT: SourceControlPublishRepositoryResult = {
36 repository: {
37 provider: "github",
38 nameWithOwner: "t3tools/t3code",
39 url: "https://github.com/t3tools/t3code",
40 sshUrl: "git@github.com:t3tools/t3code.git",
41 },
42 remoteName: "origin",
43 remoteUrl: "git@github.com:t3tools/t3code.git",
44 branch: "main",
45 upstreamBranch: "origin/main",
46 status: "pushed",
47};
48
49function session(client: WsRpcProtocolClient): RpcSession {
50 return {
51 client,
52 initialConfig: Effect.never,
53 ready: Effect.void,
54 probe: Effect.void,
55 closed: Effect.never,
56 };
57}
58
59describe("source control environment atoms", () => {
60 it.effect("invalidates cached refs after successful and failed publishing", () =>
61 Effect.scoped(
62 Effect.gen(function* () {
63 const connectionState: SupervisorConnectionState = {
64 ...AVAILABLE_CONNECTION_STATE,
65 desired: true,
66 network: "online",
67 phase: "connected",
68 attempt: 1,
69 generation: 1,
70 };
71 let publishAttempts = 0;
72 const client = {
73 [WS_METHODS.sourceControlPublishRepository]: () => {
74 publishAttempts += 1;
75 return publishAttempts === 1
76 ? Effect.succeed(PUBLISH_RESULT)
77 : Effect.fail(
78 new EnvironmentRpcUnavailableError({
79 environmentId: TARGET.environmentId,
80 message: "push failed after adding the remote",
81 }),
82 );
83 },
84 } as unknown as WsRpcProtocolClient;
85 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
86 target: TARGET,
87 state: yield* SubscriptionRef.make(connectionState),
88 session: yield* SubscriptionRef.make(Option.some(session(client))),
89 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
90 connect: Effect.void,
91 disconnect: Effect.void,
92 retryNow: Effect.void,
93 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
94 const run: EnvironmentRegistry.EnvironmentRegistry["Service"]["run"] = (
95 _environmentId,
96 effect,
97 ) => Effect.provideService(effect, EnvironmentSupervisor.EnvironmentSupervisor, supervisor);
98 const environmentRegistry = EnvironmentRegistry.EnvironmentRegistry.of({
99 run,
100 } as unknown as EnvironmentRegistry.EnvironmentRegistry["Service"]);
101 const removed = new Array<string>();
102 const cache = Persistence.EnvironmentCacheStore.of({
103 loadShell: () => Effect.succeed(Option.none()),
104 saveShell: () => Effect.void,
105 loadThread: () => Effect.succeed(Option.none()),
106 saveThread: () => Effect.void,
107 removeThread: () => Effect.void,
108 loadServerConfig: () => Effect.succeed(Option.none()),
109 saveServerConfig: () => Effect.void,
110 loadVcsRefs: () => Effect.succeed(Option.none()),
111 saveVcsRefs: () => Effect.void,
112 removeVcsRefs: (environmentId, cwd) =>
113 Effect.sync(() => {
114 removed.push(`${environmentId}:${cwd}`);
115 }),
116 clearVcsRefs: (environmentId) =>
117 Effect.sync(() => {
118 removed.push(`${environmentId}:*`);
119 }),
120 clear: () => Effect.void,
121 });
122 const runtime = Atom.runtime(
123 Layer.merge(
124 Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, environmentRegistry),
125 Layer.succeed(Persistence.EnvironmentCacheStore, cache),
126 ),
127 );
128 const atoms = createSourceControlEnvironmentAtoms(runtime);
129 const registry = yield* Effect.acquireRelease(Effect.sync(AtomRegistry.make), (registry) =>
130 Effect.sync(() => registry.dispose()),
131 );
132 const state = vcsRefsCacheStateAtom({ environmentId: TARGET.environmentId });
133
134 expect(registry.get(state).revision).toBe(0);
135 const publishResult = yield* Effect.promise(() =>
136 atoms.publishRepository.run(registry, {
137 environmentId: TARGET.environmentId,
138 input: {
139 cwd: "/repo",
140 provider: "github",
141 repository: "t3tools/t3code",
142 visibility: "private",
143 },
144 }),
145 );
146
147 expect(AsyncResult.isSuccess(publishResult)).toBe(true);
148 expect(registry.get(state).revision).toBe(1);
149 expect(removed).toEqual([`${TARGET.environmentId}:*`]);
150
151 const failedPublish = yield* Effect.promise(() =>
152 atoms.publishRepository.run(registry, {
153 environmentId: TARGET.environmentId,
154 input: {
155 cwd: "/repo",
156 provider: "github",
157 repository: "t3tools/t3code",
158 visibility: "private",
159 },
160 }),
161 );
162
163 expect(AsyncResult.isFailure(failedPublish)).toBe(true);
164 expect(registry.get(state).revision).toBe(2);
165 expect(removed).toEqual([`${TARGET.environmentId}:*`, `${TARGET.environmentId}:*`]);
166 }),
167 ),
168 );
169});