@mobius-os/mobius 0.3.66 → 0.3.67

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/tests/ui.test.tsx CHANGED
@@ -19,12 +19,14 @@ import { ChatScreen, Composer, shimmerText } from '../src/components/Chat.js'
19
19
  import { WindowsInputDecoder } from '../src/lib/windows-input.js'
20
20
  import { LoginScreen } from '../src/components/Login.js'
21
21
  import { PrepScreen } from '../src/components/PrepScreen.js'
22
+ import { ReconfigFlow } from '../src/components/ConfigFlow.js'
22
23
  import { Select, TextInput } from '../src/components/primitives.js'
23
24
  import { MobiusClient } from '../src/api.js'
24
25
  import { renderMarkdownLines } from '../src/markdown.js'
25
26
  import { dedupeUserEntries, isRoundOpenerEntry, viewsForEntry, toolLabel } from '../src/lib/entry-view.js'
26
27
  import { SseConnection } from '../src/sse.js'
27
28
  import type { ReadyState } from '../src/components/PrepScreen.js'
29
+ import { setTuiGuiMode } from '../src/aimux.js'
28
30
 
29
31
  const delay = (ms: number) => new Promise<void>((r) => setTimeout(r, ms))
30
32
  let pass = 0, fail = 0
@@ -459,6 +461,192 @@ async function testPrepSearch() {
459
461
  try { fs.rmSync(path.join(TMP_HOME, 'dir2project.json'), { force: true }) } catch { /* ignore */ }
460
462
  } finally { restoreFetch() }
461
463
  }
