import { mkdir } from "node:fs/promises";
import { run } from "./shell.ts";
import { makeState, runPromise } from "./t3.ts";

const home = process.argv[2];
if (!home) throw new Error("Pass a fresh test server home");
const state = makeState(home, { T3CODE_PORT: "14626" });
const source = await state.state.project("fixture-source");
const git = (args: string[], cwd = source) => runPromise(run("git", args, { cwd }));
const cli = (args: string[]) => runPromise(state.cli(args));
const paths = [];
for (const parent of ["a", "b"]) {
  await mkdir(`${source}/${parent}`);
  const path = `${source}/${parent}/demo`;
  await git(["clone", "--quiet", source, path]);
  await git(["remote", "set-url", "origin", "https://example.invalid/scenario/demo.git"], path);
  await cli(["project", "add", path]);
  if (parent === "a") await cli(["project", "rename", path, "demo-renamed"]);
  paths.push(path);
}
const thread = await state.state.thread({ project: paths[1], title: "Later checkout", messages: ["first"] });
console.log(JSON.stringify(thread));
