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

main
ts 154 lines 4.88 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Core from "@alchemy.run/test/Core";
5 import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
6 import * as speech from "@distilled.cloud/gcp/speech_v1";
7 import * as Effect from "effect/Effect";
8 import { expectProbe } from "../bindingHost.ts";
9 import SpeechBindingsHost, { Hints, Ships } from "./fixtures/bindings-host.ts";
10
11 const testOptions = { providers: GCP.providers() };
12 const { test, beforeAll, afterAll } = Test.make(testOptions);
13 const sharedStack = Core.scratchStack(testOptions, "SpeechBindings");
14
15 let baseUrl: string;
16 let hostAccount: string;
17 let project: string;
18 let customClassName: string;
19 let phraseSetName: string;
20
21 /**
22 * Project-level roles (with their IAM Condition, if any) held by the
23 * host's service account. Speech-to-Text has no resource-level IAM.
24 */
25 const projectRoles = () =>
26 resourcemanager
27 .getIamPolicyProjects({
28 resource: `projects/${project}`,
29 body: { options: { requestedPolicyVersion: 3 } },
30 })
31 .pipe(
32 Effect.map((policy) =>
33 (policy.bindings ?? [])
34 .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${hostAccount}`))
35 .map((binding) => ({
36 role: binding.role,
37 condition: binding.condition?.expression,
38 })),
39 ),
40 );
41
42 describe(
43 "Speech Bindings",
44 {
45 tags: ["provider:gcp", "provider:gcp:speech", "provider:gcp:run", "live"],
46 },
47 () => {
48 beforeAll(
49 Effect.gen(function* () {
50 yield* sharedStack.destroy();
51 const out = yield* sharedStack.deploy(
52 Effect.gen(function* () {
53 const host = yield* SpeechBindingsHost;
54 const ships = yield* Ships;
55 const hints = yield* Hints;
56 return {
57 uri: host.uri,
58 serviceAccount: host.serviceAccount,
59 project: host.project,
60 customClass: ships.name,
61 phraseSet: hints.name,
62 };
63 }),
64 );
65 baseUrl = out.uri!;
66 hostAccount = out.serviceAccount!;
67 project = out.project;
68 customClassName = out.customClass;
69 phraseSetName = out.phraseSet;
70 }),
71 { timeout: 900_000 },
72 );
73
74 afterAll(sharedStack.destroy(), { timeout: 600_000 });
75
76 // Every Speech binding grants the same unconditioned project role.
77 const expectSpeechClientOnly = Effect.gen(function* () {
78 expect(yield* projectRoles()).toEqual([
79 { role: "roles/speech.client", condition: undefined },
80 ]);
81 });
82
83 describe("GetCustomClass", () => {
84 test.provider(
85 "reads the bound custom class as the host",
86 (_stack) =>
87 Effect.gen(function* () {
88 const out = yield* expectProbe<{ name: string; items: string[] }>(
89 baseUrl,
90 "getCustomClass",
91 );
92 expect(out.name).toEqual(customClassName);
93 expect(out.items).toEqual(["sloop", "schooner"]);
94
95 const live = yield* speech.getProjectsLocationsCustomClasses({
96 name: customClassName,
97 });
98 expect(live.name).toEqual(out.name);
99 yield* expectSpeechClientOnly;
100 }),
101 {
102 tags: ["provider:gcp", "provider:gcp:speech", "live"],
103 timeout: 600_000,
104 },
105 );
106 });
107
108 describe("GetPhraseSet", () => {
109 test.provider(
110 "reads the bound phrase set as the host",
111 (_stack) =>
112 Effect.gen(function* () {
113 const out = yield* expectProbe<{
114 name: string;
115 phrases: string[];
116 }>(baseUrl, "getPhraseSet");
117 expect(out.name).toEqual(phraseSetName);
118 expect(out.phrases).toEqual(["weather"]);
119
120 const live = yield* speech.getProjectsLocationsPhraseSets({
121 name: phraseSetName,
122 });
123 expect(live.name).toEqual(out.name);
124 yield* expectSpeechClientOnly;
125 }),
126 {
127 tags: ["provider:gcp", "provider:gcp:speech", "live"],
128 timeout: 600_000,
129 },
130 );
131 });
132
133 describe("Recognize", () => {
134 test.provider(
135 "recognizes audio adapted with the bound phrase set as the host",
136 (_stack) =>
137 Effect.gen(function* () {
138 const out = yield* expectProbe<{
139 results: unknown[];
140 totalBilledTime: string;
141 }>(baseUrl, "recognize");
142 // Silence has nothing to transcribe, but the request is billed.
143 expect(out.results).toEqual([]);
144 expect(out.totalBilledTime).toMatch(/^\d+(\.\d+)?s$/);
145 yield* expectSpeechClientOnly;
146 }),
147 {
148 tags: ["provider:gcp", "provider:gcp:speech", "live"],
149 timeout: 600_000,
150 },
151 );
152 });
153 },
154 );