1import { GcpEnvironment } from "@/GCP/Environment";
2import * as GCP from "@/GCP";
3import * as Test from "@/Test/Alchemy";
4import * as ces from "@distilled.cloud/gcp/ces_v1";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import { MinimumLogLevel } from "effect/References";
8import * as Schedule from "effect/Schedule";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17const pingSchema = JSON.stringify({
18 openapi: "3.0.0",
19 info: { title: "Ping", version: "1.0.0" },
20 servers: [{ url: "https://example.com" }],
21 paths: {
22 "/ping": {
23 get: {
24 operationId: "ping",
25 responses: { "200": { description: "ok" } },
26 },
27 },
28 },
29});
30
31const waitUntilGone = (name: string) =>
32 ces.getProjectsLocationsAppsToolsets({ name }).pipe(
33 Effect.as("found" as const),
34 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
35 Effect.repeat({
36 schedule: Schedule.spaced("1 second"),
37 until: (status) => status === "gone",
38 times: 10,
39 }),
40 );
41
42test.provider(
43 "getProjectsLocationsAppsToolsets on a missing toolset fails with a typed tag",
44 (stack) =>
45 Effect.gen(function* () {
46 const { project } = yield* GcpEnvironment.current;
47
48 yield* stack.destroy();
49
50 const error = yield* Effect.flip(
51 ces.getProjectsLocationsAppsToolsets({
52 name: `projects/${project}/locations/us/apps/missing/toolsets/missing`,
53 }),
54 );
55 expect(error._tag).toEqual("NotFound");
56
57 yield* stack.destroy();
58 }).pipe(logLevel),
59 { tags: ["provider:gcp", "provider:gcp:ces", "live"], timeout: 300_000 },
60);
61
62test.provider(
63 "create, update, and delete a toolset",
64 (stack) =>
65 Effect.gen(function* () {
66 yield* stack.destroy();
67
68 const created = yield* stack.deploy(
69 Effect.gen(function* () {
70 const app = yield* GCP.CES.App("Support", {
71 location: "us",
72 displayName: "support-toolset",
73 });
74 const toolset = yield* GCP.CES.AppsToolset("Ping", {
75 app: app.name,
76 displayName: "ping",
77 description: "ping endpoints",
78 openApiToolset: { openApiSchema: pingSchema },
79 });
80 return { app, toolset };
81 }),
82 );
83
84 expect(created.toolset.name).toContain("/toolsets/");
85 expect(created.toolset.app).toEqual(created.app.name);
86 expect(created.toolset.displayName).toEqual("ping");
87 expect(created.toolset.description).toEqual("ping endpoints");
88 expect(created.toolset.openApiToolset?.openApiSchema).toBeDefined();
89
90 const fetched = yield* ces.getProjectsLocationsAppsToolsets({
91 name: created.toolset.name,
92 });
93 expect(fetched.name).toEqual(created.toolset.name);
94 // The description reaches the model: no ownership marker.
95 expect(fetched.description).toEqual("ping endpoints");
96
97 const updated = yield* stack.deploy(
98 Effect.gen(function* () {
99 const app = yield* GCP.CES.App("Support", {
100 appId: created.app.appId,
101 location: created.app.location,
102 displayName: "support-toolset",
103 });
104 const toolset = yield* GCP.CES.AppsToolset("Ping", {
105 app: app.name,
106 toolsetId: created.toolset.toolsetId,
107 displayName: "ping",
108 description: "ping and health",
109 openApiToolset: { openApiSchema: pingSchema },
110 });
111 return { app, toolset };
112 }),
113 );
114
115 expect(updated.toolset.name).toEqual(created.toolset.name);
116 expect(updated.toolset.description).toEqual("ping and health");
117
118 yield* stack.destroy();
119 const gone = yield* waitUntilGone(created.toolset.name);
120 expect(gone).toEqual("gone");
121 }).pipe(logLevel),
122 { tags: ["provider:gcp", "provider:gcp:ces", "live"], timeout: 300_000 },
123);