import { describe, expect, it } from "vitest";

import {
	getTerminalScrollbackLimit,
	isCurrentWebSocket,
	shouldRetryWebSocketClose,
} from "./terminal.tsx";

describe("getTerminalScrollbackLimit", () => {
	it("retains normal-buffer history for custom mobile swipe scrolling", () => {
		expect(getTerminalScrollbackLimit(true)).toBe(10_000);
	});

	it("keeps desktop scrollback disabled to prevent stale-frame wheel scrolling", () => {
		expect(getTerminalScrollbackLimit(false)).toBe(0);
	});

	it("rejects stale WebSocket callbacks after a replacement connection", () => {
		const current = {} as WebSocket;
		const stale = {} as WebSocket;

		expect(isCurrentWebSocket(current, current)).toBe(true);
		expect(isCurrentWebSocket(current, stale)).toBe(false);
	});

	it("retries a heartbeat timeout but not a server socket replacement", () => {
		expect(shouldRetryWebSocketClose(4000, "Heartbeat timeout")).toBe(true);
		expect(shouldRetryWebSocketClose(4000, "New connection established")).toBe(
			false,
		);
	});
});
