import { expect } from "e2e";
import * as Effect from "effect/Effect";
import { T3_HOME } from "../e2e.config.ts";
import { rpcClient, runPromise, test, WEB } from "./t3.ts";

test("A rejected GitHub app refresh offers sign-in again", WEB, async ({ t3, app, screen }) => {
  await t3.project("GitHub refresh example");
  await runPromise(Effect.gen(function* () {
    const client = yield* rpcClient(T3_HOME, app.baseUrl ?? "");
    yield* client["server.updateSettings"]({ patch: { sourceControlHosts: { github: {
      signIns: { "github.com": JSON.stringify({
        accessToken: "synthetic-expired-access-token", accessTokenExpiresAt: 1,
        refreshToken: "synthetic-expired-refresh-token", refreshTokenExpiresAt: null,
      }) },
    } } } });
    yield* client["server.discoverSourceControl"]({});
    yield* client["server.discoverSourceControl"]({});
  }).pipe(Effect.scoped));
  await t3.open("/settings/source-control");
  await screen.getByText("Source control & issues", { exact: true }).first().waitFor();
  await screen.getByRole("button", "Toggle GitHub details").tap();
  await screen.getByText("T3 Code for GitHub", { exact: true }).first().scrollIntoView();
  await app.screenshot("github-app-after-rejected-refresh");
  await expect(screen.getByRole("button", "Sign in with GitHub", { exact: true })).toBeVisible();
});
