Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

7.6 KB
Raw Blame
// Runs under `claude plugin test` against the built bundle (npm run test:mod).
// django-feed.ts is generated by scripts/mod-test.mjs from the recorded
// fixtures in test/fixtures, so the map draws real Django data.
import { expect, test } from 'claude-code/testing'
import { BLAST_RADIUS, HEALTH_MAP } from './django-feed'

const ROOT = 'C:\\work\\django'
const SHA = 'e78991410b78391bc07a4b5010b273dfec814ff6'
const LOCK = { pid: 4242, host: '127.0.0.1', port: 7411, url: 'http://127.0.0.1:7411', ui_port: null, server_version: '0.54.0', started_at: '2026-10-03T09:00:00Z' }
const PANE = {
  plugin: 'repowise',
  component: 'Pane',
  surface: 'terminal',
  requestId: 'lens',
  viewport: { columns: 240, rows: 60 },
  props: { title: 'Lens', isFocused: false, bodyColumns: 180, placement: 'dock', scroll: { offset: 0, bodyRows: 55 }, view: {} },
} as const

const ok = (body: unknown) => ({ value: { status: 200, ok: true, headers: {}, text: JSON.stringify(body) } })
const run = (stdout: string) => ({ value: { exitCode: 0, stdout, stderr: '' } })

/** The local server's answers by path suffix, recorded from Django. */
const ROUTES: [RegExp, unknown][] = [
  [/\/health$/, { status: 'healthy' }],
  [/\/api\/repos$/, [{ id: 'dj', name: 'django', local_path: ROOT, updated_at: null }]],
  [/\/health\/map/, HEALTH_MAP],
  [/\/blast-radius$/, BLAST_RADIUS],
]

/** Files under the repo's .repowise folder. */
const FILES: [RegExp, string][] = [
  [/state\.json$/, JSON.stringify({ last_sync_commit: SHA })],
  [/serve\.lock\.json$/, JSON.stringify(LOCK)],
]

function answer(url: string) {
  const route = ROUTES.find(([pattern]) => pattern.test(url))
  return route ? ok(route[1]) : { value: { status: 404, ok: false, headers: {}, text: '{}' } }
}

function readFile(path: string) {
  const file = FILES.find(([pattern]) => pattern.test(path))
  return file ? { value: file[1] } : { deny: 'ENOENT' }
}

/** tasklist sees the server's pid; git says HEAD is the indexed commit. */
function processRun(argv: readonly string[]) {
  if (argv[0] === 'tasklist') return run('"python.exe","4242","Console"\r\n')
  return run(argv.includes('HEAD') ? `${SHA}\n` : 'true\n')
}

/** An indexed Django copy whose local server is up, answering from the recorded payloads. */
function fullStubs(on: any, seen: { urls: string[]; blits: any[]; opened: any[] }): void {
  on('session.start', () => ({ cwd: ROOT }))
  on('session.cwd', () => ({ value: ROOT }))
  on('fs.exists', (_$: any, e: any) => ({ value: FILES.some(([pattern]) => pattern.test(e.path)) }))
  on('fs.read', (_$: any, e: any) => readFile(e.path))
  on('process.run', (_$: any, e: any) => processRun(e.argv))
  on('http.fetch', (_$: any, e: any) => (seen.urls.push(e.url), answer(e.url)))
  on('settings.read', () => ({ value: {} }))
  on('config.list', () => ({ value: [{ key: 'theme', value: 'dark' }] }))
  on('ui.open', (_$: any, e: any) => (seen.opened.push(e), { value: { isPlaced: true } }))
  on('ui.blit', (_$: any, e: any) => (seen.blits.push(e), { value: {} }))
  on('tool.call', () => ({ result: {} }))
}

async function waitFor(check: () => boolean | Promise<boolean>): Promise<boolean> {
  for (let i = 0; i < 80; i++) {
    if (await check()) return true
    await new Promise((resolve) => setTimeout(resolve, 25))
  }
  return false
}

async function openMap($: any, on: any) {
  const seen = { urls: [] as string[], blits: [] as any[], opened: [] as any[] }
  fullStubs(on, seen)
  await $.session.start({ surface: 'terminal', isInteractive: true, cwd: ROOT })
  expect(await waitFor(() => seen.urls.some((u) => u.endsWith('/api/repos')))).toBe(true)
  await $.command.run({ command: 'lens', args: 'map' })
  const ui = await $.ui.mount(PANE)
  expect(await waitFor(async () => (await ui.find({ key: 'lens-map' })) !== undefined)).toBe(true)
  return { seen, ui }
}

