Files
shumai/packages/agent/src/tools/read-skill.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

524 lines
19 KiB
TypeScript

import { invokeTool } from '@shumai/core/src/agent/tool-test-utils'
import { vi } from 'vitest'
vi.mock('@shumai/core/src/s3/s3')
vi.mock('fs')
import { s3Service } from '@shumai/core/src/s3/s3'
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import AdmZip from 'adm-zip'
import * as fs from 'fs'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { createReadSkillTool } from './read-skill'
describe('readSkillTool', () => {
setupTestDbHooks()
beforeEach(() => {
vi.clearAllMocks()
})
it('should throw error if skill not found', async () => {
const readSkillTool = createReadSkillTool(undefined, () => {})
await expect(
invokeTool(readSkillTool, '1', { skillId: 'non-existent' }, undefined, undefined),
).rejects.toThrow('not found')
})
it('should read skill from cache if hash matches', async () => {
const team = await prisma.team.create({ data: { name: 'Test Team' } })
const skill = await prisma.skill.create({
data: {
name: 'Test Skill',
assetId: 'asset1',
hash: 'match-hash',
teamId: team.id,
},
})
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs calls
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
if (path.toString().endsWith('.hash')) return 'match-hash'
if (path.toString().endsWith('SKILL.md')) return '# Test Skill Content'
return ''
})
const readSkillTool = createReadSkillTool(undefined, () => {})
const result = await invokeTool(readSkillTool, '1', { skillId: skill.id }, undefined, undefined)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((result.content[0] as any).text).toBe('# Test Skill Content')
expect(s3Service.getObject).not.toHaveBeenCalled()
})
it('should download and extract skill if cache missing or hash mismatch', async () => {
const team = await prisma.team.create({ data: { name: 'Test Team' } })
const skill = await prisma.skill.create({
data: {
name: 'New Skill',
assetId: 'asset1',
hash: 'new-hash',
teamId: team.id,
},
})
await prisma.asset.create({
data: {
id: 'asset1',
name: 'skill.zip',
storageKey: { create: { key: 'skills/skill.zip' } },
type: 'file',
status: 'uploaded',
},
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs calls
vi.spyOn(fs, 'existsSync').mockImplementation((path: any) => {
if (path.toString().endsWith('.hash')) return false
if (path.toString().endsWith('SKILL.md')) return true
return true
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs calls
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
if (path.toString().endsWith('SKILL.md')) return '# Extracted Content'
return ''
})
const zip = new AdmZip()
zip.addFile('SKILL.md', Buffer.from('# Extracted Content'))
const zipBuffer = zip.toBuffer()
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking s3Service return
;(s3Service.getObject as any).mockResolvedValue({
buffer: zipBuffer,
contentType: 'application/zip',
})
const readSkillTool = createReadSkillTool(undefined, () => {})
const result = await invokeTool(readSkillTool, '1', { skillId: skill.id }, undefined, undefined)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((result.content[0] as any).text).toBe('# Extracted Content')
expect(s3Service.getObject).toHaveBeenCalled()
expect(fs.writeFileSync).toHaveBeenCalledWith(expect.stringContaining('.hash'), 'new-hash')
})
describe('environment variables', () => {
let originalEnv: NodeJS.ProcessEnv
beforeEach(() => {
originalEnv = { ...process.env }
})
afterEach(() => {
process.env = originalEnv
})
it('should use user-configured value if it is non-empty', async () => {
const team = await prisma.team.create({ data: { name: 'Test Team' } })
const skill = await prisma.skill.create({
data: {
name: 'Skill Env Test 1',
assetId: 'asset1',
hash: 'hash1',
teamId: team.id,
config: {
environmentVariables: [{ name: 'CONFIGURED_VAR', default: 'user-val' }],
},
},
})
delete process.env.CONFIGURED_VAR
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
if (path.toString().endsWith('.hash')) return 'hash1'
if (path.toString().endsWith('SKILL.md')) return '# Content'
return ''
})
let capturedEnvs: Record<string, string> = {}
const readSkillTool = createReadSkillTool(undefined, (envs) => {
capturedEnvs = { ...capturedEnvs, ...envs }
})
await invokeTool(readSkillTool, '1', { skillId: skill.id }, undefined, undefined)
expect(capturedEnvs).toEqual({
CONFIGURED_VAR: 'user-val',
})
})
it('should favor user-configured value even if host environment variable is also defined', async () => {
const team = await prisma.team.create({ data: { name: 'Test Team' } })
const skill = await prisma.skill.create({
data: {
name: 'Skill Env Test 2',
assetId: 'asset1',
hash: 'hash2',
teamId: team.id,
config: {
environmentVariables: [{ name: 'OVERRIDDEN_VAR', default: 'user-val' }],
},
},
})
process.env.OVERRIDDEN_VAR = 'host-val'
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
if (path.toString().endsWith('.hash')) return 'hash2'
if (path.toString().endsWith('SKILL.md')) return '# Content'
return ''
})
let capturedEnvs: Record<string, string> = {}
const readSkillTool = createReadSkillTool(undefined, (envs) => {
capturedEnvs = { ...capturedEnvs, ...envs }
})
await invokeTool(readSkillTool, '1', { skillId: skill.id }, undefined, undefined)
expect(capturedEnvs).toEqual({
OVERRIDDEN_VAR: 'user-val',
})
})
it('should fall back to host environment variable if user-configured value is empty string', async () => {
const team = await prisma.team.create({ data: { name: 'Test Team' } })
const skill = await prisma.skill.create({
data: {
name: 'Skill Env Test 3',
assetId: 'asset1',
hash: 'hash3',
teamId: team.id,
config: {
environmentVariables: [{ name: 'FALLBACK_VAR', default: '' }],
},
},
})
process.env.FALLBACK_VAR = 'host-val'
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
if (path.toString().endsWith('.hash')) return 'hash3'
if (path.toString().endsWith('SKILL.md')) return '# Content'
return ''
})
let capturedEnvs: Record<string, string> = {}
const readSkillTool = createReadSkillTool(undefined, (envs) => {
capturedEnvs = { ...capturedEnvs, ...envs }
})
await invokeTool(readSkillTool, '1', { skillId: skill.id }, undefined, undefined)
expect(capturedEnvs).toEqual({
FALLBACK_VAR: 'host-val',
})
})
it('should fall back to host environment variable if user-configured value is undefined', async () => {
const team = await prisma.team.create({ data: { name: 'Test Team' } })
const skill = await prisma.skill.create({
data: {
name: 'Skill Env Test 4',
assetId: 'asset1',
hash: 'hash4',
teamId: team.id,
config: {
environmentVariables: [{ name: 'FALLBACK_VAR_UNDEF' }],
},
},
})
process.env.FALLBACK_VAR_UNDEF = 'host-val2'
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
if (path.toString().endsWith('.hash')) return 'hash4'
if (path.toString().endsWith('SKILL.md')) return '# Content'
return ''
})
let capturedEnvs: Record<string, string> = {}
const readSkillTool = createReadSkillTool(undefined, (envs) => {
capturedEnvs = { ...capturedEnvs, ...envs }
})
await invokeTool(readSkillTool, '1', { skillId: skill.id }, undefined, undefined)
expect(capturedEnvs).toEqual({
FALLBACK_VAR_UNDEF: 'host-val2',
})
})
})
describe('permissions', () => {
it('should allow loading skill matching user role level or lower', async () => {
const team = await prisma.team.create({ data: { name: 'Permission Team' } })
const ownerUser = await prisma.user.create({
data: { name: 'Owner User', email: 'owner@test.com' },
})
const reviewerUser = await prisma.user.create({
data: { name: 'Reviewer User', email: 'reviewer@test.com' },
})
await prisma.teamMember.create({
data: { teamId: team.id, userId: ownerUser.id, role: 'owner' },
})
await prisma.teamMember.create({
data: { teamId: team.id, userId: reviewerUser.id, role: 'reviewer' },
})
const ownerSkill = await prisma.skill.create({
data: {
name: 'Owner Skill',
assetId: 'asset1',
hash: 'hash-owner',
teamId: team.id,
permission: 'owner',
},
})
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
if (path.toString().endsWith('.hash')) return 'hash-owner'
if (path.toString().endsWith('SKILL.md')) return '# Owner Skill'
return ''
})
const ownerTool = createReadSkillTool(ownerUser.id, () => {})
const resultOwner = await invokeTool(
ownerTool,
'1',
{ skillId: ownerSkill.id },
undefined,
undefined,
)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((resultOwner.content[0] as any).text).toBe('# Owner Skill')
const reviewerTool = createReadSkillTool(reviewerUser.id, () => {})
await expect(
invokeTool(reviewerTool, '1', { skillId: ownerSkill.id }, undefined, undefined),
).rejects.toThrow('Permission denied')
})
it('uses the effective project role for skill permission checks', async () => {
const team = await prisma.team.create({ data: { name: 'Project Skill Team' } })
const project = await prisma.project.create({ data: { name: 'Skill Proj', teamId: team.id } })
const editorSkill = await prisma.skill.create({
data: {
name: 'Editor Project Skill',
assetId: 'asset1',
hash: 'proj-editor-hash',
teamId: team.id,
permission: 'editor',
},
})
// Team editor demoted to project reviewer: denied inside the project
const demoted = await prisma.user.create({
data: { name: 'Demoted Skill', email: 'dem-skill@test.com' },
})
const demotedTm = await prisma.teamMember.create({
data: { teamId: team.id, userId: demoted.id, role: 'editor', scope: 'team' },
})
await prisma.projectMember.create({
data: { projectId: project.id, teamMemberId: demotedTm.id, role: 'reviewer' },
})
const demotedTool = createReadSkillTool(
demoted.id,
() => {},
undefined,
undefined,
project.id,
)
await expect(
invokeTool(demotedTool, '1', { skillId: editorSkill.id }, undefined, undefined),
).rejects.toThrow('Permission denied')
// Project-scoped member invited as editor: allowed
const projEditor = await prisma.user.create({
data: { name: 'PE Skill', email: 'pe-skill@test.com' },
})
const projEditorTm = await prisma.teamMember.create({
data: { teamId: team.id, userId: projEditor.id, role: 'reviewer', scope: 'project' },
})
await prisma.projectMember.create({
data: { projectId: project.id, teamMemberId: projEditorTm.id, role: 'editor' },
})
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs calls
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
if (path.toString().endsWith('.hash')) return 'proj-editor-hash'
if (path.toString().endsWith('SKILL.md')) return '# Editor Project Skill Content'
return ''
})
const projEditorTool = createReadSkillTool(
projEditor.id,
() => {},
undefined,
undefined,
project.id,
)
const result = await invokeTool(
projEditorTool,
'1',
{ skillId: editorSkill.id },
undefined,
undefined,
)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((result.content[0] as any).text).toBe('# Editor Project Skill Content')
})
})
describe('onSkillLoaded callback', () => {
it('should invoke onSkillLoaded after a successful skill load', async () => {
const team = await prisma.team.create({ data: { name: 'Callback Team' } })
const skill = await prisma.skill.create({
data: {
name: 'Callback Skill',
assetId: 'asset1',
hash: 'cb-hash',
teamId: team.id,
},
})
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
if (path.toString().endsWith('.hash')) return 'cb-hash'
if (path.toString().endsWith('SKILL.md')) return '# Callback Skill'
return ''
})
const onSkillLoaded = vi.fn()
const readSkillTool = createReadSkillTool(undefined, () => {}, onSkillLoaded)
await invokeTool(readSkillTool, '1', { skillId: skill.id }, undefined, undefined)
expect(onSkillLoaded).toHaveBeenCalledTimes(1)
})
it('should not invoke onSkillLoaded when loading is denied', async () => {
const team = await prisma.team.create({ data: { name: 'Callback Deny Team' } })
const reviewerUser = await prisma.user.create({
data: { name: 'Reviewer Callback', email: 'callback-reviewer@test.com' },
})
await prisma.teamMember.create({
data: { teamId: team.id, userId: reviewerUser.id, role: 'reviewer' },
})
const ownerSkill = await prisma.skill.create({
data: {
name: 'Owner Callback Skill',
assetId: 'asset1',
hash: 'cb-owner-hash',
teamId: team.id,
permission: 'owner',
},
})
const onSkillLoaded = vi.fn()
const reviewerTool = createReadSkillTool(reviewerUser.id, () => {}, onSkillLoaded)
await expect(
invokeTool(reviewerTool, '1', { skillId: ownerSkill.id }, undefined, undefined),
).rejects.toThrow('Permission denied')
expect(onSkillLoaded).not.toHaveBeenCalled()
})
})
describe('enabled skills enforcement', () => {
it('should reject loading a skill that is not enabled for the agent', async () => {
const team = await prisma.team.create({ data: { name: 'Enabled Filter Team' } })
const skill = await prisma.skill.create({
data: {
name: 'Disabled For Agent Skill',
assetId: 'asset1',
hash: 'disabled-agent-hash',
teamId: team.id,
},
})
const readSkillTool = createReadSkillTool(undefined, () => {}, undefined, [
'some-other-enabled-skill',
])
await expect(
invokeTool(readSkillTool, '1', { skillId: skill.id }, undefined, undefined),
).rejects.toThrow('not enabled for this agent')
})
it('should allow loading a skill that is enabled for the agent', async () => {
const team = await prisma.team.create({ data: { name: 'Enabled Allow Team' } })
const skill = await prisma.skill.create({
data: {
name: 'Enabled For Agent Skill',
assetId: 'asset1',
hash: 'enabled-agent-hash',
teamId: team.id,
},
})
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
if (path.toString().endsWith('.hash')) return 'enabled-agent-hash'
if (path.toString().endsWith('SKILL.md')) return '# Enabled For Agent Skill'
return ''
})
const readSkillTool = createReadSkillTool(undefined, () => {}, undefined, [skill.id])
const result = await invokeTool(
readSkillTool,
'1',
{ skillId: skill.id },
undefined,
undefined,
)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((result.content[0] as any).text).toBe('# Enabled For Agent Skill')
})
it('should allow loading any skill when enabledSkillIds is not provided (backward compatibility)', async () => {
const team = await prisma.team.create({ data: { name: 'No Filter Team' } })
const skill = await prisma.skill.create({
data: {
name: 'No Filter Skill',
assetId: 'asset1',
hash: 'no-filter-hash',
teamId: team.id,
},
})
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
if (path.toString().endsWith('.hash')) return 'no-filter-hash'
if (path.toString().endsWith('SKILL.md')) return '# No Filter Skill'
return ''
})
const readSkillTool = createReadSkillTool(undefined, () => {})
const result = await invokeTool(
readSkillTool,
'1',
{ skillId: skill.id },
undefined,
undefined,
)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((result.content[0] as any).text).toBe('# No Filter Skill')
})
})
})