The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { GcpEnvironment } from "@alchemy.run/gcp/Environment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as transcoder from "@distilled.cloud/gcp/transcoder_v1"; |
| 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(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 13 | const location = "us-central1"; |
| 14 | |
| 15 | const sdConfig: GCP.Transcoder.JobConfig = { |
| 16 | elementaryStreams: [ |
| 17 | { |
| 18 | key: "video-stream0", |
| 19 | videoStream: { |
| 20 | h264: { |
| 21 | heightPixels: 360, |
| 22 | widthPixels: 640, |
| 23 | bitrateBps: 550000, |
| 24 | frameRate: 30, |
| 25 | }, |
| 26 | }, |
| 27 | }, |
| 28 | { |
| 29 | key: "audio-stream0", |
| 30 | audioStream: { codec: "aac", bitrateBps: 64000 }, |
| 31 | }, |
| 32 | ], |
| 33 | muxStreams: [ |
| 34 | { |
| 35 | key: "sd", |
| 36 | container: "mp4", |
| 37 | elementaryStreams: ["video-stream0", "audio-stream0"], |
| 38 | }, |
| 39 | ], |
| 40 | }; |
| 41 | |
| 42 | const hdConfig: GCP.Transcoder.JobConfig = { |
| 43 | elementaryStreams: [ |
| 44 | { |
| 45 | key: "video-stream0", |
| 46 | videoStream: { |
| 47 | h264: { |
| 48 | heightPixels: 720, |
| 49 | widthPixels: 1280, |
| 50 | bitrateBps: 2500000, |
| 51 | frameRate: 30, |
| 52 | }, |
| 53 | }, |
| 54 | }, |
| 55 | { |
| 56 | key: "audio-stream0", |
| 57 | audioStream: { codec: "aac", bitrateBps: 64000 }, |
| 58 | }, |
| 59 | ], |
| 60 | muxStreams: [ |
| 61 | { |
| 62 | key: "hd", |
| 63 | container: "mp4", |
| 64 | elementaryStreams: ["video-stream0", "audio-stream0"], |
| 65 | }, |
| 66 | ], |
| 67 | }; |
| 68 | |
| 69 | const waitUntilGone = (name: string) => |
| 70 | transcoder.getProjectsLocationsJobTemplates({ name }).pipe( |
| 71 | Effect.as("found" as const), |
| 72 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 73 | Effect.repeat({ |
| 74 | schedule: Schedule.spaced("1 second"), |
| 75 | until: (status) => status === "gone", |
| 76 | times: 10, |
| 77 | }), |
| 78 | ); |
| 79 | |
| 80 | test.provider( |
| 81 | "getProjectsLocationsJobTemplates on a missing template fails with a typed tag", |
| 82 | (stack) => |
| 83 | Effect.gen(function* () { |
| 84 | const { project } = yield* GcpEnvironment.current; |
| 85 | const parent = `projects/${project}/locations/${location}`; |
| 86 | const missingName = `${parent}/jobTemplates/alchemy-missing-template`; |
| 87 | |
| 88 | yield* stack.destroy(); |
| 89 | |
| 90 | const error = yield* Effect.flip( |
| 91 | transcoder.getProjectsLocationsJobTemplates({ name: missingName }), |
| 92 | ); |
| 93 | expect(error._tag).toEqual("NotFound"); |
| 94 | |
| 95 | yield* stack.destroy(); |
| 96 | }).pipe(logLevel), |
| 97 | { |
| 98 | tags: ["provider:gcp", "provider:gcp:transcoder", "live"], |
| 99 | timeout: 90_000, |
| 100 | }, |
| 101 | ); |
| 102 | |
| 103 | test.provider( |
| 104 | "create, replace, and delete a job template", |
| 105 | (stack) => |
| 106 | Effect.gen(function* () { |
| 107 | yield* stack.destroy(); |
| 108 | |
| 109 | const created = yield* stack.deploy( |
| 110 | Effect.gen(function* () { |
| 111 | return yield* GCP.Transcoder.JobTemplate("WebHd", { |
| 112 | location, |
| 113 | labels: { env: "test" }, |
| 114 | config: sdConfig, |
| 115 | }); |
| 116 | }), |
| 117 | ); |
| 118 | |
| 119 | expect(created.jobTemplateId).toEqual(expect.any(String)); |
| 120 | expect(created.jobTemplateId.length).toBeGreaterThanOrEqual(4); |
| 121 | expect(created.name).toContain("/jobTemplates/"); |
| 122 | expect(created.location).toEqual(location); |
| 123 | expect(created.labels).toMatchObject({ env: "test" }); |
| 124 | expect(created.config?.muxStreams?.[0]?.key).toEqual("sd"); |
| 125 | |
| 126 | const fetched = yield* transcoder.getProjectsLocationsJobTemplates({ |
| 127 | name: created.name, |
| 128 | }); |
| 129 | expect(fetched.name).toEqual(created.name); |
| 130 | expect(fetched.labels?.env).toEqual("test"); |
| 131 | expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String)); |
| 132 | expect(fetched.config?.muxStreams?.[0]?.key).toEqual("sd"); |
| 133 | |
| 134 | const listed = yield* transcoder.listProjectsLocationsJobTemplates({ |
| 135 | parent: created.parent, |
| 136 | pageSize: 1000, |
| 137 | }); |
| 138 | expect( |
| 139 | (listed.jobTemplates ?? []).some((template) => template.name === created.name), |
| 140 | ).toEqual(true); |
| 141 | |
| 142 | const updated = yield* stack.deploy( |
| 143 | Effect.gen(function* () { |
| 144 | return yield* GCP.Transcoder.JobTemplate("WebHd", { |
| 145 | jobTemplateId: created.jobTemplateId, |
| 146 | location, |
| 147 | labels: { env: "prod", role: "transcode" }, |
| 148 | config: hdConfig, |
| 149 | }); |
| 150 | }), |
| 151 | ); |
| 152 | |
| 153 | expect(updated.jobTemplateId).toEqual(created.jobTemplateId); |
| 154 | expect(updated.location).toEqual(location); |
| 155 | expect(updated.labels).toMatchObject({ env: "prod", role: "transcode" }); |
| 156 | expect(updated.config?.muxStreams?.[0]?.key).toEqual("hd"); |
| 157 | |
| 158 | const fetchedUpdate = yield* transcoder.getProjectsLocationsJobTemplates({ |
| 159 | name: updated.name, |
| 160 | }); |
| 161 | expect(fetchedUpdate.labels?.env).toEqual("prod"); |
| 162 | expect(fetchedUpdate.labels?.role).toEqual("transcode"); |
| 163 | expect(fetchedUpdate.config?.muxStreams?.[0]?.key).toEqual("hd"); |
| 164 | |
| 165 | const replaced = yield* stack.deploy( |
| 166 | Effect.gen(function* () { |
| 167 | return yield* GCP.Transcoder.JobTemplate("WebHd", { |
| 168 | jobTemplateId: created.jobTemplateId, |
| 169 | location: "us-east1", |
| 170 | labels: { env: "test" }, |
| 171 | config: sdConfig, |
| 172 | }); |
| 173 | }), |
| 174 | ); |
| 175 | |
| 176 | expect(replaced.jobTemplateId).toEqual(created.jobTemplateId); |
| 177 | expect(replaced.location).toEqual("us-east1"); |
| 178 | expect(replaced.name).toContain("/locations/us-east1/"); |
| 179 | expect(replaced.name).not.toEqual(created.name); |
| 180 | |
| 181 | const oldGone = yield* waitUntilGone(updated.name); |
| 182 | expect(oldGone).toEqual("gone"); |
| 183 | |
| 184 | const fetchedReplace = yield* transcoder.getProjectsLocationsJobTemplates({ |
| 185 | name: replaced.name, |
| 186 | }); |
| 187 | expect(fetchedReplace.name).toEqual(replaced.name); |
| 188 | expect(fetchedReplace.config?.muxStreams?.[0]?.key).toEqual("sd"); |
| 189 | |
| 190 | yield* stack.destroy(); |
| 191 | |
| 192 | const gone = yield* waitUntilGone(replaced.name); |
| 193 | expect(gone).toEqual("gone"); |
| 194 | }).pipe(logLevel), |
| 195 | { |
| 196 | tags: ["provider:gcp", "provider:gcp:transcoder", "live"], |
| 197 | timeout: 90_000, |
| 198 | }, |
| 199 | ); |