test('/lens map draws Django as quiet tiles, says what will light up, and offers health colours', async ($: any, on: any) => {
  const { seen, ui } = await openMap($, on)
  expect(seen.opened).toEqual([{ id: 'lens', title: 'Lens', rows: 29, focus: true }])
  expect(seen.urls.filter((u) => u.includes('/health/map?cap=4000'))).toHaveLength(1)
  const map = await ui.find({ key: 'lens-map' })
  expect(map?.type).toBe('Raster')
  expect(map?.props.columns).toBe(180)
  expect(map?.props.rows).toBe(45)
  expect(String(map?.props.cells).length).toBe(180 * 45 * 16)
  expect(await ui.find({ type: 'Text', text: /^1,\d{3} of 2,970 files drawn at this size/ })).toBeDefined()
  expect(await ui.find({ type: 'Text', text: /^Nothing lit yet this turn/ })).toBeDefined()
  expect(await ui.find({ type: 'Text', text: ' Needs work' })).toBeUndefined()
  await ui.press({ key: 'lens-map-health' })
  await ui.unmount()
  const health = await $.ui.mount(PANE)
  expect(await health.find({ type: 'Text', text: ' Needs work' })).toBeDefined()
  await health.unmount()
})

test('an edit ripples to the importers the server returned, by blit', async ($: any, on: any) => {
  const { seen, ui } = await openMap($, on)
  await $.tool.call({ tool: 'Edit', tool_use_id: 'toolu_e1', file_path: `${ROOT}\\django\\db\\models\\query.py`, old_string: 'a', new_string: 'a\nb' })
  expect(await waitFor(() => seen.blits.length >= 2)).toBe(true)
  expect(seen.blits[0]).toMatchObject({ requestId: 'lens', key: 'lens-map', columns: 180, rows: 45 })
  const fetched = BLAST_RADIUS.transitive_affected.length
  await ui.unmount()
  const after = await $.ui.mount(PANE)
  expect(await after.find({ type: 'Text', text: '◉ query.py +1 line' })).toBeDefined()
  expect(await after.find({ type: 'Text', text: new RegExp(`^${fetched} importers: `) })).toBeDefined()
  // The inspector: j selects the edit and says what it is; z zooms into its folder.
  await after.press({ key: 'lens-map-next' })
  await after.unmount()
  const picked = await $.ui.mount(PANE)
  expect(await picked.find({ type: 'Text', text: /^django\/db\/models\/query\.py · Claude edited it, \+1 line/ })).toBeDefined()
  await picked.press({ key: 'lens-map-zoom' })
  await picked.unmount()
  const zoomed = await $.ui.mount(PANE)
  expect(await zoomed.find({ type: 'Text', text: /^django \/ db/ })).toBeDefined()
  await zoomed.unmount()
})

test('without the local server the pane says what to run', async ($: any, on: any) => {
  on('session.start', () => ({ cwd: '/work' }))
  on('session.cwd', () => ({ value: '/work' }))
  on('fs.exists', (_$: any, e: any) => ({ value: e.path.endsWith('state.json') }))
  on('fs.read', () => ({ deny: 'ENOENT' }))
  on('process.run', () => run('true\n'))
  on('mcp.connect', () => ({ value: { isConnected: true, server: 'plugin:repowise:repowise' } }))
  on('settings.read', () => ({ value: {} }))
  on('ui.open', () => ({ value: { isPlaced: true } }))
  await $.session.start({ surface: 'terminal', isInteractive: true, cwd: '/work' })
  await $.command.run({ command: 'lens', args: 'map' })
  const hint = 'Lens map needs the local server: repowise serve --no-ui'
  expect(
    await waitFor(async () => {
      const ui = await $.ui.mount(PANE)
      const found = await ui.find({ type: 'Text', text: hint })
      await ui.unmount()
      return found !== undefined
    }),
  ).toBe(true)
})

test('the desktop app draws the same Django map as SVG, under the element limit', async ($: any, on: any) => {
  const { ui } = await openMap($, on)
  await ui.unmount()
  const desktop = await $.ui.mount({ ...PANE, surface: 'desktop' })
  const drawing = await desktop.find({ type: 'Svg' })
  const source = String(drawing?.props?.source ?? '')
  expect(source.startsWith('<svg ')).toBe(true)
  expect(source.length).toBeLessThan(131072)
  expect(await desktop.find({ type: 'Text', text: / of 2,970 files drawn at this size/ })).toBeDefined()
  await desktop.unmount()
})