cmvr-wms/ruoyi-ui-vue3/tests/unit/business-assistant-private-input.test.mjs

44 lines
3.2 KiB
JavaScript
Raw Normal View History

import test from 'node:test'
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { assistantContainsChatPassword, assistantPrivateRows, assistantPrivateValues } from '../../src/utils/assistant-private-input.js'
test('password entries cover only unexecuted rows and never mutate draft data', () => {
const draft = { rows: [{ _rowId: 'a', userName: 'one' }, { _rowId: 'b', userName: 'two' }], results: [{ rowId: 'a', status: 'SUCCEEDED' }] }
const before = structuredClone(draft)
const rows = assistantPrivateRows(draft)
assert.deepEqual(rows, [draft.rows[1]])
assert.equal(assistantPrivateValues(rows, {}), null)
assert.equal(assistantPrivateValues(rows, { b: { password: 'Sample12', repeat: 'Sample13' } }), null)
assert.equal(assistantPrivateValues(rows, { b: { password: 'short', repeat: 'short' } }), null)
assert.deepEqual(assistantPrivateValues(rows, { b: { password: 'Sample12', repeat: 'Sample12' } }), { b: 'Sample12' })
assert.deepEqual(draft, before)
})
test('secure input is transient and cleared on draft, account or visibility changes', () => {
const ui = readFileSync(new URL('../../src/components/BusinessAssistant/index.vue', import.meta.url), 'utf8')
assert.match(ui, /user.id, assistant.open, assistant.draft\?\.id, assistant.draft\?\.version/)
assert.match(ui, /finally \{ privateValues.value = null/)
assert.match(ui, /showPrivateInputs.value, user.token, visible.value/, 'Locking the screen, hiding the panel or changing login token must clear private inputs')
const secure = readFileSync(new URL('../../src/components/BusinessAssistant/AssistantSecureInputs.vue', import.meta.url), 'utf8')
assert.doesNotMatch(secure, /localStorage|sessionStorage|useBusinessAssistantStore/)
assert.match(secure, /type="password"/)
const api = readFileSync(new URL('../../src/api/warehouse/assistant.js', import.meta.url), 'utf8')
assert.match(api, /executeAssistantDraft[^\n]+headers: \{ repeatSubmit: false \}/, 'Password request must bypass the shared sessionStorage duplicate-body cache; server command lock provides idempotency')
})
test('system-initial-password creation visibly explains the policy without forcing per-row secret inputs', () => {
const ui = readFileSync(new URL('../../src/components/BusinessAssistant/index.vue', import.meta.url), 'utf8')
assert.match(ui, /currentAction\?\.passwordMode === 'SYSTEM_INITIAL'/)
assert.match(ui, /:title="currentAction.passwordNotice"/)
assert.match(ui, /currentAction.value\?\.requiresPassword/)
assert.doesNotMatch(ui, /privateValues.*123456|password:\s*['"]123456/)
})
test('explicit secrets in chat are rejected before creating an outgoing bubble or HTTP cache record', () => {
assert.equal(assistantContainsChatPassword('密码设置为Sample12'), true)
assert.equal(assistantContainsChatPassword('password: Sample12'), true)
assert.equal(assistantContainsChatPassword('帮张三重置密码'), false)
assert.equal(assistantContainsChatPassword('怎么重置密码?'), false)
const store = readFileSync(new URL('../../src/store/modules/businessAssistant.js', import.meta.url), 'utf8')
assert.ok(store.indexOf('if (assistantContainsChatPassword(text))') < store.indexOf('const payload = { sessionId, text'))
})