Files
shumai/packages/agent/src/tools/read-thread.authz.test.ts
T
Yiling-J 35a0e82284 refactor(agent)!: migrate to Pi Durable and embed the agent queue (#525)
* refactor(agent)!: migrate sessions to Pi Durable

Replace patched AgentHarness storage with per-session SQLite and PostgreSQL commit mirroring. Migrate legacy history automatically and run agent queues with the main server.

BREAKING CHANGE: Remove standalone agent workers and return native Pi entry records from chat history APIs.

* fix(agent): constrain session log dialog scrolling

Keep the header visible and wrap long output while log entries and expanded records scroll within the dialog. Cover desktop and narrow viewports through the dashboard Sessions flow.

* fix(agent): hide entry IDs from session log card headers

Retain internal entry IDs in expanded raw records for debugging. Verify that card headers omit the IDs while raw JSON preserves them.

* test(e2e): handle browser wheel limits in session log checks

Verify that a normal wheel event moves the viewport before explicitly scrolling to the final record. Keep the dialog bounds, fixed header, wrapping, and expanded-record assertions.

* fix(agent): select model providers without inheriting unrelated auth

* fix(webui): prevent resize feedback when toggling agent panel

* test(agent): reproduce concurrent comment session races

* fix(agent): serialize comment sessions and deduplicate history

* docs(changelog): consolidate Pi Durable changes into one entry
2026-11-11 16:05:18 +08:00

95 lines
3.5 KiB
TypeScript

import { invokeTool } from '@shumai/core/src/agent/tool-test-utils'
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { describe, expect, it } from 'vitest'
import { createReadThreadTool } from './read-thread'
/**
* Real-database authorization tests for `read_thread`.
*
* Reproduces a bug where the tool returned any comment thread by ID without
* checking that the invoking user (or, for system agents, the agent's team)
* has access to the asset the thread belongs to.
*/
describe('read_thread authorization', () => {
setupTestDbHooks()
async function seedTeamWithThread(label: string) {
const team = await prisma.team.create({ data: { name: `Team ${label}` } })
const project = await prisma.project.create({
data: { name: `Project ${label}`, teamId: team.id },
})
const asset = await prisma.asset.create({
data: { name: `${label}.png`, type: 'file', status: 'uploaded', projectId: project.id },
})
const author = await prisma.user.create({
data: { name: `Author ${label}`, email: `author-${label}@test.com`, password: 'pw' },
})
await prisma.teamMember.create({
data: { teamId: team.id, userId: author.id, role: 'owner' },
})
const thread = await prisma.assetComment.create({
data: { assetId: asset.id, creatorId: author.id, message: `secret message ${label}` },
})
await prisma.assetComment.create({
data: {
assetId: asset.id,
creatorId: author.id,
replyToId: thread.id,
message: `secret reply ${label}`,
},
})
return { team, project, asset, author, thread }
}
it('lets a team member read a thread in their team', async () => {
const a = await seedTeamWithThread('a')
const tool = createReadThreadTool({ userId: a.author.id, teamId: a.team.id })
const result = await invokeTool(tool, 'call-1', { threadId: a.thread.id })
const text = (result.content[0] as { text: string }).text
expect(text).toContain('secret message a')
expect(text).toContain('secret reply a')
})
it('rejects a user reading a thread from another team', async () => {
const a = await seedTeamWithThread('a')
const b = await seedTeamWithThread('b')
const tool = createReadThreadTool({ userId: a.author.id, teamId: a.team.id })
await expect(invokeTool(tool, 'call-1', { threadId: b.thread.id })).rejects.toThrow(
'User is not a member of the team',
)
})
it('rejects a project-scoped member reading a thread in a project they cannot access', async () => {
const b = await seedTeamWithThread('b')
const outsider = await prisma.user.create({
data: { name: 'Project scoped', email: 'scoped@test.com', password: 'pw' },
})
await prisma.teamMember.create({
data: { teamId: b.team.id, userId: outsider.id, role: 'editor', scope: 'project' },
})
const tool = createReadThreadTool({ userId: outsider.id, teamId: b.team.id })
await expect(invokeTool(tool, 'call-1', { threadId: b.thread.id })).rejects.toThrow()
})
it('confines a system agent (no user) to threads in its own team', async () => {
const a = await seedTeamWithThread('a')
const b = await seedTeamWithThread('b')
const tool = createReadThreadTool({ teamId: a.team.id })
const own = await invokeTool(tool, 'call-1', { threadId: a.thread.id })
expect((own.content[0] as { text: string }).text).toContain('secret message a')
await expect(invokeTool(tool, 'call-2', { threadId: b.thread.id })).rejects.toThrow(
'Access denied',
)
})
})