1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as jobs from "@distilled.cloud/gcp/jobs_v4";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import * as Schedule from "effect/Schedule";
7import { logLevel, currentProject, runLifecycle } from "./common.ts";
8
9const { test } = Test.make({ providers: GCP.providers() });
10
11const waitUntilGone = (name: string) =>
12 jobs.getProjectsTenants({ 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.skipIf(!runLifecycle)(
23 "getProjectsTenants on a missing tenant fails with a typed tag",
24 (stack) =>
25 Effect.gen(function* () {
26 const project = yield* currentProject;
27 yield* stack.destroy();
28
29 const error = yield* Effect.flip(
30 jobs.getProjectsTenants({
31 name: `projects/${project}/tenants/alchemy-missing-tenant`,
32 }),
33 );
34 expect(error._tag).toEqual("NotFound");
35
36 yield* stack.destroy();
37 }).pipe(logLevel),
38 { tags: ["provider:gcp", "provider:gcp:jobs", "live"], timeout: 90_000 },
39);
40
41test.provider.skipIf(runLifecycle)(
42 "createProjectsTenants fails with TalentDataPermissionRequired until onboarded",
43 (stack) =>
44 Effect.gen(function* () {
45 const project = yield* currentProject;
46 yield* stack.destroy();
47
48 const error = yield* Effect.flip(
49 jobs.createProjectsTenants({
50 parent: `projects/${project}`,
51 body: { externalId: "alchemy-jobs-probe" },
52 }),
53 );
54 expect(error._tag).toEqual("TalentDataPermissionRequired");
55
56 yield* stack.destroy();
57 }).pipe(logLevel),
58 { tags: ["provider:gcp", "provider:gcp:jobs", "live"], timeout: 90_000 },
59);
60
61test.provider.skipIf(!runLifecycle)(
62 "create, update, and delete a tenant",
63 (stack) =>
64 Effect.gen(function* () {
65 const project = yield* currentProject;
66 yield* stack.destroy();
67
68 const created = yield* stack.deploy(
69 Effect.gen(function* () {
70 return yield* GCP.Jobs.Tenant("Acme", {
71 externalId: "acme-corp",
72 });
73 }),
74 );
75
76 expect(created.name.startsWith(`projects/${project}/tenants/`)).toEqual(
77 true,
78 );
79 expect(created.tenantId.length).toBeGreaterThan(0);
80 expect(created.externalId).toEqual("acme-corp");
81 expect(created.project).toEqual(project);
82
83 const fetched = yield* jobs.getProjectsTenants({ name: created.name });
84 expect(fetched.name).toEqual(created.name);
85 expect(fetched.externalId).toContain("[alchemy ");
86 expect(fetched.externalId).toContain("acme-corp");
87
88 const updated = yield* stack.deploy(
89 Effect.gen(function* () {
90 return yield* GCP.Jobs.Tenant("Acme", {
91 tenantId: created.tenantId,
92 externalId: "acme-holdings",
93 });
94 }),
95 );
96
97 expect(updated.name).toEqual(created.name);
98 expect(updated.externalId).toEqual("acme-holdings");
99
100 const fetchedUpdate = yield* jobs.getProjectsTenants({
101 name: created.name,
102 });
103 expect(fetchedUpdate.externalId).toContain("acme-holdings");
104
105 yield* stack.destroy();
106
107 const gone = yield* waitUntilGone(created.name);
108 expect(gone).toEqual("gone");
109 }).pipe(logLevel),
110 { tags: ["provider:gcp", "provider:gcp:jobs", "live"], timeout: 90_000 },
111);