Files
accounted/app/api/user/ui-state/__tests__/route.test.ts
T
MattssonandClaude Fable 5 b4b7549004 feat(agent): resizable, undockable assistant panel (#1467)
* feat(agent): resizable, undockable assistant panel

User report: the assistant chat sheet sometimes covers the page content
the user is asking about, with no way to resize or move it.

- Docked mode is now drag-resizable from its left edge (380-800px,
  clamped so the page keeps a 480px readable column) and the page
  reflows beside it via the existing --agent-dock-w reservation.
- Expanded (focus) mode reserves page margin like the compact dock
  instead of overlaying up to 1100px of the page.
- New undock toggle turns the sheet into a floating window that can be
  dragged by its header and resized from edges/corners, clamped so the
  header always stays reachable. Desktop only; mobile keeps the
  full-screen sheet.
- Geometry (mode, dock width, float rect) persists per user in
  user_preferences.ui_state.agent_panel, server-seeded to avoid a
  first-paint jump; the ui-state API schema gains a strict agent_panel
  key with nested merge.
- Pure clamp/resize math lives in lib/agent-panel/geometry with unit
  tests; drag frames write styles imperatively and commit one
  preference update on release.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(agent): address review findings on panel drag, a11y, and persistence

CodeRabbit round 1, all six findings fixed:

- Bind drag listeners to window (plus lostpointercapture) so a failed
  pointer capture or mid-drag unmount can never leave the transition
  suppression and data-agent-resizing stuck for the session.
- Keyboard resize now steps from the visible width (expandedW in focus
  mode) instead of jumping to the persisted dock width.
- The width handle exposes window-splitter semantics: aria-valuenow,
  aria-valuemin, aria-valuemax.
- --nav-w is read reactively via a MutationObserver on #dash-shell
  instead of computed-style reads in the render body and per drag frame.
- The ui-state POST in updatePanelPrefs gets a 300ms trailing debounce
  (state stays immediate) so key auto-repeat cannot produce one
  read-merge-write per repeat; pending write flushes on unmount.
- globals.css keeps one :root token block; the agent-resizing rule moved
  below it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(agent): filter drag events by pointer id, clear fired debounce timer

CodeRabbit round 2, both findings fixed:

- Window-level drag listeners now ignore events from pointers other than
  the initiating one, so a second touch or pen cannot move the panel or
  end the first pointer's drag.
- The persist debounce timer ref is nulled when the timer fires, so the
  unmount flush only writes genuinely pending values instead of
  replaying an already-persisted (possibly stale) geometry.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-08 16:13:02 +02:00

157 lines
4.8 KiB
TypeScript

/**
* Tests for POST /api/user/ui-state: the per-user UI preference bag
* (nav collapse/fold state, split-button create modes).
*/
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { NextResponse } from 'next/server'
import { createQueuedMockSupabase, createMockRequest, parseJsonResponse } from '@/tests/helpers'
const { supabase, enqueue, reset } = createQueuedMockSupabase()
const requireAuthMock = vi.fn()
vi.mock('@/lib/auth/require-auth', () => ({
requireAuth: (...args: unknown[]) => requireAuthMock(...args),
}))
import { POST } from '../route'
beforeEach(() => {
vi.clearAllMocks()
reset()
requireAuthMock.mockResolvedValue({ user: { id: 'user-1' }, supabase, error: null })
})
function request(body: unknown) {
return createMockRequest('/api/user/ui-state', { method: 'POST', body })
}
describe('POST /api/user/ui-state', () => {
it('returns 401 when not authenticated', async () => {
requireAuthMock.mockResolvedValue({
user: null,
supabase,
error: NextResponse.json({ error: 'Unauthorized' }, { status: 401 }),
})
const res = await POST(request({ nav_collapsed: true }))
expect(res.status).toBe(401)
})
it('returns 400 on unknown keys (strict schema)', async () => {
const res = await POST(request({ nav_collapsed: true, evil: 'x' }))
expect(res.status).toBe(400)
})
it('returns 400 on wrong value types', async () => {
const res = await POST(request({ nav_collapsed: 'yes' }))
expect(res.status).toBe(400)
})
it('returns 400 on unknown agent_panel keys (strict schema)', async () => {
const res = await POST(request({ agent_panel: { mode: 'docked', evil: 1 } }))
expect(res.status).toBe(400)
})
it('returns 400 on an invalid agent_panel mode', async () => {
const res = await POST(request({ agent_panel: { mode: 'popup' } }))
expect(res.status).toBe(400)
})
it('returns 400 on fractional float pixels', async () => {
const res = await POST(
request({ agent_panel: { float: { x: 10.5, y: 0, w: 400, h: 500 } } }),
)
expect(res.status).toBe(400)
})
it('returns 400 on an incomplete float rect', async () => {
const res = await POST(request({ agent_panel: { float: { x: 10, y: 0, w: 400 } } }))
expect(res.status).toBe(400)
})
it('merges agent_panel keys instead of replacing the object', async () => {
enqueue({
data: {
ui_state: {
agent_panel: { mode: 'docked', dock_width: 620 },
},
},
})
enqueue({ data: null })
const { status, body } = await parseJsonResponse<{
data: { ui_state: { agent_panel: Record<string, unknown> } }
}>(await POST(request({ agent_panel: { mode: 'floating' } })))
expect(status).toBe(200)
// dock_width survives a mode-only patch: undocking must not forget the
// user's chosen docked width.
expect(body.data.ui_state.agent_panel).toEqual({ mode: 'floating', dock_width: 620 })
})
it('accepts a full agent_panel geometry payload', async () => {
enqueue({ data: null })
enqueue({ data: null })
const { status, body } = await parseJsonResponse<{
data: { ui_state: { agent_panel: Record<string, unknown> } }
}>(
await POST(
request({
agent_panel: {
mode: 'floating',
dock_width: 480,
float: { x: 1200, y: 300, w: 420, h: 640 },
},
}),
),
)
expect(status).toBe(200)
expect(body.data.ui_state.agent_panel).toEqual({
mode: 'floating',
dock_width: 480,
float: { x: 1200, y: 300, w: 420, h: 640 },
})
})
it('merges the patch into the existing ui_state', async () => {
// select existing row
enqueue({
data: { ui_state: { nav_collapsed: false, nav_folds: { register: true } } },
})
// upsert result
enqueue({ data: null })
const { status, body } = await parseJsonResponse<{
data: { ui_state: { nav_collapsed: boolean; nav_folds: Record<string, boolean> } }
}>(await POST(request({ nav_folds: { bokslut: true } })))
expect(status).toBe(200)
expect(body.data.ui_state).toEqual({
nav_collapsed: false,
nav_folds: { register: true, bokslut: true },
})
})
it('handles a missing preferences row (first write)', async () => {
enqueue({ data: null }) // no existing row
enqueue({ data: null }) // upsert
const { status, body } = await parseJsonResponse<{
data: { ui_state: { nav_collapsed: boolean } }
}>(await POST(request({ nav_collapsed: true })))
expect(status).toBe(200)
expect(body.data.ui_state).toEqual({ nav_collapsed: true })
})
it('returns 500 when the upsert fails', async () => {
enqueue({ data: null })
enqueue({ data: null, error: { message: 'boom' } })
const res = await POST(request({ nav_collapsed: true }))
expect(res.status).toBe(500)
})
})