Skip to content

Commit 75823a2

Browse files
committed
test(cloudflare/ruleset): require empty live phase
1 parent 90a2ffd commit 75823a2

1 file changed

Lines changed: 32 additions & 9 deletions

File tree

packages/alchemy/test/Cloudflare/Ruleset/Ruleset.test.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,24 @@ const logLevel = Effect.provideService(
1212
process.env.DEBUG ? "Debug" : "Info",
1313
);
1414

15-
const zoneId = process.env.CLOUDFLARE_TEST_RULESET_ZONE_ID;
15+
const zoneId = process.env.CLOUDFLARE_TEST_EMPTY_RULESET_ZONE_ID;
16+
const phase = "http_request_firewall_custom";
17+
type TestRulesetPhase = typeof phase;
1618

1719
test.provider.skipIf(!zoneId)(
1820
"creates, updates, and deletes a zone phase entrypoint ruleset",
1921
(stack) =>
2022
Effect.gen(function* () {
23+
const existingRules = yield* getPhaseRules(zoneId!, phase);
24+
expect(existingRules).toEqual([]);
25+
2126
yield* stack.destroy();
2227

2328
const initial = yield* stack.deploy(
2429
Effect.gen(function* () {
2530
return yield* Cloudflare.Ruleset("TestRuleset", {
2631
zone: { zoneId: zoneId! },
27-
phase: "http_request_firewall_custom",
32+
phase,
2833
rules: [
2934
{
3035
description: "Alchemy test rule",
@@ -37,14 +42,14 @@ test.provider.skipIf(!zoneId)(
3742
);
3843

3944
expect(initial.zoneId).toEqual(zoneId);
40-
expect(initial.phase).toEqual("http_request_firewall_custom");
45+
expect(initial.phase).toEqual(phase);
4146
expect(initial.rules).toHaveLength(1);
4247

4348
const updated = yield* stack.deploy(
4449
Effect.gen(function* () {
4550
return yield* Cloudflare.Ruleset("TestRuleset", {
4651
zone: { zoneId: zoneId! },
47-
phase: "http_request_firewall_custom",
52+
phase,
4853
rules: [
4954
{
5055
description: "Updated Alchemy test rule",
@@ -63,10 +68,28 @@ test.provider.skipIf(!zoneId)(
6368

6469
yield* stack.destroy();
6570

66-
const actual = yield* rulesets.getPhas({
67-
zoneId: zoneId!,
68-
rulesetPhase: "http_request_firewall_custom",
69-
} as any);
70-
expect(actual.rules).toHaveLength(0);
71+
const actualRules = yield* getPhaseRules(zoneId!, phase);
72+
expect(actualRules).toEqual([]);
7173
}).pipe(logLevel),
7274
);
75+
76+
const getPhaseRules = Effect.fn(function* (
77+
zoneId: string,
78+
phase: TestRulesetPhase,
79+
) {
80+
return yield* rulesets
81+
.getPhas({
82+
zoneId,
83+
rulesetPhase: phase,
84+
} as any)
85+
.pipe(
86+
Effect.map((ruleset) => ruleset.rules),
87+
Effect.catchIf(isNotFoundError, () => Effect.succeed([])),
88+
);
89+
});
90+
91+
const isNotFoundError = (error: unknown): boolean =>
92+
typeof error === "object" &&
93+
error !== null &&
94+
(("status" in error && (error as { status: unknown }).status === 404) ||
95+
("_tag" in error && (error as { _tag: unknown })._tag === "NotFound"));

0 commit comments

Comments
 (0)