mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
Apply remaining changes
This commit is contained in:
1 parent
47bf801e34
commit
71fe35cd02
11 files changed
+592
-48
No files matched your search
@@ -52,6 +52,21 @@ function sessionsRoute(sessions: SessionInfo[], serverId = LOCAL_OPENCODE_SERVER
|
||||
}
|
||||
}
|
||||
|
||||
const localSessionsPath = '/api/opencode/api/session'
|
||||
|
||||
function pagedSessionsRoute(pages: SessionInfo[][]): FetchRoute {
|
||||
return {
|
||||
method: 'GET',
|
||||
path: localSessionsPath,
|
||||
respond: (request) => {
|
||||
const cursor = request.searchParams.get('cursor')
|
||||
const index = cursor === null ? 0 : Number(cursor)
|
||||
const next = index + 1 < pages.length ? { next: String(index + 1) } : {}
|
||||
return { json: { data: pages[index] ?? [], cursor: next } }
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function messagesRoute(serverId = LOCAL_OPENCODE_SERVER_ID): FetchRoute {
|
||||
const path =
|
||||
serverId === LOCAL_OPENCODE_SERVER_ID
|
||||
@@ -66,6 +81,25 @@ function messagesRoute(serverId = LOCAL_OPENCODE_SERVER_ID): FetchRoute {
|
||||
}
|
||||
}
|
||||
|
||||
const localMessagesPath = '/api/opencode/api/session/:sessionID/message'
|
||||
|
||||
function pagedMessagesRoute(pageSizes: number[]): FetchRoute {
|
||||
return {
|
||||
method: 'GET',
|
||||
path: localMessagesPath,
|
||||
respond: (request, params) => {
|
||||
const cursor = request.searchParams.get('cursor')
|
||||
const index = cursor === null ? 0 : Number(cursor)
|
||||
const size = pageSizes[index] ?? 0
|
||||
const data = Array.from({ length: size }, (_, itemIndex) =>
|
||||
assistantStep(`msg_${params.sessionID}_${index}_${itemIndex}`),
|
||||
)
|
||||
const next = index + 1 <= pageSizes.length ? { next: String(index + 1) } : {}
|
||||
return { json: { data, cursor: next } }
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function rowTitles(): string[] {
|
||||
return screen
|
||||
.getAllByRole('row')
|
||||
@@ -273,4 +307,278 @@ describe('UsageSessionsTable', () => {
|
||||
expect(screen.queryByText('Out of range')).not.toBeInTheDocument()
|
||||
expect(screen.queryByText('Empty session')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('links sessions without leaking the usage query parameters into the target', async () => {
|
||||
const sessions = [
|
||||
makeSession({
|
||||
id: 'ses_demo',
|
||||
title: 'Demo session',
|
||||
cost: 0.4,
|
||||
tokens: spentTokens,
|
||||
time: { created: 1000, updated: 2000 },
|
||||
location: { directory: '/workspace/demo' },
|
||||
}),
|
||||
]
|
||||
|
||||
renderWithProviders(<UsageSessionsTable serverId={LOCAL_OPENCODE_SERVER_ID} range={{ from: 0 }} />, {
|
||||
...localOptions,
|
||||
initialEntries: ['/?assistant=1&repoTab=x&usage=open'],
|
||||
fetchRoutes: [reposRoute(), sessionsRoute(sessions), messagesRoute()],
|
||||
})
|
||||
|
||||
const link = await screen.findByRole('link', { name: 'Demo session' })
|
||||
expect(link).toHaveAttribute('href', '/repos/1/sessions/ses_demo')
|
||||
})
|
||||
|
||||
it('keeps only the panel parameter in session links', async () => {
|
||||
const sessions = [
|
||||
makeSession({
|
||||
id: 'ses_demo',
|
||||
title: 'Demo session',
|
||||
cost: 0.4,
|
||||
tokens: spentTokens,
|
||||
time: { created: 1000, updated: 2000 },
|
||||
location: { directory: '/workspace/demo' },
|
||||
}),
|
||||
]
|
||||
|
||||
renderWithProviders(<UsageSessionsTable serverId={LOCAL_OPENCODE_SERVER_ID} range={{ from: 0 }} />, {
|
||||
...localOptions,
|
||||
initialEntries: ['/?panel=files&usage=open'],
|
||||
fetchRoutes: [reposRoute(), sessionsRoute(sessions), messagesRoute()],
|
||||
})
|
||||
|
||||
const link = await screen.findByRole('link', { name: 'Demo session' })
|
||||
expect(link).toHaveAttribute('href', '/repos/1/sessions/ses_demo?panel=files')
|
||||
})
|
||||
|
||||
it('keeps sessions updated after the range end when they were active since the start', async () => {
|
||||
const sessions = [
|
||||
makeSession({
|
||||
id: 'ses_later',
|
||||
title: 'Updated later',
|
||||
cost: 0.4,
|
||||
tokens: spentTokens,
|
||||
time: { created: 1000, updated: 9000 },
|
||||
}),
|
||||
makeSession({
|
||||
id: 'ses_active',
|
||||
title: 'Active in range',
|
||||
cost: 0.2,
|
||||
tokens: spentTokens,
|
||||
time: { created: 1000, updated: 2000 },
|
||||
}),
|
||||
]
|
||||
|
||||
renderWithProviders(
|
||||
<UsageSessionsTable
|
||||
serverId={LOCAL_OPENCODE_SERVER_ID}
|
||||
range={{ from: 1000, to: 3000 }}
|
||||
/>,
|
||||
{
|
||||
...localOptions,
|
||||
fetchRoutes: [reposRoute(), sessionsRoute(sessions), messagesRoute()],
|
||||
},
|
||||
)
|
||||
|
||||
await screen.findByText('Updated later')
|
||||
|
||||
expect(rowTitles()).toEqual(['Updated later', 'Active in range'])
|
||||
})
|
||||
|
||||
it('renders an error banner when the session list request fails', async () => {
|
||||
renderWithProviders(<UsageSessionsTable serverId={LOCAL_OPENCODE_SERVER_ID} range={{ from: 0 }} />, {
|
||||
...localOptions,
|
||||
fetchRoutes: [
|
||||
reposRoute(),
|
||||
{
|
||||
method: 'GET',
|
||||
path: localSessionsPath,
|
||||
respond: () => ({ status: 500, json: { error: 'boom' } }),
|
||||
},
|
||||
messagesRoute(),
|
||||
],
|
||||
})
|
||||
|
||||
expect(await screen.findByText('Sessions unavailable')).toBeInTheDocument()
|
||||
expect(
|
||||
screen.getByText('This OpenCode server could not list its sessions.'),
|
||||
).toBeInTheDocument()
|
||||
expect(screen.getByText('UnexpectedStatus: 500')).toBeInTheDocument()
|
||||
expect(screen.queryByText('No sessions in this range.')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('keeps loaded rows and notes an incomplete ranking when a later page fails', async () => {
|
||||
const firstPage = Array.from({ length: 100 }, (_, index) =>
|
||||
makeSession({
|
||||
id: `ses_first_${index}`,
|
||||
title: `First page ${index}`,
|
||||
cost: 0.5,
|
||||
tokens: spentTokens,
|
||||
time: { created: 1000, updated: 10_000 - index },
|
||||
}),
|
||||
)
|
||||
|
||||
renderWithProviders(<UsageSessionsTable serverId={LOCAL_OPENCODE_SERVER_ID} range={{ from: 0 }} />, {
|
||||
...localOptions,
|
||||
fetchRoutes: [
|
||||
reposRoute(),
|
||||
{
|
||||
method: 'GET',
|
||||
path: localSessionsPath,
|
||||
respond: (request) =>
|
||||
request.searchParams.get('cursor') === null
|
||||
? { json: { data: firstPage, cursor: { next: '1' } } }
|
||||
: { status: 500, json: { error: 'boom' } },
|
||||
},
|
||||
messagesRoute(),
|
||||
],
|
||||
})
|
||||
|
||||
expect(
|
||||
await screen.findByText('Some sessions could not be loaded, so this ranking may be incomplete.'),
|
||||
).toBeInTheDocument()
|
||||
expect(screen.getByText('First page 0')).toBeInTheDocument()
|
||||
expect(screen.queryByText('Sessions unavailable')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('loads further pages while the oldest loaded session is still in range', async () => {
|
||||
const firstPage = Array.from({ length: 100 }, (_, index) =>
|
||||
makeSession({
|
||||
id: `ses_first_${index}`,
|
||||
title: `First page ${index}`,
|
||||
cost: 0.5,
|
||||
tokens: spentTokens,
|
||||
time: { created: 1000, updated: 10_000 - index },
|
||||
}),
|
||||
)
|
||||
const secondPage = [
|
||||
makeSession({
|
||||
id: 'ses_second_0',
|
||||
title: 'Second page session',
|
||||
cost: 0.9,
|
||||
tokens: spentTokens,
|
||||
time: { created: 1000, updated: 9000 },
|
||||
}),
|
||||
]
|
||||
|
||||
const result = renderWithProviders(
|
||||
<UsageSessionsTable serverId={LOCAL_OPENCODE_SERVER_ID} range={{ from: 0 }} />,
|
||||
{
|
||||
...localOptions,
|
||||
fetchRoutes: [
|
||||
reposRoute(),
|
||||
pagedSessionsRoute([firstPage, secondPage]),
|
||||
messagesRoute(),
|
||||
],
|
||||
},
|
||||
)
|
||||
|
||||
expect(await screen.findByText('Second page session')).toBeInTheDocument()
|
||||
|
||||
const sessionRequests = result.fetchFake.findRequests('GET', localSessionsPath)
|
||||
expect(sessionRequests).toHaveLength(2)
|
||||
expect(sessionRequests[1]?.searchParams.get('cursor')).toBe('1')
|
||||
})
|
||||
|
||||
it('does not load further pages once the oldest loaded session is older than the range start', async () => {
|
||||
const firstPage = Array.from({ length: 100 }, (_, index) =>
|
||||
makeSession({
|
||||
id: `ses_first_${index}`,
|
||||
title: `First page ${index}`,
|
||||
cost: 0.5,
|
||||
tokens: spentTokens,
|
||||
time: { created: 1000, updated: 10_000 - index * 60 },
|
||||
}),
|
||||
)
|
||||
const secondPage = [
|
||||
makeSession({
|
||||
id: 'ses_beyond_0',
|
||||
title: 'Beyond range session',
|
||||
cost: 10,
|
||||
tokens: spentTokens,
|
||||
time: { created: 1000, updated: 9000 },
|
||||
}),
|
||||
]
|
||||
|
||||
const result = renderWithProviders(
|
||||
<UsageSessionsTable serverId={LOCAL_OPENCODE_SERVER_ID} range={{ from: 5000 }} />,
|
||||
{
|
||||
...localOptions,
|
||||
fetchRoutes: [
|
||||
reposRoute(),
|
||||
pagedSessionsRoute([firstPage, secondPage]),
|
||||
messagesRoute(),
|
||||
],
|
||||
},
|
||||
)
|
||||
|
||||
await screen.findByText('First page 0')
|
||||
await waitFor(() =>
|
||||
expect(result.fetchFake.findRequests('GET', localMessagesPath)).toHaveLength(10),
|
||||
)
|
||||
|
||||
expect(result.fetchFake.findRequests('GET', localSessionsPath)).toHaveLength(1)
|
||||
expect(screen.queryByText('Beyond range session')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('notes when ranking stops at the 500 most recently active sessions', async () => {
|
||||
const pages = Array.from({ length: 6 }, (_, pageIndex) =>
|
||||
Array.from({ length: 100 }, (_, index) =>
|
||||
makeSession({
|
||||
id: `ses_cap_${pageIndex}_${index}`,
|
||||
title: `Cap session ${pageIndex}-${index}`,
|
||||
cost: 0.01,
|
||||
tokens: spentTokens,
|
||||
time: { created: 1000, updated: 10_000 - (pageIndex * 100 + index) },
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
const result = renderWithProviders(
|
||||
<UsageSessionsTable serverId={LOCAL_OPENCODE_SERVER_ID} range={{ from: 0 }} />,
|
||||
{
|
||||
...localOptions,
|
||||
fetchRoutes: [reposRoute(), pagedSessionsRoute(pages), messagesRoute()],
|
||||
},
|
||||
)
|
||||
|
||||
await waitFor(
|
||||
() => expect(result.fetchFake.findRequests('GET', localSessionsPath)).toHaveLength(5),
|
||||
{ timeout: 5000 },
|
||||
)
|
||||
expect(
|
||||
screen.getByText('Ranked from the 500 most recently active sessions.'),
|
||||
).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('marks a truncated step count with a plus', async () => {
|
||||
const sessions = [
|
||||
makeSession({
|
||||
id: 'ses_long',
|
||||
title: 'Long session',
|
||||
cost: 0.4,
|
||||
tokens: spentTokens,
|
||||
time: { created: 1000, updated: 2000 },
|
||||
}),
|
||||
]
|
||||
|
||||
const result = renderWithProviders(
|
||||
<UsageSessionsTable serverId={LOCAL_OPENCODE_SERVER_ID} range={{ from: 0 }} />,
|
||||
{
|
||||
...localOptions,
|
||||
fetchRoutes: [
|
||||
reposRoute(),
|
||||
sessionsRoute(sessions),
|
||||
pagedMessagesRoute([200, 200, 200, 200, 200]),
|
||||
],
|
||||
},
|
||||
)
|
||||
|
||||
await screen.findByText('Long session')
|
||||
|
||||
await waitFor(() => expect(cellsFor('Long session')[1]).toHaveTextContent('1000+'))
|
||||
expect(cellsFor('Long session')[5]).toHaveTextContent('50.0 tok/s')
|
||||
expect(result.fetchFake.findRequests('GET', localMessagesPath)).toHaveLength(5)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user