| 1 | import { Query } from "@distilled.cloud/core/query"; |
| 2 | import { Railway as RailwayApi } from "@distilled.cloud/railway"; |
| 3 | import * as Railway from "@/Railway"; |
| 4 | import { suitePartition } from "../suiteProject.ts"; |
| 5 | import * as Test from "@/Test/Alchemy"; |
| 6 | import { expect } from "alchemy-test"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import * as pathe from "pathe"; |
| 11 | import { cloneFixture } from "../../Cloudflare/Utils/Fixture.ts"; |
| 12 | import { expectUrlContains } from "../../Cloudflare/Utils/Http.ts"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: Railway.providers() }); |
| 15 | |
| 16 | const logLevel = Effect.provideService( |
| 17 | MinimumLogLevel, |
| 18 | process.env.DEBUG ? "Debug" : "Info", |
| 19 | ); |
| 20 | |
| 21 | const fixtureDir = pathe.resolve( |
| 22 | import.meta.dirname, |
| 23 | "../../AWS/Website/fixtures/react-router-app", |
| 24 | ); |
| 25 | const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp"); |
| 26 | const fixtureEntries = [ |
| 27 | ".gitignore", |
| 28 | "package.json", |
| 29 | "react-router.config.ts", |
| 30 | "vite.config.ts", |
| 31 | "app", |
| 32 | "public", |
| 33 | ]; |
| 34 | |
| 35 | const readService = Query.fn((id: string) => ({ |
| 36 | deletedAt: RailwayApi.service({ id }).deletedAt, |
| 37 | })); |
| 38 | |
| 39 | const waitUntilGone = (serviceId: string) => |
| 40 | readService(serviceId).pipe( |
| 41 | Effect.map((service) => |
| 42 | service.deletedAt != null ? ("gone" as const) : ("found" as const), |
| 43 | ), |
| 44 | Effect.catchTag("RailwayNotFound", () => Effect.succeed("gone" as const)), |
| 45 | Effect.repeat({ |
| 46 | schedule: Schedule.spaced("1 second"), |
| 47 | until: (status) => status === "gone", |
| 48 | times: 10, |
| 49 | }), |
| 50 | ); |
| 51 | |
| 52 | test.provider( |
| 53 | "ReactRouter: deploy, GET /, destroy, gone", |
| 54 | (stack) => |
| 55 | Effect.gen(function* () { |
| 56 | yield* stack.destroy(); |
| 57 | |
| 58 | const rootDir = yield* cloneFixture(fixtureDir, { |
| 59 | prefix: "alchemy-react-router-railway-", |
| 60 | tempRoot, |
| 61 | entries: fixtureEntries, |
| 62 | }); |
| 63 | |
| 64 | const deployed = yield* stack.deploy( |
| 65 | Effect.gen(function* () { |
| 66 | const { project, environment } = yield* suitePartition; |
| 67 | const site = yield* Railway.Website.ReactRouter("Web", { |
| 68 | project, |
| 69 | environment, |
| 70 | rootDir, |
| 71 | memo: { |
| 72 | include: [ |
| 73 | "app/**", |
| 74 | "public/**", |
| 75 | "package.json", |
| 76 | "react-router.config.ts", |
| 77 | "vite.config.ts", |
| 78 | ], |
| 79 | }, |
| 80 | }); |
| 81 | return { site }; |
| 82 | }), |
| 83 | ); |
| 84 | |
| 85 | const url = deployed.site.url; |
| 86 | expect(url).toBeDefined(); |
| 87 | expect(url).toMatch(/^https:\/\//); |
| 88 | expect(deployed.site.service).toBeDefined(); |
| 89 | expect(deployed.site.project).toBeDefined(); |
| 90 | |
| 91 | yield* expectUrlContains(`${url!}/`, "REACT_ROUTER_AWS_PAGE_MARKER", { |
| 92 | timeout: "60 seconds", |
| 93 | label: "home page", |
| 94 | }); |
| 95 | yield* expectUrlContains( |
| 96 | `${url!}/api/hello?echo=roundtrip`, |
| 97 | "REACT_ROUTER_AWS_API_MARKER", |
| 98 | { |
| 99 | timeout: "30 seconds", |
| 100 | label: "api route", |
| 101 | }, |
| 102 | ); |
| 103 | |
| 104 | const serviceId = deployed.site.service!.serviceId; |
| 105 | yield* stack.destroy(); |
| 106 | const gone = yield* waitUntilGone(serviceId); |
| 107 | expect(gone).toEqual("gone"); |
| 108 | }).pipe(logLevel), |
| 109 | { |
| 110 | tags: [ |
| 111 | "provider:railway", |
| 112 | "provider:railway:project", |
| 113 | "provider:railway:projectenvironment", |
| 114 | "provider:railway:service", |
| 115 | "provider:railway:website", |
| 116 | "live", |
| 117 | ], |
| 118 | timeout: 120_000, |
| 119 | }, |
| 120 | ); |