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