| 1 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 2 | import * as GCP from "@/GCP"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as ces from "@distilled.cloud/gcp/ces_v1"; |
| 5 | import { expect } from "alchemy-test"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService( |
| 13 | MinimumLogLevel, |
| 14 | process.env.DEBUG ? "Debug" : "Info", |
| 15 | ); |
| 16 | |
| 17 | const waitUntilGone = (name: string) => |
| 18 | ces.getProjectsLocationsAppsExamples({ name }).pipe( |
| 19 | Effect.as("found" as const), |
| 20 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.spaced("1 second"), |
| 23 | until: (status) => status === "gone", |
| 24 | times: 10, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | test.provider( |
| 29 | "getProjectsLocationsAppsExamples on a missing example fails with a typed tag", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | const { project } = yield* GcpEnvironment.current; |
| 33 | |
| 34 | yield* stack.destroy(); |
| 35 | |
| 36 | const error = yield* Effect.flip( |
| 37 | ces.getProjectsLocationsAppsExamples({ |
| 38 | name: `projects/${project}/locations/us/apps/missing/examples/missing`, |
| 39 | }), |
| 40 | ); |
| 41 | expect(error._tag).toEqual("NotFound"); |
| 42 | |
| 43 | yield* stack.destroy(); |
| 44 | }).pipe(logLevel), |
| 45 | { tags: ["provider:gcp", "provider:gcp:ces", "live"], timeout: 300_000 }, |
| 46 | ); |
| 47 | |
| 48 | test.provider( |
| 49 | "create, update, and delete an example", |
| 50 | (stack) => |
| 51 | Effect.gen(function* () { |
| 52 | yield* stack.destroy(); |
| 53 | |
| 54 | const created = yield* stack.deploy( |
| 55 | Effect.gen(function* () { |
| 56 | const app = yield* GCP.CES.App("Support", { |
| 57 | location: "us", |
| 58 | displayName: "support-example", |
| 59 | }); |
| 60 | const example = yield* GCP.CES.AppsExample("Hello", { |
| 61 | app: app.name, |
| 62 | displayName: "hello", |
| 63 | description: "greeting", |
| 64 | messages: [ |
| 65 | { role: "user", chunks: [{ text: "Hi" }] }, |
| 66 | { role: "agent", chunks: [{ text: "Hello!" }] }, |
| 67 | ], |
| 68 | }); |
| 69 | return { app, example }; |
| 70 | }), |
| 71 | ); |
| 72 | |
| 73 | expect(created.example.name).toContain("/examples/"); |
| 74 | expect(created.example.app).toEqual(created.app.name); |
| 75 | expect(created.example.displayName).toEqual("hello"); |
| 76 | expect(created.example.description).toEqual("greeting"); |
| 77 | expect(created.example.messages?.length).toEqual(2); |
| 78 | |
| 79 | const fetched = yield* ces.getProjectsLocationsAppsExamples({ |
| 80 | name: created.example.name, |
| 81 | }); |
| 82 | expect(fetched.name).toEqual(created.example.name); |
| 83 | expect(fetched.description).toContain("alchemy-id="); |
| 84 | |
| 85 | const updated = yield* stack.deploy( |
| 86 | Effect.gen(function* () { |
| 87 | const app = yield* GCP.CES.App("Support", { |
| 88 | appId: created.app.appId, |
| 89 | location: created.app.location, |
| 90 | displayName: "support-example", |
| 91 | }); |
| 92 | const example = yield* GCP.CES.AppsExample("Hello", { |
| 93 | app: app.name, |
| 94 | exampleId: created.example.exampleId, |
| 95 | displayName: "hello", |
| 96 | description: "greeting with help", |
| 97 | messages: [ |
| 98 | { role: "user", chunks: [{ text: "Hi" }] }, |
| 99 | { |
| 100 | role: "agent", |
| 101 | chunks: [{ text: "Hello! How can I help?" }], |
| 102 | }, |
| 103 | ], |
| 104 | }); |
| 105 | return { app, example }; |
| 106 | }), |
| 107 | ); |
| 108 | |
| 109 | expect(updated.example.name).toEqual(created.example.name); |
| 110 | expect(updated.example.description).toEqual("greeting with help"); |
| 111 | |
| 112 | yield* stack.destroy(); |
| 113 | const gone = yield* waitUntilGone(created.example.name); |
| 114 | expect(gone).toEqual("gone"); |
| 115 | }).pipe(logLevel), |
| 116 | { tags: ["provider:gcp", "provider:gcp:ces", "live"], timeout: 300_000 }, |
| 117 | ); |