mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-01 13:34:33 +00:00
* feat(sessions): show token usage and estimated cost in logs * fix(sessions): compact the usage footer * fix(ci): bypass Bun cache for Windows ARM64
158 lines
5.3 KiB
TypeScript
158 lines
5.3 KiB
TypeScript
import { expect, test } from '../../fixtures'
|
|
|
|
test('session logs scroll with usage pinned below long messages and expanded records', async ({
|
|
owner,
|
|
prisma,
|
|
}) => {
|
|
const { page, teamId, email } = owner
|
|
const user = await prisma.user.findUniqueOrThrow({ where: { email } })
|
|
const agentUser = await prisma.user.create({
|
|
data: { name: 'Session Logs Agent', email: 'session-logs@shumai.test', type: 'agent' },
|
|
})
|
|
const agent = await prisma.agent.create({
|
|
data: {
|
|
id: agentUser.id,
|
|
teamId,
|
|
type: 'chat',
|
|
enabled: false,
|
|
config: { provider: 'test', model: 'test' },
|
|
},
|
|
})
|
|
const session = await prisma.agentSession.create({
|
|
data: {
|
|
agentId: agent.id,
|
|
userId: user.id,
|
|
type: 'chat',
|
|
name: 'Scrollable Session Logs',
|
|
cwd: '/test',
|
|
accountedUsage: {
|
|
inputTokens: 12800,
|
|
outputTokens: 1580,
|
|
cacheReadTokens: 10000,
|
|
totalTokens: 24380,
|
|
cost: 0.014,
|
|
},
|
|
},
|
|
})
|
|
await prisma.agentSessionCommit.create({
|
|
data: {
|
|
sessionId: session.id,
|
|
seq: 1n,
|
|
data: {
|
|
version: 1,
|
|
writes: [
|
|
{ type: 'conversation', value: { id: 1 } },
|
|
...Array.from({ length: 30 }, (_, index) => ({
|
|
type: 'entry',
|
|
value: {
|
|
id: index + 2,
|
|
conversationId: 1,
|
|
kind: 'pi.user',
|
|
model: [
|
|
{
|
|
role: 'user',
|
|
content: `Log message ${index + 1}\n\n\`\`\`text\n${'unbroken_output_'.repeat(100)}\n\`\`\``,
|
|
timestamp: Date.now(),
|
|
},
|
|
],
|
|
data: { output: 'raw_output_'.repeat(100) },
|
|
},
|
|
})),
|
|
],
|
|
},
|
|
},
|
|
})
|
|
|
|
await page.goto(`/teams/${teamId}/dashboard`)
|
|
await page.getByRole('button', { name: /^(Sessions|会话)$/ }).click()
|
|
await page.getByRole('row').filter({ hasText: session.name! }).click()
|
|
const dialog = page.getByRole('dialog')
|
|
await expect(dialog.getByText('Log message 1', { exact: true })).toBeVisible()
|
|
await expect(dialog.getByText(/^(Entry ID|条目 ID):/)).toHaveCount(0)
|
|
const viewport = dialog.locator('[data-slot="scroll-area-viewport"]')
|
|
const heading = dialog.getByRole('heading')
|
|
const footer = dialog.locator('footer')
|
|
const breakdown = footer.getByRole('button', { name: /^(Breakdown|明细|상세 내역)$/ })
|
|
await expect(footer).toContainText('24,380')
|
|
await expect(footer).toContainText('$0.014')
|
|
|
|
for (const size of [
|
|
{ width: 1280, height: 800 },
|
|
{ width: 390, height: 640 },
|
|
]) {
|
|
await page.setViewportSize(size)
|
|
await dialog.evaluate(async (element) => {
|
|
await Promise.all(element.getAnimations().map((animation) => animation.finished))
|
|
})
|
|
await expect
|
|
.poll(async () => {
|
|
return dialog.evaluate((element) => {
|
|
const bounds = element.getBoundingClientRect()
|
|
return (
|
|
bounds.top >= 0 &&
|
|
bounds.bottom <= window.innerHeight &&
|
|
bounds.left >= 0 &&
|
|
bounds.right <= window.innerWidth
|
|
)
|
|
})
|
|
})
|
|
.toBe(true)
|
|
await expect
|
|
.poll(() =>
|
|
viewport.evaluate(
|
|
(element) =>
|
|
element.clientHeight > 0 &&
|
|
element.scrollHeight > element.clientHeight &&
|
|
element.scrollWidth <= element.clientWidth,
|
|
),
|
|
)
|
|
.toBe(true)
|
|
const headerTop = (await heading.boundingBox())!.y
|
|
const footerTop = (await footer.boundingBox())!.y
|
|
await viewport.evaluate((element) => {
|
|
element.scrollTop = 0
|
|
})
|
|
await viewport.hover()
|
|
// Wheel events may be capped; verify movement before jumping to the final record.
|
|
await page.mouse.wheel(0, 400)
|
|
await expect.poll(() => viewport.evaluate((element) => element.scrollTop)).toBeGreaterThan(0)
|
|
const record = dialog.locator('details').last()
|
|
await viewport.evaluate((element) => {
|
|
element.scrollTop = element.scrollHeight
|
|
})
|
|
await expect(record.locator('summary')).toBeInViewport()
|
|
expect((await heading.boundingBox())!.y).toBeCloseTo(headerTop, 0)
|
|
expect((await footer.boundingBox())!.y).toBeCloseTo(footerTop, 0)
|
|
await expect(footer).toBeInViewport()
|
|
|
|
await breakdown.click()
|
|
const popover = page.getByRole('dialog', {
|
|
name: /Token breakdown|Token 用量明细|토큰 사용량 상세 내역/,
|
|
})
|
|
await expect(popover).toBeVisible()
|
|
for (const count of ['12,800', '1,580', '10,000']) {
|
|
await expect(popover.getByText(count, { exact: true })).toBeVisible()
|
|
}
|
|
await expect
|
|
.poll(async () => {
|
|
const bounds = await popover.boundingBox()
|
|
return bounds ? bounds.y + bounds.height : Infinity
|
|
})
|
|
.toBeLessThan(footerTop + 1)
|
|
await page.keyboard.press('Escape')
|
|
await expect(popover).toBeHidden()
|
|
await expect(breakdown).toBeFocused()
|
|
|
|
await record.locator('summary').click()
|
|
await viewport.evaluate((element) => {
|
|
element.scrollTop = element.scrollHeight
|
|
})
|
|
await expect(record.locator('pre')).toBeInViewport()
|
|
await expect(record.locator('pre')).toContainText('"id": 31')
|
|
expect(await viewport.evaluate((element) => element.scrollWidth <= element.clientWidth)).toBe(
|
|
true,
|
|
)
|
|
await record.locator('summary').click()
|
|
}
|
|
})
|