import { describe, it, expect, vi } from "vitest";
import { render } from "@testing-library/react";

import type { Gateways } from "@/ports";
import { MockTerminalGateway } from "@/adapters/mock";
import { DIProvider } from "@/app/di";

const terminalOptions: unknown[] = [];

vi.mock("@xterm/xterm", () => ({
  Terminal: class {
    readonly rows = 24;
    readonly cols = 80;

    constructor(options: unknown) {
      terminalOptions.push(options);
    }

    loadAddon() {}
    open() {}
    onData() {
      return { dispose() {} };
    }
    write() {}
    input() {}
    focus() {}
    dispose() {}
  },
}));

vi.mock("@xterm/addon-fit", () => ({
  FitAddon: class {
    fit() {}
  },
}));

vi.mock("@xterm/xterm/css/xterm.css", () => ({}));

if (typeof globalThis.ResizeObserver === "undefined") {
  globalThis.ResizeObserver = class {
    observe() {}
    unobserve() {}
    disconnect() {}
  } as unknown as typeof ResizeObserver;
}

import {
  TerminalView,
  TERMINAL_SCROLLBACK_LINES,
} from "./TerminalView";

describe("TerminalView scrollback", () => {
  it("configures xterm with a deep scrollback for chatty OpenCode agents", async () => {
    const gateways = { terminal: new MockTerminalGateway() } as unknown as Gateways;

    render(
      <DIProvider gateways={gateways}>
        <TerminalView cwd="/cwd" />
      </DIProvider>,
    );

    expect(terminalOptions[0]).toMatchObject({
      scrollback: TERMINAL_SCROLLBACK_LINES,
    });
    expect(TERMINAL_SCROLLBACK_LINES).toBeGreaterThan(1_000);
  });
});
