| 1 | import * as GCP from "@/GCP"; |
| 2 | import { PROBE_NAME, PROBE_PARENT } from "@/GCP/RealTimeBidding/internal.ts"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as rtb from "@distilled.cloud/gcp/realtimebidding_v1"; |
| 5 | import { expect } from "alchemy-test"; |
| 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( |
| 13 | MinimumLogLevel, |
| 14 | process.env.DEBUG ? "Debug" : "Info", |
| 15 | ); |
| 16 | |
| 17 | const waitUntilGone = (name: string) => |
| 18 | rtb.getBiddersPretargetingConfigs({ name }).pipe( |
| 19 | Effect.as("found" as const), |
| 20 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.spaced("1 second"), |
| 23 | until: (status) => status === "gone", |
| 24 | times: 10, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | const probeCreate = (parent: string) => |
| 29 | rtb.createBiddersPretargetingConfigs({ |
| 30 | parent, |
| 31 | body: { displayName: "alchemy-rtb-probe" }, |
| 32 | }); |
| 33 | |
| 34 | // Real-time Bidding needs a bidder account and the realtime-bidding OAuth |
| 35 | // scope; the service account profile gets InsufficientScopes. Set |
| 36 | // GCP_TEST_REALTIMEBIDDING_PARENT=bidders/{id} with such credentials. |
| 37 | const parent = process.env.GCP_TEST_REALTIMEBIDDING_PARENT?.trim(); |
| 38 | const runLifecycle = !!parent && !process.env.FAST; |
| 39 | |
| 40 | test.provider.skipIf(runLifecycle)( |
| 41 | "getBiddersPretargetingConfigs without the Real-time Bidding scope fails with InsufficientScopes", |
| 42 | (stack) => |
| 43 | Effect.gen(function* () { |
| 44 | yield* stack.destroy(); |
| 45 | |
| 46 | const error = yield* Effect.flip( |
| 47 | rtb.getBiddersPretargetingConfigs({ name: PROBE_NAME }), |
| 48 | ); |
| 49 | expect(error._tag).toEqual("InsufficientScopes"); |
| 50 | |
| 51 | yield* stack.destroy(); |
| 52 | }).pipe(logLevel), |
| 53 | { |
| 54 | tags: ["provider:gcp", "provider:gcp:realtimebidding", "live"], |
| 55 | timeout: 90_000, |
| 56 | }, |
| 57 | ); |
| 58 | |
| 59 | test.provider.skipIf(runLifecycle)( |
| 60 | "createBiddersPretargetingConfigs without the Real-time Bidding scope fails with InsufficientScopes", |
| 61 | (stack) => |
| 62 | Effect.gen(function* () { |
| 63 | yield* stack.destroy(); |
| 64 | |
| 65 | const error = yield* Effect.flip(probeCreate(PROBE_PARENT)); |
| 66 | expect(error._tag).toEqual("InsufficientScopes"); |
| 67 | |
| 68 | yield* stack.destroy(); |
| 69 | }).pipe(logLevel), |
| 70 | { |
| 71 | tags: ["provider:gcp", "provider:gcp:realtimebidding", "live"], |
| 72 | timeout: 90_000, |
| 73 | }, |
| 74 | ); |
| 75 | |
| 76 | test.provider.skipIf(!runLifecycle)( |
| 77 | "create, update, and delete a pretargeting config", |
| 78 | (stack) => |
| 79 | Effect.gen(function* () { |
| 80 | yield* stack.destroy(); |
| 81 | |
| 82 | const created = yield* stack.deploy( |
| 83 | Effect.gen(function* () { |
| 84 | return yield* GCP.RealTimeBidding.BiddersPretargetingConfig( |
| 85 | "WebHtml", |
| 86 | { |
| 87 | parent: parent!, |
| 88 | displayName: "web-html", |
| 89 | includedEnvironments: ["WEB"], |
| 90 | includedFormats: ["HTML"], |
| 91 | }, |
| 92 | ); |
| 93 | }), |
| 94 | ); |
| 95 | |
| 96 | expect(created.name).toContain("/pretargetingConfigs/"); |
| 97 | expect(created.parent).toEqual(parent); |
| 98 | expect(created.configId.length).toBeGreaterThan(0); |
| 99 | expect(created.displayName).toEqual("web-html"); |
| 100 | expect(created.includedEnvironments).toContain("WEB"); |
| 101 | expect(created.includedFormats).toContain("HTML"); |
| 102 | expect(["ACTIVE", "SUSPENDED"]).toContain(created.state); |
| 103 | |
| 104 | const fetched = yield* rtb.getBiddersPretargetingConfigs({ |
| 105 | name: created.name, |
| 106 | }); |
| 107 | expect(fetched.name).toEqual(created.name); |
| 108 | expect(fetched.displayName).toContain("[alchemy "); |
| 109 | expect(fetched.displayName).toContain("web-html"); |
| 110 | expect(fetched.includedFormats).toContain("HTML"); |
| 111 | |
| 112 | const listed = yield* rtb.listBiddersPretargetingConfigs({ |
| 113 | parent: parent!, |
| 114 | pageSize: 100, |
| 115 | }); |
| 116 | expect( |
| 117 | (listed.pretargetingConfigs ?? []).some( |
| 118 | (row) => row.name === created.name, |
| 119 | ), |
| 120 | ).toEqual(true); |
| 121 | |
| 122 | const updated = yield* stack.deploy( |
| 123 | Effect.gen(function* () { |
| 124 | return yield* GCP.RealTimeBidding.BiddersPretargetingConfig( |
| 125 | "WebHtml", |
| 126 | { |
| 127 | parent: created.parent, |
| 128 | configId: created.configId, |
| 129 | displayName: "web-html-v2", |
| 130 | includedEnvironments: ["WEB"], |
| 131 | includedFormats: ["HTML", "NATIVE"], |
| 132 | }, |
| 133 | ); |
| 134 | }), |
| 135 | ); |
| 136 | |
| 137 | expect(updated.name).toEqual(created.name); |
| 138 | expect(updated.displayName).toEqual("web-html-v2"); |
| 139 | expect(updated.includedFormats).toContain("NATIVE"); |
| 140 | |
| 141 | const fetchedUpdate = yield* rtb.getBiddersPretargetingConfigs({ |
| 142 | name: updated.name, |
| 143 | }); |
| 144 | expect(fetchedUpdate.displayName).toContain("web-html-v2"); |
| 145 | expect(fetchedUpdate.displayName).toContain("[alchemy "); |
| 146 | expect(fetchedUpdate.includedFormats).toContain("NATIVE"); |
| 147 | |
| 148 | yield* stack.destroy(); |
| 149 | |
| 150 | const gone = yield* waitUntilGone(updated.name); |
| 151 | expect(gone).toEqual("gone"); |
| 152 | }).pipe(logLevel), |
| 153 | { |
| 154 | tags: ["provider:gcp", "provider:gcp:realtimebidding", "live"], |
| 155 | timeout: 90_000, |
| 156 | }, |
| 157 | ); |