import { expect } from "e2e";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";

import { T3_HOME } from "../e2e.config.ts";
import { makeState, rpcClient, runPromise, test, WEB } from "./t3.ts";

const KEPT = "timing";
// The emoji's two UTF-16 units straddle unit 200, where the server cuts a run's title.
const DEFINITION = `export default T3.workflow("timing", { version: 1, payload: Schema.Struct({}) },
  Effect.fn(function* () {
    yield* T3.step("under ten seconds", { returns: Schema.Null }, Effect.as(Effect.sleep("9700 millis"), null));
    yield* T3.step("almost ten seconds", { returns: Schema.Null }, Effect.as(Effect.sleep("9960 millis"), null));
  }));
`;

test(
  "A completed almost-ten-second workflow step reads 10s",
  WEB,
  async ({ t3, app, screen }) => {
    const definition = await runPromise(
      Effect.map(Path.Path, (path) => path.join(T3_HOME, "workflows/timing.ts")),
    );
    const workflowsOn = (on: boolean) =>
      runPromise(
        Effect.gen(function* () {
          const client = yield* rpcClient(T3_HOME, app.baseUrl ?? "");
          yield* client["server.updateSettings"]({ patch: { workflowsEnabled: on } });
        }).pipe(Effect.scoped),
      );
    try {
      await runPromise(
        Effect.gen(function* () {
          const fs = yield* FileSystem.FileSystem;
          const path = yield* Path.Path;
          yield* fs.makeDirectory(path.dirname(definition), { recursive: true });
          yield* fs.writeFileString(definition, DEFINITION);
        }),
      );
      await t3.project("demo");
      await workflowsOn(true);
      const started = await runPromise(
        makeState(T3_HOME).cli(["workflow", "start", "timing", "--key", "run1", "--json"]),
      );
      const { run } = JSON.parse(started.stdout);
      await t3.open(`/workflows?run=${run.runId}`);

      await screen.getByText("took 20s", { exact: false }).waitFor({ timeout: 60_000 });
      await app.screenshot("completed-workflow-step-times");
      await expect(screen.getByText("10s", { exact: true })).toBeVisible();
    } finally {
      await workflowsOn(false);
      await runPromise(
        Effect.flatMap(FileSystem.FileSystem, (fs) => fs.remove(definition, { force: true })),
      );
    }
  },
);
