Files
opencode-manager/frontend/src/lib/artifactTool.test.ts
T
Chris Scott 5e2452172c fix(artifacts): render artifacts from the checked path and serve raw files once
- show_artifact returns the validated path as tool metadata; the chat card
  renders from it and maps repo paths with getWorkspaceFilePath, so local
  repos and moved sessions load the right file
- serve .htm as text/html
- raw file route stats and reads once and adds ETag/Last-Modified with 304
- reuse isPathWithinRoot from utils/fs-safe
- move readShowArtifactCall to frontend/src/lib/artifactTool
2026-10-12 16:43:04 -04:00

82 lines
2.6 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import type { SessionMessageAssistantTool } from '@opencode-manager/shared/opencode'
import { readShowArtifactCall } from './artifactTool'
const ocmPart = (
params: unknown,
options: { status?: 'running' | 'completed'; metadata?: { path: string } } = {},
): SessionMessageAssistantTool => ({
type: 'tool',
id: 't1',
name: 'ocm',
time: { created: 1 },
state:
options.status === 'running'
? {
status: 'running',
input: { action: 'show_artifact', params },
metadata: {},
}
: {
status: 'completed',
input: { action: 'show_artifact', params },
content: [{ type: 'text', text: 'Artifact shown.' }],
metadata: options.metadata,
},
})
describe('readShowArtifactCall', () => {
it('returns the validated title and path without a resolved path', () => {
expect(readShowArtifactCall(ocmPart({ title: 'Report', path: 'reports/summary.html' }))).toEqual({
title: 'Report',
path: 'reports/summary.html',
resolvedPath: undefined,
})
})
it('returns the backend-resolved absolute path when metadata carries one', () => {
expect(
readShowArtifactCall(
ocmPart(
{ title: 'Report', path: 'reports/summary.html' },
{ metadata: { path: '/workspace/sessions/ses_1/report.html' } },
),
),
).toEqual({
title: 'Report',
path: 'reports/summary.html',
resolvedPath: '/workspace/sessions/ses_1/report.html',
})
})
it('ignores an empty metadata path', () => {
expect(
readShowArtifactCall(ocmPart({ title: 'Report', path: 'a.html' }, { metadata: { path: '' } }))?.resolvedPath,
).toBeUndefined()
})
it('returns undefined for params that do not validate', () => {
expect(readShowArtifactCall(ocmPart({ title: 'No path' }))).toBeUndefined()
expect(readShowArtifactCall(ocmPart('not-an-object'))).toBeUndefined()
})
it('returns undefined for an unfinished call', () => {
expect(readShowArtifactCall(ocmPart({ title: 'Report', path: 'a.html' }, { status: 'running' }))).toBeUndefined()
})
it('returns undefined for another action', () => {
const part: SessionMessageAssistantTool = {
type: 'tool',
id: 't1',
name: 'ocm',
time: { created: 1 },
state: {
status: 'completed',
input: { action: 'open_walkthrough', params: { title: 'Report', path: 'a.html' } },
content: [{ type: 'text', text: 'Opened.' }],
},
}
expect(readShowArtifactCall(part)).toBeUndefined()
})
})