1import * as GCP from "@/GCP";
2import { GcpEnvironment } from "@/GCP/Environment";
3import * as Test from "@/Test/Alchemy";
4import * as aiplatform from "@distilled.cloud/gcp/aiplatform_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 waitUntilGone = (name: string) =>
18 aiplatform.getProjectsLocationsCachedContents({ name }).pipe(
19 Effect.as("found" as const),
20 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("2 seconds"),
23 until: (status) => status === "gone",
24 times: 10,
25 }),
26 );
27
28test.provider(
29 "getProjectsLocationsCachedContents on a missing cache fails with a typed tag",
30 (stack) =>
31 Effect.gen(function* () {
32 const { project } = yield* GcpEnvironment.current;
33 const parent = `projects/${project}/locations/us-central1`;
34 yield* stack.destroy();
35
36 const error = yield* Effect.flip(
37 aiplatform.getProjectsLocationsCachedContents({
38 name: `${parent}/cachedContents/alchemy-aiplatform-missing`,
39 }),
40 );
41 expect(error._tag).toEqual("NotFound");
42 const page = yield* aiplatform.listProjectsLocationsCachedContents({
43 parent,
44 pageSize: 10,
45 });
46 expect(
47 (page.cachedContents ?? []).map((item) => item.name),
48 ).not.toContain(`${parent}/cachedContents/alchemy-aiplatform-missing`);
49
50 yield* stack.destroy();
51 }).pipe(logLevel),
52 {
53 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
54 timeout: 90_000,
55 },
56);
57
58// Explicit caching needs at least 1024 tokens of content.
59const styleGuide = Array.from(
60 { length: 400 },
61 (_, i) =>
62 `Rule ${i + 1}: answer question ${i + 1} in at most ${i + 2} words.`,
63).join("\n");
64
65test.provider(
66 "create, update, and delete cached content",
67 (stack) =>
68 Effect.gen(function* () {
69 const { project } = yield* GcpEnvironment.current;
70 const parent = `projects/${project}/locations/us-central1`;
71 const model = `${parent}/publishers/google/models/gemini-2.5-flash`;
72 yield* stack.destroy();
73
74 const created = yield* stack.deploy(
75 Effect.gen(function* () {
76 return yield* GCP.AIPlatform.CachedContent("Style", {
77 location: "us-central1",
78 model,
79 ttl: "3600s",
80 displayName: "style-cache",
81 contents: [
82 {
83 role: "user",
84 parts: [{ text: styleGuide }],
85 },
86 ],
87 });
88 }),
89 );
90
91 expect(created.name).toContain("/cachedContents/");
92 expect(created.model).toContain("gemini");
93 expect(created.displayName).toEqual("style-cache");
94
95 const fetched = yield* aiplatform.getProjectsLocationsCachedContents({
96 name: created.name,
97 });
98 expect(fetched.name).toEqual(created.name);
99
100 const updated = yield* stack.deploy(
101 Effect.gen(function* () {
102 return yield* GCP.AIPlatform.CachedContent("Style", {
103 location: "us-central1",
104 model,
105 ttl: "7200s",
106 displayName: "style-cache",
107 contents: [
108 {
109 role: "user",
110 parts: [{ text: styleGuide }],
111 },
112 ],
113 });
114 }),
115 );
116
117 expect(updated.name).toEqual(created.name);
118
119 yield* stack.destroy();
120
121 const gone = yield* waitUntilGone(created.name);
122 expect(gone).toEqual("gone");
123 }).pipe(logLevel),
124 {
125 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
126 timeout: 300_000,
127 },
128);