fix(search): harden authorized results and ordering

This commit is contained in:
2026-07-21 17:42:22 +08:00
parent 110423406b
commit 60e5ef0a87
13 changed files with 481 additions and 80 deletions

View File

@@ -125,6 +125,19 @@ test('FormData body leaves Content-Type unset so fetch provides the boundary', a
assert.equal(requestInit.headers['Content-Type'], undefined)
})
test('request forwards its AbortSignal to fetch', async () => {
let requestInit
const controller = new AbortController()
globalThis.fetch = async (_url, init) => {
requestInit = init
return Response.json({ ok: true })
}
await apiRequest('/search', { signal: controller.signal })
assert.equal(requestInit.signal, controller.signal)
})
test('base URL normalizer trims whitespace and all trailing slashes', () => {
assert.equal(normalizeBaseUrl(' https://host.example/// '), 'https://host.example')
})

View File

@@ -0,0 +1,41 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import { createSearchRequestGate } from '../src/app/search-request-gate.ts'
test('starting a new request aborts and invalidates the previous request', () => {
const gate = createSearchRequestGate()
const first = gate.begin()
const second = gate.begin()
assert.equal(first.signal.aborted, true)
assert.equal(first.isCurrent(), false)
assert.equal(second.signal.aborted, false)
assert.equal(second.isCurrent(), true)
})
test('session cleanup aborts the request and suppresses its stale error', async () => {
const gate = createSearchRequestGate()
const request = gate.begin()
const surfacedErrors = []
const oldSessionFailure = Promise.reject(new Error('旧会话搜索失败')).catch((error) => {
if (request.isCurrent()) surfacedErrors.push(error.message)
})
gate.invalidate()
await oldSessionFailure
assert.equal(request.signal.aborted, true)
assert.equal(request.isCurrent(), false)
assert.deepEqual(surfacedErrors, [])
})
test('a request after invalidation belongs to the new session generation', () => {
const gate = createSearchRequestGate()
const oldSessionRequest = gate.begin()
gate.invalidate()
const newSessionRequest = gate.begin()
assert.equal(oldSessionRequest.isCurrent(), false)
assert.equal(newSessionRequest.isCurrent(), true)
assert.equal(newSessionRequest.signal.aborted, false)
})

View File

