Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

10.8 KB
Raw Blame
import assert from "node:assert/strict";
import test from "node:test";
import {
  WORK_PANEL_CHAT_MAX_WIDTH,
  WORK_PANEL_CHAT_MIN_WIDTH,
  MAIN_PANE_MIN_WIDTH,
  MAIN_PANE_REOPEN_TARGET_WIDTH,
  WORK_PANEL_DEFAULT_WIDTH,
  WORK_PANEL_COMPACT_MIN_WIDTH,
  WORK_PANEL_MIN_WIDTH,
  clampWorkPanelChatWidth,
  clampWorkPanelWidth,
  committedWorkPanelChatWidth,
  parseWorkPanelChatWidth,
  workPanelLayout,
  workPanelResetWidth,
  workPanelWidthBounds,
  workPanelChatWidthFromPointer,
  workPanelGestureMinimum,
  workPanelRequestedWidth,
  workPanelWidthForSidebarReopen,
} from "../src/lib/work-panel-resize.ts";

test("the three-column budget protects MainChat and collapses the sidebar at the threshold", () => {
  assert.equal(MAIN_PANE_MIN_WIDTH, 450);
  assert.equal(MAIN_PANE_REOPEN_TARGET_WIDTH, 460);
  const layout = workPanelLayout({
    containerWidth: 1040,
    sidebarWidth: 275,
    sidebarCollapsed: false,
    requestedPanelWidth: 720,
  });

  assert.equal(layout.maxPanelWidth, 315);
  assert.equal(layout.panelWidth, 315);
  assert.equal(layout.mainWidth, MAIN_PANE_MIN_WIDTH);
  assert.equal(layout.shouldCollapseSidebar, true);
});

test("the expanded sidebar collapses as soon as MainChat reaches 450px", () => {
  const justAbove = workPanelLayout({
    containerWidth: 1040,
    sidebarWidth: 275,
    sidebarCollapsed: false,
    requestedPanelWidth: 314,
  });
  const atFloor = workPanelLayout({
    containerWidth: 1040,
    sidebarWidth: 275,
    sidebarCollapsed: false,
    requestedPanelWidth: 315,
  });

  assert.equal(justAbove.mainWidth, 451);
  assert.equal(justAbove.shouldCollapseSidebar, false);
  assert.equal(atFloor.mainWidth, MAIN_PANE_MIN_WIDTH);
  assert.equal(atFloor.shouldCollapseSidebar, true);
});

test("a collapsed sidebar exposes the full dynamic right-column budget", () => {
  const layout = workPanelLayout({
    containerWidth: 1040,
    sidebarWidth: 275,
    sidebarCollapsed: true,
    requestedPanelWidth: 720,
  });

  assert.equal(layout.maxPanelWidth, 590);
  assert.equal(layout.panelWidth, 590);
  assert.equal(layout.mainWidth, MAIN_PANE_MIN_WIDTH);
  assert.equal(layout.shouldCollapseSidebar, false);
});

test("sidebar reopen spends right-panel width before using the 460px target", () => {
  assert.equal(
    workPanelWidthForSidebarReopen({
      containerWidth: 1040,
      sidebarWidth: 275,
      currentPanelWidth: 600,
    }),
    305,
  );
  assert.equal(
    workPanelWidthForSidebarReopen({
      containerWidth: 1040,
      sidebarWidth: 520,
      currentPanelWidth: 360,
    }),
    60,
  );
  assert.equal(
    workPanelWidthForSidebarReopen({
      containerWidth: 2000,
      sidebarWidth: 275,
      currentPanelWidth: 360,
    }),
    360,
  );
});

test("the compact width stays representable for the reopen path", () => {
  assert.equal(
    workPanelWidthForSidebarReopen({
      containerWidth: 1040,
      sidebarWidth: 800,
      currentPanelWidth: WORK_PANEL_MIN_WIDTH,
    }),
    WORK_PANEL_COMPACT_MIN_WIDTH,
  );
  assert.equal(
    clampWorkPanelWidth(120, WORK_PANEL_COMPACT_MIN_WIDTH),
    120,
  );
  assert.equal(clampWorkPanelWidth(0, WORK_PANEL_COMPACT_MIN_WIDTH), WORK_PANEL_COMPACT_MIN_WIDTH);
});

// The reported sequence: an 866px shell, a 275px sidebar and a 360px panel.
const narrowShell = { containerWidth: 866, sidebarWidth: 275 };
const panelLayout = (sidebarCollapsed, yieldedWidth, containerWidth = 866) =>
  workPanelLayout({
    ...narrowShell,
    containerWidth,
    sidebarCollapsed,
    requestedPanelWidth: workPanelRequestedWidth({
      ...narrowShell,
      containerWidth,
      sidebarCollapsed,
      preferredWidth: 360,
      yieldedWidth,
    }),
  });

