@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 06:00 UTC

main
ts 177 lines 6.31 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as workflowexecutions from "@distilled.cloud/gcp/workflowexecutions_v1";
5 import * as workflows from "@distilled.cloud/gcp/workflows_v1";
6 import type { StackServices } from "alchemy/Stack";
7 import * as Effect from "effect/Effect";
8 import * as Layer from "effect/Layer";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({
13 providers: GCP.providers() as Layer.Layer<GCP.ProviderRequirements, never, StackServices>,
14 });
15
16 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
17
18 const SOURCE_V1 = `main:
19 steps:
20 - done:
21 return: hello
22 `;
23
24 const SOURCE_V2 = `main:
25 steps:
26 - done:
27 return: world
28 `;
29
30 const waitUntilGone = (name: string) =>
31 workflows.getProjectsLocationsWorkflows({ name }).pipe(
32 Effect.as("found" as const),
33 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
34 Effect.repeat({
35 schedule: Schedule.spaced("1 second"),
36 until: (status) => status === "gone",
37 times: 10,
38 }),
39 );
40
41 const waitUntilSucceeded = (name: string) =>
42 workflowexecutions.getProjectsLocationsWorkflowsExecutions({ name }).pipe(
43 Effect.repeat({
44 schedule: Schedule.spaced("1 second"),
45 until: (execution) =>
46 execution.state === "SUCCEEDED" ||
47 execution.state === "FAILED" ||
48 execution.state === "CANCELLED",
49 times: 10,
50 }),
51 );
52
53 test.provider(
54 "create, update, replace, and delete a workflow",
55 (stack) =>
56 Effect.gen(function* () {
57 yield* stack.destroy();
58
59 const created = yield* stack.deploy(
60 Effect.gen(function* () {
61 return yield* GCP.Workflows.Workflow("Greet", {
62 location: "us-central1",
63 description: "greet",
64 labels: { env: "test" },
65 sourceContents: SOURCE_V1,
66 });
67 }),
68 );
69
70 expect(created.workflowId).toEqual(expect.any(String));
71 expect(created.name).toContain("/workflows/");
72 expect(created.location).toEqual("us-central1");
73 expect(created.description).toEqual("greet");
74 expect(created.labels).toMatchObject({ env: "test" });
75 expect(created.state).toEqual("ACTIVE");
76 expect(created.sourceContents).toContain("hello");
77 expect(created.revisionId).toEqual(expect.any(String));
78
79 const fetched = yield* workflows.getProjectsLocationsWorkflows({
80 name: created.name,
81 });
82 expect(fetched.name).toEqual(created.name);
83 expect(fetched.description).toEqual("greet");
84 expect(fetched.labels?.env).toEqual("test");
85 expect(fetched.state).toEqual("ACTIVE");
86 expect(fetched.sourceContents).toContain("hello");
87 expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String));
88
89 const listed = yield* workflows.listProjectsLocationsWorkflows({
90 parent: `projects/${created.project}/locations/${created.location}`,
91 });
92 expect((listed.workflows ?? []).some((workflow) => workflow.name === created.name)).toEqual(
93 true,
94 );
95
96 const execution = yield* workflowexecutions.createProjectsLocationsWorkflowsExecutions({
97 parent: created.name,
98 body: {},
99 });
100 expect(execution.name).toEqual(expect.any(String));
101 const finished = yield* waitUntilSucceeded(execution.name!);
102 expect(finished.state).toEqual("SUCCEEDED");
103 expect(finished.result).toContain("hello");
104
105 const updated = yield* stack.deploy(
106 Effect.gen(function* () {
107 return yield* GCP.Workflows.Workflow("Greet", {
108 workflowId: created.workflowId,
109 location: "us-central1",
110 description: "greet v2",
111 labels: { env: "prod", role: "greeter" },
112 callLogLevel: "LOG_ERRORS_ONLY",
113 userEnvVars: { greeting: "world" },
114 sourceContents: SOURCE_V2,
115 });
116 }),
117 );
118
119 expect(updated.name).toEqual(created.name);
120 expect(updated.workflowId).toEqual(created.workflowId);
121 expect(updated.description).toEqual("greet v2");
122 expect(updated.labels).toMatchObject({ env: "prod", role: "greeter" });
123 expect(updated.callLogLevel).toEqual("LOG_ERRORS_ONLY");
124 expect(updated.userEnvVars).toMatchObject({ greeting: "world" });
125 expect(updated.sourceContents).toContain("world");
126 expect(updated.revisionId).not.toEqual(created.revisionId);
127 expect(updated.state).toEqual("ACTIVE");
128
129 const fetchedUpdate = yield* workflows.getProjectsLocationsWorkflows({
130 name: updated.name,
131 });
132 expect(fetchedUpdate.description).toEqual("greet v2");
133 expect(fetchedUpdate.labels?.env).toEqual("prod");
134 expect(fetchedUpdate.labels?.role).toEqual("greeter");
135 expect(fetchedUpdate.callLogLevel).toEqual("LOG_ERRORS_ONLY");
136 expect(fetchedUpdate.userEnvVars?.greeting).toEqual("world");
137 expect(fetchedUpdate.sourceContents).toContain("world");
138 expect(fetchedUpdate.state).toEqual("ACTIVE");
139
140 const replaced = yield* stack.deploy(
141 Effect.gen(function* () {
142 return yield* GCP.Workflows.Workflow("Greet", {
143 workflowId: created.workflowId,
144 location: "us-east1",
145 description: "greet east",
146 labels: { env: "test" },
147 sourceContents: SOURCE_V1,
148 });
149 }),
150 );
151
152 expect(replaced.workflowId).toEqual(created.workflowId);
153 expect(replaced.location).toEqual("us-east1");
154 expect(replaced.name).toContain("/locations/us-east1/");
155 expect(replaced.name).not.toEqual(created.name);
156 expect(replaced.state).toEqual("ACTIVE");
157
158 const oldGone = yield* waitUntilGone(created.name);
159 expect(oldGone).toEqual("gone");
160
161 const fetchedReplace = yield* workflows.getProjectsLocationsWorkflows({
162 name: replaced.name,
163 });
164 expect(fetchedReplace.name).toEqual(replaced.name);
165 expect(fetchedReplace.state).toEqual("ACTIVE");
166 expect(fetchedReplace.sourceContents).toContain("hello");
167
168 yield* stack.destroy();
169
170 const gone = yield* waitUntilGone(replaced.name);
171 expect(gone).toEqual("gone");
172 }).pipe(logLevel),
173 {
174 tags: ["provider:gcp", "provider:gcp:workflows", "live"],
175 timeout: 120_000,
176 },
177 );