sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ Railway/ CloudAgent.test.ts

import * as Railway from "@alchemy.run/railway";
import { expect } from "@alchemy.run/test";
import * as Test from "@alchemy.run/test/Alchemy";
import { Query } from "@distilled.cloud/core/query";
import { Railway as RailwayApi } from "@distilled.cloud/railway";
import * as Provider from "alchemy/Provider";
import * as Effect from "effect/Effect";
import { MinimumLogLevel } from "effect/References";
import * as Result from "effect/Result";
import * as Schedule from "effect/Schedule";
import { suitePartition } from "./suiteProject.ts";

const { test } = Test.make({ providers: Railway.providers() });

const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");

// Cloud agents are Priority Boarding. Unentitled workspaces reject
// `cloudAgentCreate` with a typed plan/auth tag. The probe always runs
// and pins that tag. When the token is entitled the same test continues
// through the resource create+list+delete lifecycle.

// `cloudAgents` declares no not-found error; a missing environment
// surfaces as an empty list.
const listLive = Query.fn((environmentId: string) =>
  RailwayApi.cloudAgents({ environmentId, mine: true }).pipe(
    Query.map((agent) => ({
      id: agent.id,
      name: agent.name,
      status: agent.status,
      environmentId: agent.environmentId,
      projectId: agent.projectId,
    })),
  ),
);

const waitUntilAgentGone = (environmentId: string, cloudAgentId: string) =>
  listLive(environmentId).pipe(
    Effect.map((items) =>
      items.some((agent) => agent.id === cloudAgentId && agent.status !== "DELETING")
        ? ("found" as const)
        : ("gone" as const),
    ),
    Effect.repeat({
      schedule: Schedule.spaced("1 second"),
      until: (status) => status === "gone",
      times: 10,
    }),
  );

const deleteAgent = Query.fn((id: string) => RailwayApi.cloudAgentDelete({ id }));

const createCloudAgent = Query.fn((input: { environmentId: string; name: string }) => {
  const agent = RailwayApi.cloudAgentCreate({ input });
  return { id: agent.id };
});

test.provider(
  "create, list, and delete a cloud agent",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();

      const projectOnly = yield* stack.deploy(
        Effect.gen(function* () {
          const { project, environment } = yield* suitePartition;
          return { project, environment };
        }),
      );

      const probe = yield* Effect.result(
        createCloudAgent({
          environmentId: projectOnly.environment.environmentId,
          name: projectOnly.project.name,
        }),
      );
      if (Result.isFailure(probe)) {
        expect(
          ["RailwayForbidden", "RailwayPlanLimitExceeded"].includes(probe.failure._tag),
        ).toEqual(true);
        yield* stack.destroy();
        return;
      }

      yield* deleteAgent(probe.success.id);
      yield* waitUntilAgentGone(projectOnly.environment.environmentId, probe.success.id);

      const created = yield* stack.deploy(
        Effect.gen(function* () {
          const { project, environment } = yield* suitePartition;
          const agent = yield* Railway.CloudAgent("Coder", { environment });
          return { project, environment, agent };
        }),
      );

      expect(created.agent.cloudAgentId).toEqual(expect.any(String));
      expect(created.agent.cloudAgentId.length).toBeGreaterThan(0);
      expect(created.agent.environmentId).toEqual(created.environment.environmentId);
      expect(created.agent.projectId).toEqual(created.project.projectId);
      expect(created.agent.name).toEqual(expect.any(String));
      expect(created.agent.name.length).toBeGreaterThan(0);
      expect(created.agent.name.length).toBeLessThanOrEqual(32);
      expect(created.agent.name).toMatch(/^[a-z][a-z0-9-]*$/);
      expect(created.agent.createdAt).toEqual(expect.any(String));
      expect(created.agent.status).toEqual(expect.any(String));
      expect(
        ["CRASHED", "DELETING", "FAILED", "RUNNING", "SLEEPING", "STARTING"].includes(
          created.agent.status,
        ),
      ).toEqual(true);

      const listed = yield* listLive(created.environment.environmentId);
      const fetched = listed.find((agent) => agent.id === created.agent.cloudAgentId);
      expect(fetched).toBeDefined();
      expect(fetched?.name).toEqual(created.agent.name);
      expect(fetched?.environmentId).toEqual(created.environment.environmentId);
      expect(fetched?.projectId).toEqual(created.project.projectId);

      const provider = yield* Provider.findProvider(Railway.CloudAgent);
      const fromProvider = yield* provider.list();
      const found = fromProvider.find((agent) => agent.cloudAgentId === created.agent.cloudAgentId);
      expect(found).toBeDefined();
      expect(found?.name).toEqual(created.agent.name);
      expect(found?.environmentId).toEqual(created.environment.environmentId);

      yield* stack.destroy();

      const agentGone = yield* waitUntilAgentGone(
        created.environment.environmentId,
        created.agent.cloudAgentId,
      );
      expect(agentGone).toEqual("gone");
    }).pipe(logLevel),
  {
    tags: [
      "provider:railway",
      "provider:railway:cloudagent",
      "provider:railway:project",
      "provider:railway:projectenvironment",
      "live",
    ],
    timeout: 120_000,
  },
);