test("a narrow sidebar reopen only lends panel width", () => {
  const lent = workPanelWidthForSidebarReopen({
    ...narrowShell,
    currentPanelWidth: 360,
  });
  assert.equal(lent, 131);

  const reopened = panelLayout(false, lent);
  assert.equal(reopened.panelWidth, 131);
  assert.equal(reopened.mainWidth, MAIN_PANE_REOPEN_TARGET_WIDTH);
  assert.equal(reopened.shouldCollapseSidebar, false);

  // Collapsing the sidebar again ends the loan: the panel returns to its
  // preferred width, and resizing keeps the regular minimum.
  const collapsed = panelLayout(true, lent);
  assert.equal(collapsed.panelWidth, 360);
  assert.equal(workPanelGestureMinimum(collapsed.panelWidth), WORK_PANEL_MIN_WIDTH);
});

test("space returning to a reopened sidebar goes back to the panel first", () => {
  assert.equal(panelLayout(false, 131, 1000).panelWidth, 265);
  assert.equal(panelLayout(false, 131, 1000).mainWidth, MAIN_PANE_REOPEN_TARGET_WIDTH);
  assert.equal(panelLayout(false, 131, 1200).panelWidth, 360);
  assert.ok(panelLayout(false, 131, 1200).mainWidth > MAIN_PANE_REOPEN_TARGET_WIDTH);

  // A narrower window keeps the lent width until it no longer fits beside
  // the 450px floor; then the sidebar yields as usual.
  const narrower = panelLayout(false, 131, 860);
  assert.equal(narrower.panelWidth, 131);
  assert.equal(narrower.shouldCollapseSidebar, false);
  assert.equal(panelLayout(false, 131, 856).shouldCollapseSidebar, true);
});

test("without a loan the panel asks for its preferred width", () => {
  assert.equal(
    workPanelRequestedWidth({
      ...narrowShell,
      sidebarCollapsed: false,
      preferredWidth: 360,
      yieldedWidth: null,
    }),
    360,
  );
});

test("a resize gesture never starts below its regular minimum or jumps", () => {
  assert.equal(workPanelGestureMinimum(360), WORK_PANEL_MIN_WIDTH);
  assert.equal(workPanelGestureMinimum(WORK_PANEL_MIN_WIDTH), WORK_PANEL_MIN_WIDTH);
  // A panel shown narrower than the minimum cannot be dragged narrower.
  assert.equal(workPanelGestureMinimum(131), 131);
  assert.equal(workPanelGestureMinimum(0), WORK_PANEL_COMPACT_MIN_WIDTH);
});

test("a wide window lets the panel grow past the old fixed cap", () => {
  // Regression guard: the panel maximum is the live budget, so a 1600px shell
  // must allow 875px (1600 - 275 sidebar - 450 floor) rather than stopping at
  // a fixed 720px cap.
  const wide = workPanelLayout({
    containerWidth: 1600,
    sidebarWidth: 275,
    sidebarCollapsed: false,
    requestedPanelWidth: 1200,
  });
  assert.equal(wide.maxPanelWidth, 875);
  assert.equal(wide.panelWidth, 875);
  assert.equal(wide.mainWidth, MAIN_PANE_MIN_WIDTH);
  assert.equal(wide.shouldCollapseSidebar, true);

  // The same window with the sidebar already yielded spends its width on the
  // panel down to the floor.
  const yielded = workPanelLayout({
    containerWidth: 1600,
    sidebarWidth: 275,
    sidebarCollapsed: true,
    requestedPanelWidth: 1500,
  });
  assert.equal(yielded.maxPanelWidth, 1150);
  assert.equal(yielded.panelWidth, 1150);
  assert.equal(yielded.mainWidth, MAIN_PANE_MIN_WIDTH);
});

test("preview mode gives the panel the whole client area beside the sidebar", () => {
  const expanded = workPanelLayout({
    containerWidth: 1200,
    sidebarWidth: 275,
    sidebarCollapsed: false,
    requestedPanelWidth: 500,
    maximized: true,
  });
  assert.equal(expanded.panelWidth, 925);
  assert.equal(expanded.maxPanelWidth, 925);
  assert.equal(expanded.mainWidth, 0);
  assert.equal(expanded.shouldCollapseSidebar, false);

  const collapsed = workPanelLayout({
    containerWidth: 1200,
    sidebarWidth: 275,
    sidebarCollapsed: true,
    requestedPanelWidth: 500,
    maximized: true,
  });
  assert.equal(collapsed.panelWidth, 1200);
  assert.equal(collapsed.mainWidth, 0);
});

