Files
knowledge-governance/web/src/PPConnection.mounted.test.tsx
T
姚章浩 3825859da6
build / build (push) Successful in 2m20s
feat(settings): 打通 PP 认证配置并统一 DWS 来源授权
PP 增加受控凭据更新、真实登录与目录检查,待验证修改不会替换生效配置;扫描与取件共享生效修订。
钉钉来源向导、校验与登记共用 DWS 授权,支持中途恢复与换账号保护,取消旧扫码自动回落。
认证审计复用已有来源审计页,配置响应不回传密码;补齐运行镜像脚本与权限。

验证:make verify 退出 0;PP/DWS/httpapi 目标 race 回归通过;隔离 HTTP 服务验证 PP 协议及扫描/取件消费者。
尚未部署;真实 PP 凭据与目录可用性在测试站部署后验证。
2026-11-17 19:08:46 +08:00

77 lines
3.1 KiB
TypeScript

// @vitest-environment jsdom
import { act } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { PPConnection } from './PPConnection'
let root: Root, host: HTMLDivElement
let fetcher: ReturnType<typeof vi.fn<(path: string, init?: RequestInit) => Promise<Response>>>
const active = {
id: 'active-one',
base_url: 'https://api.example.invalid',
username: 'example',
password_configured: true,
}
const view = {
revision: 1,
active,
pending: null,
observation: { state: 'valid', message: '认证有效 · 目录可读', checked_at: '2026-11-17T10:00:00Z' },
pending_observation: null,
}
const json = (data: unknown) => new Response(JSON.stringify({ data }))
const click = async (label: string) => {
const b = [...host.querySelectorAll('button')].find((x) => x.textContent?.trim() === label)
expect(b, label).toBeTruthy()
await act(async () => b!.click())
}
const type = async (label: string, value: string) => {
const input = [...host.querySelectorAll('label')]
.find((x) => x.textContent?.startsWith(label))
?.querySelector('input')
expect(input, label).toBeTruthy()
await act(async () => {
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set!.call(input, value)
input!.dispatchEvent(new Event('input', { bubbles: true }))
})
}
beforeEach(() => {
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true })
host = document.createElement('div')
document.body.append(host)
root = createRoot(host)
fetcher = vi.fn(async (_p: string, init?: RequestInit) =>
json(init?.method === 'PUT' ? { ...view, revision: 2 } : view)
)
vi.stubGlobal('fetch', fetcher)
})
afterEach(async () => {
await act(async () => root.unmount())
host.remove()
vi.unstubAllGlobals()
})
describe('PP configuration editor', () => {
it('keeps the stored password without sending a masked replacement or launching scans', async () => {
await act(async () => root.render(<PPConnection />))
await click('配置')
expect(host.querySelector('input[type=password]')).toBeNull()
await click('保存并校验')
const writes = fetcher.mock.calls.filter(([, i]) => i?.method === 'PUT')
expect(writes).toHaveLength(1)
const data = JSON.parse(String(writes[0][1]?.body))
expect(data).toEqual({ base_url: active.base_url, username: 'example', revision: 1 })
expect(host.textContent).toContain('已保存并生效')
expect(fetcher.mock.calls.every(([p]) => p.startsWith('/api/v1/pp-connection'))).toBe(true)
})
it('sends a newly entered password only on explicit replacement and clears it after save', async () => {
await act(async () => root.render(<PPConnection />))
await click('配置')
await click('更新密码')
await type('新密码', 'example-replacement')
await click('保存并校验')
const write = fetcher.mock.calls.find(([, i]) => i?.method === 'PUT')
expect(JSON.parse(String(write?.[1]?.body)).password).toBe('example-replacement')
expect(host.querySelector('input[type=password]')).toBeNull()
expect(host.textContent).not.toContain('example-replacement')
})
})