Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
import { describe, it, expect, vi } from 'vitest'
import {
previewKindFor,
extensionOf,
basenameOf,
isAssetKind,
unresolvedRefs,
externalRefs,
previewUrlFor,
resolveInRoot,
resolveInRoots,
opensInDefaultApp,
openOrReveal,
} from '@/lib/coworkPreview'
describe('previewKindFor', () => {
it('maps the renderable types', () => {
expect(previewKindFor('a/index.html')).toBe('html')
expect(previewKindFor('a/logo.svg')).toBe('svg')
expect(previewKindFor('README.md')).toBe('markdown')
expect(previewKindFor('shot.PNG')).toBe('image')
expect(previewKindFor('clip.mp4')).toBe('video')
expect(previewKindFor('track.mp3')).toBe('audio')
expect(previewKindFor('main.rs')).toBe('text')
})
it('falls back to a file card for unknown and binary types', () => {
// These must not be rendered as text — `read_file_sync` is
// `read_to_string` and would fail on them anyway.
expect(previewKindFor('deck.pptx')).toBe('file')
expect(previewKindFor('report.pdf')).toBe('file')
expect(previewKindFor('archive.zip')).toBe('file')
expect(previewKindFor('bin/tool')).toBe('file')
})
it('treats a dotfile as having no extension, not an extension', () => {
expect(extensionOf('.gitignore')).toBe('')
expect(previewKindFor('.gitignore')).toBe('file')
})
it('reads the extension from the basename, not an earlier path segment', () => {
expect(extensionOf('my.dir/file')).toBe('')
expect(previewKindFor('v1.2/notes.md')).toBe('markdown')
})
it('handles windows separators', () => {
expect(basenameOf('a\\b\\index.html')).toBe('index.html')
expect(previewKindFor('a\\b\\index.html')).toBe('html')
})
it('routes only image/video through the asset protocol', () => {
expect(isAssetKind('image')).toBe(true)
expect(isAssetKind('video')).toBe(true)
expect(isAssetKind('html')).toBe(false)
expect(isAssetKind('text')).toBe(false)
})
})
describe('unresolvedRefs', () => {
// The pane renders into an opaque-origin sandbox with no base URL, so these
// silently fail to load. Counting them is what lets us say so.
it('counts relative src/href that the sandbox cannot resolve', () => {
expect(unresolvedRefs('<img src="./logo.png">')).toBe(1)
expect(unresolvedRefs('<link href="style.css"><script src="app.js">')).toBe(2)
expect(unresolvedRefs("<img src='sub/dir/a.png'>")).toBe(1)
})
it('does not count anything that resolves on its own', () => {
expect(unresolvedRefs('<img src="data:image/png;base64,AAA">')).toBe(0)
expect(unresolvedRefs('<script src="https://cdn.example/x.js">')).toBe(0)
expect(unresolvedRefs('<a href="#section">x</a>')).toBe(0)
expect(unresolvedRefs('<img src="blob:abc">')).toBe(0)
})
it('is zero for a self-contained page, which is the common generated case', () => {
const page = `<html><style>body{color:red}</style><script>1</script></html>`
expect(unresolvedRefs(page)).toBe(0)
})
it('ignores empty values', () => {
expect(unresolvedRefs('<img src="">')).toBe(0)
})
// about:srcdoc has no scheme or host to inherit, so `//cdn` is as dead as
// `./x.png`.
it('counts protocol-relative refs, which have nothing to inherit from', () => {
expect(unresolvedRefs('<img src="//cdn.example/x.png">')).toBe(1)
})
})
describe('externalRefs', () => {
it('counts http(s) refs and nothing else', () => {
expect(
externalRefs(
'<script src="https://cdn.example/phaser.js"></script>' +
'<link href="http://x.dev/a.css"><img src="data:image/png;base64,AA">' +
'<img src="./local.png"><a href="#top">x</a>'
)
).toBe(2)
expect(externalRefs('<canvas></canvas><script>run()</script>')).toBe(0)
})
})
describe('previewUrlFor', () => {
it('keeps the directory structure so relative assets resolve', () => {
expect(
previewUrlFor('/home/me/proj/index.html', 'http://preview.localhost/%2F')
).toBe('http://preview.localhost/home/me/proj/index.html')
expect(previewUrlFor('/a/b.html', 'preview://localhost/%2F')).toBe(
'preview://localhost/a/b.html'
)
})
it('encodes segments and normalizes a windows path', () => {
expect(
previewUrlFor('C:\\Users\\me\\my game\\index.html', 'http://preview.localhost/x')
).toBe('http://preview.localhost/C%3A/Users/me/my%20game/index.html')
})
})
describe('resolveInRoot', () => {
const root = '/Users/me/proj'
it('resolves a relative path', () => {
expect(resolveInRoot(root, 'index.html')).toBe('/Users/me/proj/index.html')
expect(resolveInRoot(root, 'a/b.md')).toBe('/Users/me/proj/a/b.md')
})
it('accepts an absolute path inside the root', () => {
// Regression: the agent reports absolute paths for some writes and
// relative for others; refusing absolute rejected in-project files.
expect(resolveInRoot(root, '/Users/me/proj/flappy.html')).toBe(
'/Users/me/proj/flappy.html'
)
})
it('refuses anything that lands outside the root', () => {
expect(resolveInRoot(root, '../secrets.env')).toBeNull()
expect(resolveInRoot(root, 'a/../../etc/passwd')).toBeNull()
expect(resolveInRoot(root, '/etc/passwd')).toBeNull()
// Sibling dir sharing a name prefix must not pass the prefix check.
expect(resolveInRoot(root, '/Users/me/proj-other/x')).toBeNull()
})
it('collapses . and trailing slashes', () => {
expect(resolveInRoot(root + '/', './a.md')).toBe('/Users/me/proj/a.md')
expect(resolveInRoot(root, 'a/./b/../c.md')).toBe('/Users/me/proj/a/c.md')
})
it('handles windows paths', () => {
expect(resolveInRoot('C:\\proj', 'a\\b.html')).toBe('C:/proj/a/b.html')
expect(resolveInRoot('C:\\proj', 'C:\\other\\x')).toBeNull()
})
it('compares containment case-insensitively (Windows/macOS volumes are)', () => {
expect(resolveInRoot('/Users/me/Proj', '/users/me/proj/a.html')).toBe(
'/users/me/proj/a.html'
)
expect(resolveInRoot('c:/proj', 'C:/Proj/a.html')).toBe('C:/Proj/a.html')
// Still refuses a genuinely different directory.
expect(resolveInRoot('/Users/me/Proj', '/Users/me/Other/a.html')).toBeNull()
})
it('is null on empty input', () => {
expect(resolveInRoot(root, '')).toBeNull()
expect(resolveInRoot('', 'a.md')).toBeNull()
})
})
describe('resolveInRoots', () => {
const sandbox = '/data/agent-workspace/sessions/s1'
const folder = '/home/me/My Slides'
it('resolves against the sandbox and returns the matching root', () => {
expect(resolveInRoots([sandbox, folder], 'deck.html')).toEqual({
abs: `${sandbox}/deck.html`,
root: sandbox,
})
})
it('resolves a file that lives in the attached folder (#8875)', () => {
expect(resolveInRoots([sandbox, folder], `${folder}/deck.html`)).toEqual({
abs: `${folder}/deck.html`,
root: folder,
})
})
it('skips null roots and returns null when nothing contains the path', () => {
expect(resolveInRoots([null, sandbox], '/etc/passwd')).toBeNull()
expect(resolveInRoots([null, null], 'a.md')).toBeNull()
})
})
describe('opensInDefaultApp', () => {
it('opens documents, media and text', () => {
for (const p of [
'/w/report.pdf',
'/w/chart.PNG',
'C:\\w\\notes.md',
'/w/data.csv',
'/w/page.html',
'/w/deck.pptx',
'/w/clip.mp4',
'/w/song.mp3',
'/w/logo.svg',
]) {
expect(opensInDefaultApp(p), p).toBe(true)
}
})
it('never opens anything the OS would run', () => {
for (const p of [
'/w/run.command',
'/w/Thing.app',
'/w/link.lnk',
'/w/x.desktop',
'/w/setup.exe',
'/w/a.bat',
'/w/a.cmd',
'/w/a.ps1',
'/w/a.sh',
'/w/a.terminal',
'/w/a.scpt',
'/w/a.jar',
'/w/a.js',
'/w/a.py',
'/w/a.vbs',
'/w/report.pdf.command',
'/w/noext',
'/w/.bashrc',
]) {
expect(opensInDefaultApp(p), p).toBe(false)
}
})
})
describe('openOrReveal', () => {
const opener = () => ({
openArtifact: vi.fn().mockResolvedValue(undefined),
revealItemInDir: vi.fn().mockResolvedValue(undefined),
})
it('opens an allowed type through the host check, with the session roots', async () => {
const o = opener()
await openOrReveal(o, [null, '/w'], '/w/report.pdf')
expect(o.openArtifact).toHaveBeenCalledWith(['/w'], '/w/report.pdf')
expect(o.revealItemInDir).not.toHaveBeenCalled()
})
// The name passed, but the file did not (e.g. a link to a program).
it('reveals a file the host refuses to open', async () => {
const o = opener()
o.openArtifact.mockRejectedValueOnce(new Error('is a link'))
await openOrReveal(o, ['/w'], '/w/report.pdf')
expect(o.revealItemInDir).toHaveBeenCalledWith('/w/report.pdf')
})
it('reveals anything else instead of opening it', async () => {
const o = opener()
await openOrReveal(o, ['/w'], '/w/run.command')
expect(o.revealItemInDir).toHaveBeenCalledWith('/w/run.command')
expect(o.openArtifact).not.toHaveBeenCalled()
})
})