chore: 保存 Agent 安全优化前基线

This commit is contained in:
Logan.Li committed 2026-09-02 23:31:38 +08:00
1 parent 08696896f9
commit ff016347cd
116 files changed
+3858 -2485

No files matched your search

+39 -10
View File
@@ -2,7 +2,7 @@
* TabStore Tests - 标签页状态管理测试
*/
import { describe, it, expect, beforeEach } from 'vitest'
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { useTabStore } from '../../src/stores/tabStore'
describe('TabStore', () => {
@@ -31,7 +31,7 @@ describe('TabStore', () => {
const newTabs = useTabStore.getState().tabs
expect(newTabs).toHaveLength(tabs.length + 1)
expect(newTabs.find(t => t.type === 'terminal')).toBeDefined()
expect(newTabs.find((t) => t.type === 'terminal')).toBeDefined()
})
it('添加新标签后应该激活它', () => {
@@ -39,7 +39,7 @@ describe('TabStore', () => {
addTab({ title: 'Terminal', type: 'terminal' })
const { activeTabId, tabs } = useTabStore.getState()
const activeTab = tabs.find(t => t.id === activeTabId)
const activeTab = tabs.find((t) => t.id === activeTabId)
expect(activeTab?.type).toBe('terminal')
})
@@ -104,12 +104,12 @@ describe('TabStore', () => {
addTab({ title: 'Tab 2', type: 'sftp' })
const { tabs } = useTabStore.getState()
const tabToClose = tabs.find(t => t.title === 'Tab 1')
const tabToClose = tabs.find((t) => t.title === 'Tab 1')
if (tabToClose) {
closeTab(tabToClose.id)
const { tabs: newTabs } = useTabStore.getState()
expect(newTabs.find(t => t.isActive)?.title).toBe('Tab 2')
expect(newTabs.find((t) => t.isActive)?.title).toBe('Tab 2')
}
})
@@ -118,7 +118,7 @@ describe('TabStore', () => {
addTab({ title: 'Tab 1', type: 'terminal' })
const { tabs } = useTabStore.getState()
const tabToClose = tabs.find(t => t.title === 'Tab 1')
const tabToClose = tabs.find((t) => t.title === 'Tab 1')
if (tabToClose) {
const closed = closeTab(tabToClose.id)
@@ -133,7 +133,7 @@ describe('TabStore', () => {
addTab({ title: 'Tab 1', type: 'terminal' })
addTab({ title: 'Tab 2', type: 'sftp' })
const tabToKeep = useTabStore.getState().tabs.find(t => t.title === 'Tab 1')
const tabToKeep = useTabStore.getState().tabs.find((t) => t.title === 'Tab 1')
if (tabToKeep) {
closeOtherTabs(tabToKeep.id)
const { tabs: newTabs } = useTabStore.getState()
@@ -150,12 +150,12 @@ describe('TabStore', () => {
addTab({ title: 'Tab 2', type: 'sftp' })
const { tabs } = useTabStore.getState()
const tabToActivate = tabs.find(t => t.title === 'Tab 1')
const tabToActivate = tabs.find((t) => t.title === 'Tab 1')
if (tabToActivate) {
activateTab(tabToActivate.id)
const { tabs: newTabs, activeTabId } = useTabStore.getState()
const activeTab = newTabs.find(t => t.id === activeTabId)
const activeTab = newTabs.find((t) => t.id === activeTabId)
expect(activeTab?.title).toBe('Tab 1')
}
})
@@ -167,7 +167,7 @@ describe('TabStore', () => {
addTab({ title: 'Test Tab', type: 'terminal' })
const { tabs } = useTabStore.getState()
const testTab = tabs.find(t => t.title === 'Test Tab')
const testTab = tabs.find((t) => t.title === 'Test Tab')
if (testTab) {
const found = getTabById(testTab.id)
@@ -191,4 +191,33 @@ describe('TabStore', () => {
expect(tabs[0].title).toBe('Updated Welcome')
})
})
describe('持久化恢复', () => {
afterEach(() => {
localStorage.removeItem('opsxshell-tabs')
})
const seedTabs = (tabs: unknown[]) => {
localStorage.setItem('opsxshell-tabs', JSON.stringify({ state: { tabs, activeTabId: null }, version: 0 }))
}
it('SSH 标签恢复后仍需手动重连', async () => {
seedTabs([{ id: 'ssh-1', title: 'user@host', type: 'terminal', sessionId: 's1' }])
await useTabStore.persist.rehydrate()
const tab = useTabStore.getState().tabs.find((t) => t.id === 'ssh-1')
expect(tab?.needsReconnect).toBe(true)
})
it('本地终端标签恢复后直接拉起 Shell,不弹重连横幅', async () => {
seedTabs([{ id: 'local-1', title: 'Bash', type: 'terminal', isLocal: true, localShellPath: '/bin/bash' }])
await useTabStore.persist.rehydrate()
const tab = useTabStore.getState().tabs.find((t) => t.id === 'local-1')
expect(tab?.needsReconnect).toBe(false)
expect(tab?.localShellPath).toBe('/bin/bash')
})
})
})