464
+ // TEST 5c — GUI authorization step adapts to the TUI's own privilege
465
+ // ════════════════════════════════════════════════════════════════════════════
466
+ async function testPrepGuiStep() {
467
+ console.log('\n[UI 5c] GUI authorization step (windows)')
468
+ const client = new MobiusClient('http://mock.local', 'mock-jwt-token')
469
+ installMock((url) => {
470
+ if (url.includes('/api/projects') && !url.includes('/issues') && !url.includes('/skills') && !url.includes('/memories')) {
471
+ return jsonResponse([{ id: 'p9', name: '界面项目' }])
472
+ }
473
+ if (url.includes('/issues')) return jsonResponse([{ id: 'i9', project_id: 'p9', title: '授权任务' }])
474
+ if (url.includes('/sessions/model-options')) return jsonResponse([])
475
+ if (url.includes('/sessions/default-model')) return jsonResponse({ model: 'codex' })
476
+ if (url.includes('/memories')) return jsonResponse([])
477
+ if (url.includes('/skills')) return jsonResponse([])
478
+ return jsonResponse({ error: 'no mock' }, 404)
479
+ })
480
+ // Pre-seed the cwd binding + preferences so the wizard resumes right at the
481
+ // GUI step instead of walking project → issue → model → language first.
482
+ const HOME = process.env.MOBIUS_TUI_HOME as string
483
+ const cwd = process.cwd()
484
+ fs.writeFileSync(path.join(HOME, 'dir2project.json'), JSON.stringify({ [cwd]: 'p9' }))
485
+ const seedPrefs = () => fs.writeFileSync(path.join(HOME, 'dir2project_preference.json'), JSON.stringify({
486
+ [cwd]: { issueId: 'i9', prefs: { i9: { excluded_skill_ids: [], excluded_memory_ids: [], done: ['model', 'language'] } } },
487
+ }))
488
+ seedPrefs()
489
+ // No file at all means no choice has ever been made, i.e. 'off'.
490
+ const readMode = (): string => {
491
+ try { return JSON.parse(fs.readFileSync(path.join(HOME, 'tui-gui-authorized.json'), 'utf8')).mode } catch { return 'off' }
492
+ }
493
+ try {
494
+ // ── an ordinary (unelevated) terminal ────────────────────────────────────
495
+ const { lastFrame, stdin, unmount } = render(<PrepScreen client={client} onReady={() => {}} platform="win32" elevated={false} />)
496
+ await delay(160)
497
+ let frame = lastFrame() ?? ''
498
+ ok(frame.includes('使用纯命令行操作(推荐)'), 'option 1: command line only, marked recommended')
499
+ ok(frame.includes('授权使用图形界面(低权限)'), 'option 2 is offered')
500
+ ok(frame.includes('授权使用图形界面(高权限 - 不可用,请用管理员模式打开 Mobius TUI)'),
501
+ 'option 3 is shown but unavailable, and says how to get it')
502
+ // The unavailable row is shown yet unreachable: Down from option 2 wraps
503
+ // past it back to option 1 rather than landing on it.
504
+ stdin.write('\x1b[B'); await delay(20)
505
+ ok((lastFrame() ?? '').includes('❯ 授权使用图形界面(低权限)'), 'option 2 is reachable')
506
+ ok((lastFrame() ?? '').includes('不弹 UAC'), 'option 2 advertises that it asks Windows for nothing')
507
+ stdin.write('\x1b[B'); await delay(20)
508
+ ok((lastFrame() ?? '').includes('❯ 使用纯命令行操作(推荐)'), 'the focus steps over the unavailable option')
509
+ stdin.write('\r'); await delay(120)
510
+ ok(!(lastFrame() ?? '').includes('使用纯命令行操作(推荐)'), 'choosing command line advances past the step')
511
+
512
+ seedPrefs()
513
+ const low = render(<PrepScreen client={client} onReady={() => {}} platform="win32" elevated={false} />)
514
+ await delay(160)
515
+ low.stdin.write('\x1b[B'); await delay(20)
516
+ low.stdin.write('\r'); await delay(140)
517
+ ok(readMode() === 'no-elevate', 'low privilege is what an unelevated TUI stores')
518
+ ok(!(low.lastFrame() ?? '').includes('授权使用图形界面(低权限)'), 'and the wizard moves on past the step')
519
+ low.unmount()
520
+ unmount()
521
+
522
+ // ── an already-elevated terminal ─────────────────────────────────────────
523
+ seedPrefs()
524
+ const adm = render(<PrepScreen client={client} onReady={() => {}} platform="win32" elevated={true} />)
525
+ await delay(160)
526
+ frame = adm.lastFrame() ?? ''
527
+ ok(frame.includes('授权使用图形界面(低权限 - 不可用,请用非管理员模式打开 Mobius TUI)'),
528
+ 'an elevated TUI cannot offer low privilege, and says why')
529
+ ok(frame.includes('授权使用图形界面(高权限)'), 'high privilege is plain available')
530
+ adm.stdin.write('\x1b[B'); await delay(20)
531
+ ok((adm.lastFrame() ?? '').includes('❯ 授权使用图形界面(高权限)'), 'the focus steps over the unavailable low-privilege row')
532
+ ok(!(adm.lastFrame() ?? '').includes('❯ 授权使用图形界面(低权限)'), 'and never lands on it')
533
+ adm.stdin.write('\r'); await delay(140)
534
+ ok(readMode() === 'elevate', 'high privilege is stored')
535
+ adm.unmount()
536
+
537
+ // ── macOS: no elevation at all, so no middle row ─────────────────────────
538
+ seedPrefs()
539
+ const mac = render(<PrepScreen client={client} onReady={() => {}} platform="darwin" elevated={false} />)
540
+ await delay(160)
541
+ frame = mac.lastFrame() ?? ''
542
+ ok(frame.includes('授权使用图形界面(高权限)'), 'macOS still sees the GUI step')
543
+ ok(!frame.includes('(低权限'), 'macOS is offered no low/high distinction')
544
+ mac.unmount()
545
+ } finally { restoreFetch() }
546
+
547
+ for (const f of ['dir2project.json', 'dir2project_preference.json', 'tui-gui-authorized.json']) {
548
+ try { fs.rmSync(path.join(HOME, f), { force: true }) } catch { /* ignore */ }
549
+ }
550
+ }
551
+
552
+
553
+ // ════════════════════════════════════════════════════════════════════════════
554
+ // TEST 5d — /config walks through the GUI authorization again
555
+ // ════════════════════════════════════════════════════════════════════════════
556
+ async function testConfigGuiStep() {
557
+ console.log('\n[UI 5d] /config can revisit the GUI authorization')
558
+ const client = new MobiusClient('http://mock.local', 'mock-jwt-token')
559
+ let sessionBody: any = null
560
+ installMock((url, init) => {
561
+ if (url.includes('/api/projects/p1/issues')) return jsonResponse([{ id: 'i1', project_id: 'p1', title: '配置任务' }])
562
+ if (url.includes('/api/projects')) return jsonResponse([{ id: 'p1', name: '配置项目' }])
563
+ if (url.includes('/api/sessions/model-options')) return jsonResponse([{ key: 'm1', label: '模型一', sub: '' }])
564
+ if (url.includes('/api/sessions/default-model')) return jsonResponse({ model: 'm1' })
565
+ if (url.includes('/api/issues/i1/sessions') && init?.method === 'POST') {
566
+ sessionBody = JSON.parse(String(init.body))
567
+ return jsonResponse({ session_id: 's-config' })
568
+ }
569
+ return jsonResponse({ error: 'no mock' }, 404)
570
+ })
571
+ const HOME = process.env.MOBIUS_TUI_HOME as string
572
+ // No file at all means no choice has ever been made, i.e. 'off'.
573
+ const readMode = (): string => {
574
+ try { return JSON.parse(fs.readFileSync(path.join(HOME, 'tui-gui-authorized.json'), 'utf8')).mode } catch { return 'off' }
575
+ }
576
+ let done = 0
577
+ // The mode cache is process-global, so pin it (and the file) to a known value
578
+ // before the flow, otherwise "unchanged" skips the write this test inspects.
579
+ setTuiGuiMode('off')
580
+ try {
581
+ const { lastFrame, stdin, unmount } = render(<ReconfigFlow client={client} onDone={() => { done += 1 }} onCancel={() => {}} platform="win32" elevated={false} />)
582
+ // Wait on the loaded rows, not the step titles: the titles render while the
583
+ // list is still fetching, and an Enter sent then goes nowhere.
584
+ ok(await waitFor(lastFrame, '配置项目'), 'config flow shows the project picker')
585
+ stdin.write('\r')
586
+ ok(await waitFor(lastFrame, '配置任务'), 'reaches the issue step')
587
+ stdin.write('\r')
588
+ ok(await waitFor(lastFrame, '模型一'), 'reaches the model step')
589
+ stdin.write('\r')
590
+ ok(await waitFor(lastFrame, 'AIMUX 图形界面授权'), 'the GUI authorization is offered inside /config')
591
+ let frame = lastFrame() ?? ''
592
+ ok(frame.includes('使用纯命令行操作(推荐)') && frame.includes('授权使用图形界面(低权限)'), 'with the same rows as the wizard')
593
+ ok(frame.includes('高权限 - 不可用,请用管理员模式打开 Mobius TUI'), 'and the same availability rule')
594
+
595
+ // Esc goes back to the model list rather than dropping the whole flow.
596
+ stdin.write('\x1b')
597
+ ok(await waitFor(lastFrame, '模型一'), 'Esc returns to the model step')
598
+ // Let the freshly mounted picker attach its input handler before typing.
599
+ await delay(150)
600
+ stdin.write('\r')
601
+ ok(await waitFor(lastFrame, 'AIMUX 图形界面授权'), 'and forward again')
602
+
603
+ await delay(150) // let the picker attach its input handler
604
+ stdin.write('\x1b[B') // → low privilege (the middle row, since it is available here)
605
+ await delay(40)
606
+ stdin.write('\r')
607
+ for (let i = 0; i < 80 && done === 0; i += 1) await delay(25)
608
+ ok(done === 1, 'the flow finishes by creating the session, exactly once')
609
+ ok(readMode() === 'no-elevate', 'and the chosen mode was stored')
610
+ ok(sessionBody?.pc_client_metadata?.gui_authorized === true, 'the session records that GUI was authorized')
611
+ unmount()
612
+ } finally {
613
+ setTuiGuiMode('off')
614
+ restoreFetch()
615
+ }
616
+ for (const f of ['dir2project.json', 'dir2project_preference.json', 'tui-gui-authorized.json']) {
617
+ try { fs.rmSync(path.join(HOME, f), { force: true }) } catch { /* ignore */ }
618
+ }
619
+ }
620
+
621
+ // ════════════════════════════════════════════════════════════════════════════
622
+ // TEST 5e — Select skips unchoosable rows
623
+ // ════════════════════════════════════════════════════════════════════════════
624
+ async function testSelectDisabled() {
625
+ console.log('\n[UI 5e] Select disabled rows')
626
+ let picked = ''
627
+ const { lastFrame, stdin } = render(<Select items={[
628
+ { label: '选项一', value: 'a' },
629
+ { label: '选项二(不可用)', value: 'b', disabled: true },
630
+ { label: '选项三', value: 'c' },
631
+ ]} onSelect={v => { picked = v }} />)
632
+ await delay(30)
633
+ ok((lastFrame() ?? '').includes('❯ 选项一'), 'an enabled first row takes the focus')
634
+ stdin.write('\x1b[B'); await delay(20)
635
+ ok((lastFrame() ?? '').includes('❯ 选项三'), 'Down steps over the disabled row')
636
+ stdin.write('\r'); await delay(20)
637
+ ok(picked === 'c', 'Enter selects the focused enabled row')
638
+ // From the last row, Down wraps past the disabled row back to the first.
639
+ stdin.write('\x1b[B'); await delay(20)
640
+ ok((lastFrame() ?? '').includes('❯ 选项一'), 'Down wraps without landing on the disabled row')
641
+ // A leading disabled row cannot hold the initial focus either.
642
+ const leading = render(<Select items={[
643
+ { label: '不可用', value: 'x', disabled: true },
644
+ { label: '可用', value: 'y' },
645
+ ]} onSelect={() => {}} />)
646
+ await delay(30)
647
+ ok((leading.lastFrame() ?? '').includes('❯ 可用'), 'the initial focus skips a disabled row too')
648
+ leading.unmount()
649
+ }
462
650
 
463
651
  // ════════════════════════════════════════════════════════════════════════════
464
652
  // TEST 6 — Select viewport: a long list must not overflow the terminal
@@ -1299,6 +1487,9 @@ async function main() {
1299
1487
  testFirstUserEntryDedupe()
1300
1488
  await testPrepRender()
1301
1489
  await testPrepSearch()
1490
+ await testPrepGuiStep()
1491
+ await testConfigGuiStep()
1492
+ await testSelectDisabled()
1302
1493
  await testSelectViewport()
1303
1494
  await testProjectPickerEscQuit()
1304
1495
  await testTextInputBackspace()