1import * as AWS from "@/AWS";
2import * as Provider from "@/Provider";
3import * as Test from "./Test.ts";
4import * as ag from "@distilled.cloud/aws/api-gateway";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import { assertRestApiDeleted } from "./assertions.ts";
8
9const { test } = Test.make({ providers: AWS.providers() });
10
11test.provider.skipIf(!!process.env.FAST)(
12 "create and delete gateway response",
13 (stack) =>
14 Effect.gen(function* () {
15 yield* stack.destroy();
16
17 const { api } = yield* stack.deploy(
18 Effect.gen(function* () {
19 const api = yield* AWS.ApiGateway.RestApi("AgGwRespApi", {
20 endpointConfiguration: { types: ["REGIONAL"] },
21 });
22 yield* AWS.ApiGateway.GatewayResponse("AgDefault4xx", {
23 restApiId: api.restApiId,
24 responseType: "DEFAULT_4XX",
25 responseTemplates: {
26 "application/json": '{"message":"test"}',
27 },
28 });
29 return { api };
30 }),
31 );
32
33 const g = yield* ag.getGatewayResponse({
34 restApiId: api.restApiId,
35 responseType: "DEFAULT_4XX",
36 });
37 expect(g.responseType).toEqual("DEFAULT_4XX");
38
39 yield* stack.destroy();
40 yield* assertRestApiDeleted(api.restApiId);
41 }),
42 { tags: ["provider:aws", "provider:aws:apigateway", "live"] },
43);
44
45test.provider.skipIf(!!process.env.FAST)(
46 "update gateway response status and templates",
47 (stack) =>
48 Effect.gen(function* () {
49 yield* stack.destroy();
50
51 const { api } = yield* stack.deploy(
52 Effect.gen(function* () {
53 const api = yield* AWS.ApiGateway.RestApi("AgGwRespUpdateApi", {
54 endpointConfiguration: { types: ["REGIONAL"] },
55 });
56 yield* AWS.ApiGateway.GatewayResponse("AgDefault5xx", {
57 restApiId: api.restApiId,
58 responseType: "DEFAULT_5XX",
59 statusCode: "500",
60 responseTemplates: {
61 "application/json": '{"message":"v1"}',
62 },
63 });
64 return { api };
65 }),
66 );
67
68 yield* stack.deploy(
69 Effect.gen(function* () {
70 const apiAgain = yield* AWS.ApiGateway.RestApi("AgGwRespUpdateApi", {
71 endpointConfiguration: { types: ["REGIONAL"] },
72 });
73 yield* AWS.ApiGateway.GatewayResponse("AgDefault5xx", {
74 restApiId: apiAgain.restApiId,
75 responseType: "DEFAULT_5XX",
76 statusCode: "502",
77 responseTemplates: {
78 "application/json": '{"message":"v2"}',
79 },
80 });
81 }),
82 );
83
84 const g = yield* ag.getGatewayResponse({
85 restApiId: api.restApiId,
86 responseType: "DEFAULT_5XX",
87 });
88 expect(g.statusCode).toEqual("502");
89 expect(g.responseTemplates?.["application/json"]).toEqual(
90 '{"message":"v2"}',
91 );
92
93 yield* stack.destroy();
94 yield* assertRestApiDeleted(api.restApiId);
95 }),
96 // Three sequential deploy/destroy cycles against API Gateway's account-wide
97 // throttle: ~95s alone, >120s under a saturated full-suite run.
98 {
99 tags: ["provider:aws", "provider:aws:apigateway", "live"],
100 timeout: 240_000,
101 },
102);
103
104test.provider.skipIf(!!process.env.FAST)(
105 "list enumerates the deployed gateway response",
106 (stack) =>
107 Effect.gen(function* () {
108 yield* stack.destroy();
109
110 const { api } = yield* stack.deploy(
111 Effect.gen(function* () {
112 const api = yield* AWS.ApiGateway.RestApi("AgGwRespListApi", {
113 endpointConfiguration: { types: ["REGIONAL"] },
114 });
115 yield* AWS.ApiGateway.GatewayResponse("AgListDefault4xx", {
116 restApiId: api.restApiId,
117 responseType: "DEFAULT_4XX",
118 responseTemplates: {
119 "application/json": '{"message":"list"}',
120 },
121 });
122 return { api };
123 }),
124 );
125
126 const provider = yield* Provider.findProvider(
127 AWS.ApiGateway.GatewayResponse,
128 );
129 const all = yield* provider.list();
130
131 expect(
132 all.some(
133 (g) =>
134 g.restApiId === api.restApiId && g.responseType === "DEFAULT_4XX",
135 ),
136 ).toBe(true);
137
138 yield* stack.destroy();
139 yield* assertRestApiDeleted(api.restApiId);
140 }),
141 { tags: ["provider:aws", "provider:aws:apigateway", "live"] },
142);