test("clamps the work panel to its minimum without a fixed upper bound", () => {
  assert.equal(clampWorkPanelWidth(900), 900);
  assert.equal(clampWorkPanelWidth(500), 500);
  assert.equal(clampWorkPanelWidth(200), WORK_PANEL_MIN_WIDTH);
});

test("clamps the conversation area to its bounded native resize range", () => {
  assert.equal(WORK_PANEL_CHAT_MIN_WIDTH, 800);
  assert.equal(clampWorkPanelChatWidth(700), WORK_PANEL_CHAT_MIN_WIDTH);
  assert.equal(clampWorkPanelChatWidth(900), 900);
  assert.equal(clampWorkPanelChatWidth(1200), 1200);
  assert.equal(clampWorkPanelChatWidth(20000), WORK_PANEL_CHAT_MAX_WIDTH);
});

test("anchors inner-divider resizing to the conversation width at gesture start", () => {
  const gesture = { startClientX: 800, startWidth: 1200 };

  assert.equal(workPanelChatWidthFromPointer(gesture, 800), 1200);
  assert.equal(workPanelChatWidthFromPointer(gesture, 880), 1280);
  assert.equal(workPanelChatWidthFromPointer(gesture, 720), 1120);
});

test("inner-divider resizing respects the conversation limits", () => {
  assert.equal(
    workPanelChatWidthFromPointer(
      { startClientX: 800, startWidth: WORK_PANEL_CHAT_MIN_WIDTH },
      300,
    ),
    WORK_PANEL_CHAT_MIN_WIDTH,
  );
  assert.equal(
    workPanelChatWidthFromPointer(
      { startClientX: 800, startWidth: WORK_PANEL_CHAT_MAX_WIDTH },
      1300,
    ),
    WORK_PANEL_CHAT_MAX_WIDTH,
  );
});

test("commits only a changed conversation preview after a completed gesture", () => {
  const gesture = { startClientX: 800, startWidth: WORK_PANEL_DEFAULT_WIDTH + 920 };

  assert.equal(
    committedWorkPanelChatWidth(gesture, gesture.startWidth, true),
    null,
  );
  assert.equal(committedWorkPanelChatWidth(gesture, 1400, false), null);
  assert.equal(committedWorkPanelChatWidth(gesture, 1400, true), 1400);
});

test("accepts only bounded integer conversation widths over IPC", () => {
  assert.equal(parseWorkPanelChatWidth({ width: 800 }), 800);
  assert.equal(parseWorkPanelChatWidth({ width: 10000 }), 10000);
  assert.equal(parseWorkPanelChatWidth({ width: 799 }), null);
  assert.equal(parseWorkPanelChatWidth({ width: 10000.5 }), null);
  assert.equal(parseWorkPanelChatWidth({ width: "1200" }), null);
  assert.equal(parseWorkPanelChatWidth(null), null);
});

test("double-click resets the panel to its default width", () => {
  assert.equal(WORK_PANEL_DEFAULT_WIDTH, 360);
  assert.equal(
    workPanelResetWidth(WORK_PANEL_MIN_WIDTH, 800),
    WORK_PANEL_DEFAULT_WIDTH,
  );
  assert.equal(
    workPanelResetWidth(WORK_PANEL_COMPACT_MIN_WIDTH, 800),
    WORK_PANEL_DEFAULT_WIDTH,
  );
  assert.equal(
    workPanelResetWidth(WORK_PANEL_MIN_WIDTH, 400),
    WORK_PANEL_DEFAULT_WIDTH,
  );
});

test("a reset stays inside the live bounds instead of breaching MainChat", () => {
  // A narrower budget than the default keeps the capped width.
  assert.equal(workPanelResetWidth(WORK_PANEL_MIN_WIDTH, 300), 300);
  assert.equal(workPanelResetWidth(WORK_PANEL_COMPACT_MIN_WIDTH, 120), 120);
  assert.deepEqual(workPanelWidthBounds(WORK_PANEL_COMPACT_MIN_WIDTH, 120), {
    minimum: WORK_PANEL_COMPACT_MIN_WIDTH,
    maximum: 120,
  });
  // The three-column budget replaces the regular minimum when it is smaller.
  assert.deepEqual(workPanelWidthBounds(WORK_PANEL_MIN_WIDTH, 120), {
    minimum: 120,
    maximum: 120,
  });
  assert.deepEqual(workPanelWidthBounds(WORK_PANEL_MIN_WIDTH, 800), {
    minimum: WORK_PANEL_MIN_WIDTH,
    maximum: 800,
  });
});