sailfork / t3code-github / .repos/ alchemy-effect/ examples/ cloudflare-git-artifacts/ test/ integ.test.ts

import { expect } from "bun:test";
import * as Cloudflare from "@alchemy.run/cloudflare";
import * as Test from "@alchemy.run/test/Bun";
import * as Effect from "effect/Effect";
import * as HttpApiClient from "effect/http-api/HttpApiClient";
import Stack from "../alchemy.run.ts";
import { RepoApi } from "../src/Api.ts";

const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
  providers: Cloudflare.providers(),
  state: Cloudflare.state(),
});

const stack = beforeAll(deploy(Stack));
afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));

// Deterministic repo name — the same on every run (never Date.now()/random)
// so a crashed run's leftover repo is purged below instead of leaking.
const repoName = "tutorial-integ-lifecycle";

test(
  "repo lifecycle",
  Effect.gen(function* () {
    const { url } = yield* stack;
    const client = yield* HttpApiClient.make(RepoApi, { baseUrl: url });

    // Purge any leftover from a previously crashed run so createRepo
    // doesn't fail with RepoConflict.
    yield* client.repos
      .deleteRepo({ params: { name: repoName } })
      .pipe(Effect.catchTag("RepoNotFound", () => Effect.void));

    const created = yield* client.repos.createRepo({
      payload: { name: repoName, description: "tutorial repo" },
    });
    expect(created.name).toBe(repoName);
    expect(created.remote).toBeString();
    expect(created.token).toBeString();

    const info = yield* client.repos.getRepo({ params: { name: repoName } });
    expect(info.name).toBe(repoName);
    expect(info.defaultBranch).toBe("main");
    expect(info.metadata?.description).toBe("tutorial repo");
    expect(info.metadata?.stars).toBe(0);

    const token = yield* client.repos.cloneToken({
      params: { name: repoName },
      payload: { scope: "read", ttl: 600 },
    });
    expect(token.plaintext).toBeString();
    expect(token.scope).toBe("read");

    const updated = yield* client.repos.updateRepo({
      params: { name: repoName },
      payload: { description: "now with stars", topics: ["demo", "alchemy"] },
    });
    expect(updated.description).toBe("now with stars");
    expect(updated.topics).toEqual(["demo", "alchemy"]);

    const starred = yield* client.repos.starRepo({
      params: { name: repoName },
    });
    expect(starred.stars).toBe(1);

    yield* client.repos.deleteRepo({ params: { name: repoName } });
  }),
  { timeout: 120_000 },
);