@@ -21,7 +21,11 @@ const failures = []
const projectId = '019b0000-0000-7000-8000-000000000001'
const taskSearchResultId = '019b0000-0000-7000-8000-000000000003'
const unknownProjectId = '019b0000-0000-7000-8000-000000000099'
const externalProjectId = '019b0000-0000-7000-8000-000000000088'
const externalTaskId = '019b0000-0000-7000-8000-000000000089'
const externalNoteId = '019b0000-0000-7000-8000-000000000090'
const searchRequests = []
const workspaceRequests = []
const projectPatchRequests = []
let expectingProjectPatchError = false
let expectedProjectPatchConsoleErrorCount = 0
@@ -83,6 +87,7 @@ await page.route('http://localhost:9150/api/v1/**', async (route) => {
return
}
if (url.pathname === `/api/v1/projects/${projectId}/workspace`) {
workspaceRequests.push(url.pathname)
await route.fulfill({ json: visualCheckWorkspace })
return
}
@@ -90,16 +95,22 @@ await page.route('http://localhost:9150/api/v1/**', async (route) => {
const query = url.searchParams.get('q') ?? ''
searchRequests.push(query)
if (query === '慢请求') await new Promise((resolve) => setTimeout(resolve, 300))
const projectIdForResult = query === '未知项目' ? unknownProjectId : projectId
const result = query === '外部任务'
? { id: externalTaskId, type: 'task', title: '获授权外部任务', projectId: externalProjectId, snippet: '仅限处理合同签署' }
: query === '外部笔记'
? { id: externalNoteId, type: 'note', title: '获授权外部笔记', projectId: externalProjectId, snippet: '仅共享合同背景段落' }
: query === '未知结果'
? { id: 'untrusted-result', type: 'source', title: '未知类型结果', projectId: unknownProjectId, snippet: '不得预览或导航' }
: {
id: taskSearchResultId,
type: 'task',
title: query === '慢请求' ? '慢请求任务' : query === '快请求' ? '快请求任务' : '回调任务',
projectId,
snippet: '检查签名',
}
await route.fulfill({
json: {
items: [{
id: taskSearchResultId,
type: 'task',
title: query === '未知项目' ? '未知项目任务' : query === '慢请求' ? '慢请求任务' : query === '快请求' ? '快请求任务' : '回调任务',
projectId: projectIdForResult,
snippet: '检查签名',
}],
items: [result],
},
})
return
@@ -351,18 +362,67 @@ if (await searchInput.count() === 0 || await searchButton.count() === 0) {
}
}
await searchInput.fill('未知项目')
const projectBeforeExternalPreview = await page.locator('.project-title h5').textContent()
const externalWorkspacePath = `/api/v1/projects/${externalProjectId}/workspace`
await searchInput.fill('外部任务')
await searchButton.click()
await page.waitForTimeout(150)
const unknownResult = page.getByRole('button', { name: /未知项目任务/ })
if (await unknownResult.count() === 0) {
failures.push('Search button must submit the current query and show results')
const externalTaskResult = page.getByRole('button', { name: /获授权外部任务/ })
if (await externalTaskResult.count() === 0) {
failures.push('authorized external task must render as a search result')
} else {
await externalTaskResult.click()
const preview = page.getByRole('dialog', { name: '授权对象预览' })
if (await preview.count() === 0) {
failures.push('authorized external task must open a restricted preview')
} else {
for (const text of ['仅显示被授权对象', '获授权外部任务', '仅限处理合同签署', externalProjectId]) {
if (!await preview.getByText(text, { exact: false }).isVisible()) failures.push(`external task preview missing ${text}`)
}
await preview.getByRole('button', { name: '关闭预览', exact: true }).click()
}
}
await searchInput.fill('外部笔记')
await searchButton.click()
await page.waitForTimeout(150)
const externalNoteResult = page.getByRole('button', { name: /获授权外部笔记/ })
if (await externalNoteResult.count() === 0) {
failures.push('authorized external note must render as a search result')
} else {
await externalNoteResult.click()
const preview = page.getByRole('dialog', { name: '授权对象预览' })
if (await preview.count() === 0) {
failures.push('authorized external note must open a restricted preview')
} else {
for (const text of ['仅显示被授权对象', '获授权外部笔记', '仅共享合同背景段落', externalProjectId]) {
if (!await preview.getByText(text, { exact: false }).isVisible()) failures.push(`external note preview missing ${text}`)
}
await preview.getByRole('button', { name: '关闭预览', exact: true }).click()
}
}
if (workspaceRequests.includes(externalWorkspacePath)) {
failures.push('external previews must never load an unowned project workspace')
}
if (await page.locator('.project-title h5').textContent() !== projectBeforeExternalPreview) {
failures.push('external previews must not switch the active owned project')
}
await searchInput.fill('未知结果')
await searchButton.click()
await page.waitForTimeout(150)
const unknownResult = page.getByRole('button', { name: /未知类型结果/ })
if (await unknownResult.count() === 0) {
failures.push('fixture must expose the unknown result for its safety assertion')
} else {
const projectBeforeUnknownResult = await page.locator('.project-title h5').textContent()
await unknownResult.click()
const projectAfterUnknownResult = await page.locator('.project-title h5').textContent()
if (projectAfterUnknownResult !== projectBeforeUnknownResult) {
failures.push('a result for an unavailable project must not fabricate project navigation')
if (await page.getByRole('dialog', { name: '授权对象预览' }).count() !== 0) {
failures.push('unknown search result types must not open an authorized preview')
}
if (await page.locator('.project-title h5').textContent() !== projectBeforeExternalPreview) {
failures.push('unknown search results must not switch or fabricate a project')
}
}