1import * as Lambda from "@/AWS/Lambda";
2import * as Transfer from "@/AWS/Transfer";
3import * as Duration from "effect/Duration";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Stream from "effect/Stream";
7import { HttpServerRequest } from "effect/http/HttpServerRequest";
8import * as HttpServerResponse from "effect/http/HttpServerResponse";
9import path from "pathe";
10
11const main = path.resolve(import.meta.dirname, "workflow-handler.ts");
12
13/**
14 * Minimal ungated fixture: exercises the account-level
15 * `SendWorkflowStepState` binding and the `consumeFileTransferEvents`
16 * EventBridge event source without provisioning a (slow, hourly-billed)
17 * Transfer server.
18 */
19export class TransferWorkflowTestFunction extends Lambda.Function<Lambda.Function>()(
20 "TransferWorkflowTestFunction",
21) {}
22
23export default TransferWorkflowTestFunction.make(
24 {
25 main,
26 functionUrl: true,
27 timeout: Duration.seconds(30),
28 },
29 Effect.gen(function* () {
30 // Event source: subscribe the host to Transfer Family file-transfer
31 // events. The deploy proves the EventBridge rule + invoke permission
32 // wiring; Transfer publishes to the default bus automatically.
33 yield* Transfer.consumeFileTransferEvents(
34 { kinds: ["file-upload-completed", "file-upload-failed"] },
35 (events) =>
36 Stream.runForEach(events, (event) =>
37 Effect.log(
38 `transfer ${event["detail-type"]}: ${event.detail.username}`,
39 ),
40 ),
41 );
42
43 const sendWorkflowStepState = yield* Transfer.SendWorkflowStepState();
44
45 return {
46 fetch: Effect.gen(function* () {
47 const request = yield* HttpServerRequest;
48 const url = new URL(request.originalUrl);
49 const pathname = url.pathname;
50
51 if (request.method === "GET" && pathname === "/bindings") {
52 return yield* HttpServerResponse.json({
53 bound: ["sendWorkflowStepState"],
54 });
55 }
56
57 // Report a step state against a nonexistent workflow: proves the
58 // grant + call round-trips and the rejection is a typed tag.
59 if (request.method === "POST" && pathname === "/workflow-step") {
60 const sent = yield* sendWorkflowStepState({
61 WorkflowId: "w-1234567890abcdef0",
62 ExecutionId: "00000000-0000-0000-0000-000000000000",
63 Token: "MA==",
64 Status: "SUCCESS",
65 }).pipe(Effect.result);
66 return yield* HttpServerResponse.json(
67 sent._tag === "Success"
68 ? { ok: true }
69 : { ok: false, tag: sent.failure._tag },
70 );
71 }
72
73 return yield* HttpServerResponse.json(
74 { error: "Not found", method: request.method, pathname },
75 { status: 404 },
76 );
77 }).pipe(Effect.orDie),
78 };
79 }).pipe(
80 Effect.provide(
81 Layer.mergeAll(Lambda.EventSource, Transfer.SendWorkflowStepStateHttp),
82 ),
83 ),
84);