1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as speech from "@distilled.cloud/gcp/speech_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import * as Schedule from "effect/Schedule";
7import { location, logLevel, currentParent, currentProject } from "./common.ts";
8
9const { test } = Test.make({ providers: GCP.providers() });
10
11const waitUntilGone = (name: string) =>
12 speech.getProjectsLocationsCustomClasses({ name }).pipe(
13 Effect.as("found" as const),
14 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
15 Effect.repeat({
16 schedule: Schedule.spaced("1 second"),
17 until: (status) => status === "gone",
18 times: 10,
19 }),
20 );
21
22test.provider(
23 "getProjectsLocationsCustomClasses on a missing class fails with a typed tag",
24 (stack) =>
25 Effect.gen(function* () {
26 const parent = yield* currentParent;
27 yield* stack.destroy();
28
29 const error = yield* Effect.flip(
30 speech.getProjectsLocationsCustomClasses({
31 name: `${parent}/customClasses/alchemy-missing-class`,
32 }),
33 );
34 expect(error._tag).toEqual("NotFound");
35
36 yield* stack.destroy();
37 }).pipe(logLevel),
38 { tags: ["provider:gcp", "provider:gcp:speech", "live"], timeout: 90_000 },
39);
40
41test.provider(
42 "create, update, and delete a custom class",
43 (stack) =>
44 Effect.gen(function* () {
45 const project = yield* currentProject;
46 yield* stack.destroy();
47
48 const created = yield* stack.deploy(
49 Effect.gen(function* () {
50 return yield* GCP.Speech.CustomClass("Ships", {
51 location,
52 items: [{ value: "sloop" }, { value: "ketch" }],
53 });
54 }),
55 );
56
57 expect(created.name).toMatch(
58 new RegExp(`^projects/[^/]+/locations/${location}/customClasses/`),
59 );
60 expect(created.customClassId.length).toBeGreaterThanOrEqual(4);
61 expect(created.project).toEqual(project);
62 expect(created.location).toEqual(location);
63 expect(created.items.map((item) => item.value).sort()).toEqual([
64 "ketch",
65 "sloop",
66 ]);
67
68 const fetched = yield* speech.getProjectsLocationsCustomClasses({
69 name: created.name,
70 });
71 expect(fetched.name).toEqual(created.name);
72 const fetchedValues = (fetched.items ?? [])
73 .map((item) => item.value ?? "")
74 .filter((value) => value.length > 0);
75 expect(fetchedValues).toContain("sloop");
76 expect(fetchedValues).toContain("ketch");
77 // Items steer recognition: Alchemy adds no ownership item.
78 expect(fetchedValues.some((value) => value.startsWith("alc "))).toEqual(
79 false,
80 );
81
82 const updated = yield* stack.deploy(
83 Effect.gen(function* () {
84 return yield* GCP.Speech.CustomClass("Ships", {
85 location,
86 customClassId: created.customClassId,
87 items: [{ value: "brig" }, { value: "barque" }],
88 });
89 }),
90 );
91
92 expect(updated.name).toEqual(created.name);
93 expect(updated.items.map((item) => item.value).sort()).toEqual([
94 "barque",
95 "brig",
96 ]);
97
98 const fetchedUpdate = yield* speech.getProjectsLocationsCustomClasses({
99 name: created.name,
100 });
101 const updatedValues = (fetchedUpdate.items ?? []).map(
102 (item) => item.value ?? "",
103 );
104 expect(updatedValues).toContain("brig");
105 expect(updatedValues).toContain("barque");
106 expect(updatedValues).not.toContain("sloop");
107
108 yield* stack.destroy();
109
110 const gone = yield* waitUntilGone(created.name);
111 expect(gone).toEqual("gone");
112 }).pipe(logLevel),
113 { tags: ["provider:gcp", "provider:gcp:speech", "live"], timeout: 90_000 },
114);