@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 77 lines 2.98 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { makeObjectMedia } from "@alchemy.run/gcp/Storage/ObjectMedia";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as scheduler from "@distilled.cloud/gcp/cloudscheduler_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import ScheduleService, { Markers, markerFor, SCHEDULE_BODY } from "./fixtures/schedule-service.ts";
10
11 const { test } = Test.make({ providers: GCP.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 test.provider(
16 "a Cloud Scheduler job invokes an effect-native Function",
17 (stack) =>
18 Effect.gen(function* () {
19 yield* stack.destroy();
20
21 const out = yield* stack.deploy(
22 Effect.gen(function* () {
23 const service = yield* ScheduleService;
24 const bucket = yield* Markers;
25 return {
26 uri: service.uri,
27 project: service.project,
28 bucket: bucket.bucketName,
29 };
30 }),
31 );
32
33 // The job POSTs to the service with an OIDC token.
34 const { jobs = [] } = yield* scheduler.listProjectsLocationsJobs({
35 parent: `projects/${out.project}/locations/us-central1`,
36 pageSize: 500,
37 });
38 const job = jobs.find((candidate) =>
39 candidate.httpTarget?.uri?.startsWith(`${out.uri}/__alchemy/scheduler/`),
40 );
41 expect(job).toBeDefined();
42 expect(job!.httpTarget?.httpMethod).toEqual("POST");
43 expect(job!.httpTarget?.oidcToken?.serviceAccountEmail).toBeDefined();
44 expect(job!.httpTarget?.oidcToken?.audience).toEqual(
45 `${out.uri}/__alchemy/scheduler/heartbeat`,
46 );
47
48 // Force one run instead of waiting for the (yearly) cron. The job's
49 // default retry policy redelivers until the host accepts it.
50 const jobName = job!.name!.split("/").pop()!;
51 const media = yield* makeObjectMedia;
52 yield* scheduler.runProjectsLocationsJobs({ name: job!.name!, body: {} });
53 const marker = yield* media.download({ bucket: out.bucket, object: markerFor(jobName) }).pipe(
54 Effect.retry({
55 while: (error) => error._tag === "GCP.Storage.ObjectNotFound",
56 schedule: Schedule.spaced("5 seconds"),
57 times: 48,
58 }),
59 );
60 const event = JSON.parse(new TextDecoder().decode(marker.body));
61 expect(event.jobName).toEqual(jobName);
62 expect(event.body).toEqual(SCHEDULE_BODY);
63 expect(event.scheduleTime).toMatch(/^\d{4}-\d{2}-\d{2}T/);
64
65 yield* stack.destroy();
66
67 const gone = yield* scheduler.getProjectsLocationsJobs({ name: job!.name! }).pipe(
68 Effect.as("found" as const),
69 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
70 );
71 expect(gone).toEqual("gone");
72 }).pipe(logLevel),
73 {
74 tags: ["provider:gcp", "provider:gcp:cloudscheduler", "live"],
75 timeout: 600_000,
76 },
77 );