The best way to control Claude Code, Codex and more from any computer or phone

main
ts 148 lines 4.75 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as translate from "@distilled.cloud/gcp/translate_v3";
5 import * as Effect from "effect/Effect";
6 import * as Schedule from "effect/Schedule";
7 import * as Stream from "effect/Stream";
8 import { location, logLevel, currentParent, runLifecycle } from "./common.ts";
9
10 const { test } = Test.make({ providers: GCP.providers() });
11
12 const waitUntilGone = (name: string) =>
13 translate.getProjectsLocationsModels({ name }).pipe(
14 Effect.as("found" as const),
15 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
16 Effect.repeat({
17 schedule: Schedule.spaced("2 seconds"),
18 until: (status) => status === "gone",
19 times: 10,
20 }),
21 );
22
23 const findDataset = (parent: string, displayName: string) =>
24 translate.listProjectsLocationsDatasets.pages({ parent, pageSize: 100 }).pipe(
25 Stream.flatMap((page) => Stream.fromIterable(page.datasets ?? [])),
26 Stream.filter((dataset) => dataset.displayName === displayName),
27 Stream.runHead,
28 );
29
30 /** An empty Translation dataset (no sentence pairs imported). */
31 const createEmptyDataset = (displayName: string) =>
32 Effect.gen(function* () {
33 const parent = yield* currentParent;
34 const existing = yield* findDataset(parent, displayName);
35 if (existing._tag === "Some" && existing.value.name) {
36 return existing.value.name;
37 }
38 const operation = yield* translate.createProjectsLocationsDatasets({
39 parent,
40 body: {
41 displayName,
42 sourceLanguageCode: "en",
43 targetLanguageCode: "es",
44 },
45 });
46 yield* GCP.Translate.waitForOperation(operation);
47 const created = yield* findDataset(parent, displayName);
48 expect(created._tag).toEqual("Some");
49 return created._tag === "Some" ? (created.value.name ?? "") : "";
50 });
51
52 const deleteDataset = (name: string) =>
53 translate.deleteProjectsLocationsDatasets({ name }).pipe(
54 Effect.flatMap((operation) => GCP.Translate.waitForOperation(operation, { notFoundOk: true })),
55 Effect.catchTag("NotFound", () => Effect.void),
56 );
57
58 test.provider(
59 "getProjectsLocationsModels on a missing model fails with a typed tag",
60 (stack) =>
61 Effect.gen(function* () {
62 const parent = yield* currentParent;
63 yield* stack.destroy();
64
65 const error = yield* Effect.flip(
66 translate.getProjectsLocationsModels({
67 name: `${parent}/models/NM0000000000000000000`,
68 }),
69 );
70 expect(error._tag).toEqual("NotFound");
71
72 yield* stack.destroy();
73 }).pipe(logLevel),
74 { tags: ["provider:gcp", "provider:gcp:translate", "live"], timeout: 90_000 },
75 );
76
77 test.provider.skipIf(!runLifecycle)(
78 "a model trained from an empty dataset fails with GCP.OperationFailed",
79 (stack) =>
80 Effect.gen(function* () {
81 yield* stack.destroy();
82
83 const dataset = yield* createEmptyDataset("alcemptymodelds");
84
85 const error = yield* Effect.flip(
86 stack.deploy(
87 Effect.gen(function* () {
88 return yield* GCP.Translate.Model("Empty", {
89 location,
90 dataset,
91 displayName: "empty",
92 });
93 }),
94 ),
95 );
96 expect(error._tag).toEqual("GCP.OperationFailed");
97
98 yield* stack.destroy();
99 yield* deleteDataset(dataset);
100 }).pipe(logLevel),
101 {
102 tags: ["provider:gcp", "provider:gcp:translate", "live"],
103 timeout: 180_000,
104 },
105 );
106
107 // Training a model needs a dataset with imported sentence pairs and takes
108 // hours. Point GCP_TEST_TRANSLATE_MODEL_DATASET at such a dataset.
109 const runModelLifecycle =
110 runLifecycle &&
111 process.env.GCP_TEST_TRANSLATE_MODEL === "1" &&
112 !!process.env.GCP_TEST_TRANSLATE_MODEL_DATASET;
113
114 test.provider.skipIf(!runModelLifecycle)(
115 "create and delete a translation model",
116 (stack) =>
117 Effect.gen(function* () {
118 yield* stack.destroy();
119 const dataset = process.env.GCP_TEST_TRANSLATE_MODEL_DATASET ?? "";
120
121 const model = yield* stack.deploy(
122 Effect.gen(function* () {
123 return yield* GCP.Translate.Model("EnEs", {
124 location,
125 dataset,
126 displayName: "enes",
127 });
128 }),
129 );
130 expect(model.name).toContain("/models/");
131 expect(model.location).toEqual(location);
132 expect(model.displayName).toEqual("enes");
133
134 const fetched = yield* translate.getProjectsLocationsModels({
135 name: model.name,
136 });
137 expect(fetched.name).toEqual(model.name);
138 expect(fetched.displayName).toEqual("enes");
139
140 yield* stack.destroy();
141 const gone = yield* waitUntilGone(model.name);
142 expect(gone).toEqual("gone");
143 }).pipe(logLevel),
144 {
145 tags: ["provider:gcp", "provider:gcp:translate", "live"],
146 timeout: 12 * 60 * 60_000,
147 },
148 );