1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as androidpublisher from "@distilled.cloud/gcp/androidpublisher_v3";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import * as Schedule from "effect/Schedule";
7import {
8 logLevel,
9 packageName,
10 probePackageName,
11 missingTag,
12 runLifecycle,
13} from "./common.ts";
14
15const { test } = Test.make({ providers: GCP.providers() });
16
17const waitUntilGone = (appId: string, sku: string) =>
18 androidpublisher.getInappproducts({ packageName: appId, sku }).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
28test.provider(
29 "getInappproducts on a missing product fails with a typed tag",
30 (stack) =>
31 Effect.gen(function* () {
32 yield* stack.destroy();
33
34 const error = yield* Effect.flip(
35 androidpublisher.getInappproducts({
36 packageName: probePackageName,
37 sku: "alchemy_missing_iap",
38 }),
39 );
40 expect(error._tag).toEqual(missingTag);
41
42 yield* stack.destroy();
43 }).pipe(logLevel),
44 {
45 tags: ["provider:gcp", "provider:gcp:androidpublisher", "live"],
46 timeout: 90_000,
47 },
48);
49
50test.provider.skipIf(runLifecycle)(
51 "insertInappproducts without Play access fails with a typed tag",
52 (stack) =>
53 Effect.gen(function* () {
54 yield* stack.destroy();
55
56 const error = yield* Effect.flip(
57 androidpublisher.insertInappproducts({
58 packageName: probePackageName,
59 autoConvertMissingPrices: true,
60 body: {
61 packageName: probePackageName,
62 sku: "alchemy_probe_iap",
63 defaultLanguage: "en-US",
64 purchaseType: "managedUser",
65 defaultPrice: { currency: "USD", priceMicros: "990000" },
66 listings: { "en-US": { title: "Alchemy Probe" } },
67 },
68 }),
69 );
70 expect(error._tag).toEqual("Forbidden");
71
72 yield* stack.destroy();
73 }).pipe(logLevel),
74 {
75 tags: ["provider:gcp", "provider:gcp:androidpublisher", "live"],
76 timeout: 90_000,
77 },
78);
79
80test.provider.skipIf(!runLifecycle)(
81 "create, update, and delete an in-app product",
82 (stack) =>
83 Effect.gen(function* () {
84 yield* stack.destroy();
85
86 const created = yield* stack.deploy(
87 Effect.gen(function* () {
88 return yield* GCP.AndroidPublisher.Inappproduct("GemPack", {
89 packageName: packageName!,
90 listings: { "en-US": { title: "Gem pack" } },
91 });
92 }),
93 );
94
95 expect(created.sku.length).toBeGreaterThan(0);
96 expect(created.packageName).toEqual(packageName);
97 expect(created.listings?.["en-US"]?.title).toEqual("Gem pack");
98
99 const fetched = yield* androidpublisher.getInappproducts({
100 packageName: created.packageName,
101 sku: created.sku,
102 });
103 expect(fetched.sku).toEqual(created.sku);
104 expect(fetched.listings?.["en-US"]?.title).toEqual("Gem pack");
105
106 const updated = yield* stack.deploy(
107 Effect.gen(function* () {
108 return yield* GCP.AndroidPublisher.Inappproduct("GemPack", {
109 packageName: created.packageName,
110 sku: created.sku,
111 listings: { "en-US": { title: "Starter gem pack" } },
112 });
113 }),
114 );
115
116 expect(updated.sku).toEqual(created.sku);
117 expect(updated.listings?.["en-US"]?.title).toEqual("Starter gem pack");
118
119 yield* stack.destroy();
120
121 const gone = yield* waitUntilGone(created.packageName, created.sku);
122 expect(gone).toEqual("gone");
123 }).pipe(logLevel),
124 {
125 tags: ["provider:gcp", "provider:gcp:androidpublisher", "live"],
126 timeout: 90_000,
127 },
128);