Files
opencode-manager/frontend/src/lib/utils.test.ts
T
ioplex 4ec62cd4e7 fix: address findings from the full Docker evaluation of main
Critical and high:
- repos: unlinking a local repo removes only the Manager reference and alias,
  never the repository files; confirm button reads "Unlink"
- auth: rate-limit by the socket address; trust proxy headers only when
  AUTH_TRUSTED_PROXY_HEADERS is set (documented and forwarded in compose)
- composer: per-session in-memory drafts (text, bash mode, attachments);
  Escape no longer clears the prompt
- url params: compose same-tick updates so schedule dialogs stay closed after
  create/delete; delete dialog names the job

Medium:
- tool rows show failures (non-zero shell exit, failed exploration members)
- voice conversation shows the specific error and a starting state
- settings: Escape in dropdowns and the shortcut recorder no longer closes
  the dialog; mobile deep links open the requested section; one content tree
  per platform
- closing pushed sheets, settings, tool panels and schedule dialogs goes back
  in history
- login returns to the requested page; signed-out pages make no
  authenticated requests
- action terminals keep their output (Manager-side output capture and replay)
- unpushed-commit detection, commit message kept on failure
- binary files with unknown extensions are detected and not editable
- tool panel resizes when docked and floats as a dismissible sheet when the
  page is too narrow
- Docker image runs tini as PID 1

Low: not-found states, add-repo and branch validation errors (4xx),
search-aware empty states, mobile repo sort/filter/select cues, sidebar
resize affordance and collapse shortcut, mobile unread badge, preview port
filtering, terminal close handling, walkthrough empty state and helper
session filtering, placeholder contrast, permission labels and focus,
TTS task-list sanitizing, git identity email validation, quieter git
logging, hermetic worktree tests, and accessible names for icon-only
controls.
2026-10-23 11:42:37 -04:00

263 lines
8.6 KiB
TypeScript

import { describe, it, expect, afterEach } from 'vitest'
import {
getRepoDisplayName,
getRepoBranchLabel,
sanitizeForTTS,
randomId,
formatShortRelativeTime,
getOriginOnlyBranchNames,
shortenPath,
} from './utils'
describe('shortenPath', () => {
it('keeps short paths and collapses the leading segments of long ones', () => {
expect(shortenPath('/')).toBe('/')
expect(shortenPath('')).toBe('/')
expect(shortenPath('/a/b/c')).toBe('/a/b/c')
expect(shortenPath('a/b')).toBe('/a/b')
expect(shortenPath('/Users/chris/development/oc-manager/workspace/repos/test')).toBe('/.../workspace/repos/test')
expect(shortenPath('/a/b/c/d/e', 4)).toBe('/.../b/c/d/e')
})
})
describe('sanitizeForTTS', () => {
it('should handle headers', () => {
expect(sanitizeForTTS('# Main Header\n## Sub Header\n### Detail')).toBe('Main Header\nSub Header\nDetail')
})
it('should handle bullet lists', () => {
expect(sanitizeForTTS('- Milk\n- Eggs\n* Bread')).toBe('Milk\nEggs\nBread')
})
it('should handle numbered lists', () => {
expect(sanitizeForTTS('1. First\n2. Second')).toBe('First\nSecond')
})
it('should remove inline code but keep content', () => {
expect(sanitizeForTTS('Use `const x = 1` here')).toBe('Use const x = 1 here')
})
it('should remove code blocks entirely', () => {
expect(sanitizeForTTS('Start\n```\ncode\n```\nEnd')).toBe('Start\nEnd')
})
it('should handle bold formatting', () => {
expect(sanitizeForTTS('This is **bold** text')).toBe('This is bold text')
})
it('should handle italic formatting', () => {
expect(sanitizeForTTS('Simple *italic* example')).toBe('Simple italic example')
})
it('should remove markdown links but keep display text', () => {
expect(sanitizeForTTS('Visit [OpenCode](https://opencode.ai)')).toBe('Visit OpenCode')
})
it('should handle images and tables', () => {
expect(sanitizeForTTS('See ![diagram](url) below:\n|A|B|\n|-|-|\n|1|2|')).toBe('See diagram below:\nA B\n1 2')
})
it('should handle blockquotes', () => {
expect(sanitizeForTTS('> Important\n> Note')).toBe('Important\nNote')
})
it('should remove citations and footnotes', () => {
expect(sanitizeForTTS('See [1] and [^2] for more')).toBe('See and for more')
})
it('should handle strikethrough', () => {
expect(sanitizeForTTS('~~removed~~')).toBe('removed')
})
it('should handle complex mixed content', () => {
const input = '# Results\nFunction: `calc()`\nSee [doc](url):\n- A\n- B'
const expected = 'Results\nFunction: calc()\nSee doc:\nA\nB'
expect(sanitizeForTTS(input)).toBe(expected)
})
it('should remove task-list markers but keep the task text', () => {
expect(sanitizeForTTS('- [x] task done\n- [ ] task pending')).toBe('task done\ntask pending')
expect(sanitizeForTTS('* [X] starred\n+ [ ] plus\n1. [x] numbered')).toBe('starred\nplus\nnumbered')
})
it('should remove nested task-list markers and checked variants', () => {
expect(sanitizeForTTS(' - [x] nested done\n - [ ] nested pending')).toBe('nested done\nnested pending')
})
it('should remove horizontal rules', () => {
expect(sanitizeForTTS('Before\n---\nAfter')).toBe('Before\nAfter')
})
it('should remove setext heading underlines', () => {
expect(sanitizeForTTS('Title\n===')).toBe('Title')
})
it('should normalize whitespace', () => {
expect(sanitizeForTTS('Line 1\n\n\nLine 2')).toBe('Line 1\nLine 2')
})
it('should fix punctuation spacing', () => {
expect(sanitizeForTTS('Hello , world !')).toBe('Hello, world!')
})
it('should return empty string for empty input', () => {
expect(sanitizeForTTS('')).toBe('')
expect(sanitizeForTTS(' ')).toBe('')
expect(sanitizeForTTS(null as unknown as string)).toBe('')
})
it('should handle headers and lists combined', () => {
expect(sanitizeForTTS('## Shopping List\n- Milk\n- Eggs')).toBe('Shopping List\nMilk\nEggs')
})
it('should handle HTML tags', () => {
expect(sanitizeForTTS('Text with <tag>content</tag> here')).toBe('Text with content here')
})
})
describe('getOriginOnlyBranchNames', () => {
it('returns short names for origin remote branches without a local branch', () => {
expect(
getOriginOnlyBranchNames([
{ name: 'main', type: 'local' },
{ name: 'remotes/origin/release', type: 'remote' },
]),
).toEqual(['release'])
})
it('excludes remote branches shadowed by a local branch', () => {
expect(
getOriginOnlyBranchNames([
{ name: 'release', type: 'local' },
{ name: 'remotes/origin/release', type: 'remote' },
]),
).toEqual([])
})
it('excludes non-origin remotes', () => {
expect(
getOriginOnlyBranchNames([
{ name: 'remotes/upstream/release', type: 'remote' },
{ name: 'remotes/origin/release', type: 'remote' },
]),
).toEqual(['release'])
})
it('excludes HEAD', () => {
expect(
getOriginOnlyBranchNames([
{ name: 'remotes/origin/HEAD', type: 'remote' },
{ name: 'remotes/origin/main', type: 'remote' },
]),
).toEqual(['main'])
})
it('excludes empty short names', () => {
expect(getOriginOnlyBranchNames([{ name: 'remotes/origin/', type: 'remote' }])).toEqual([])
})
it('dedupes while preserving input order', () => {
expect(
getOriginOnlyBranchNames([
{ name: 'remotes/origin/zeta', type: 'remote' },
{ name: 'remotes/origin/alpha', type: 'remote' },
{ name: 'remotes/origin/zeta', type: 'remote' },
{ name: 'remotes/origin/beta', type: 'remote' },
]),
).toEqual(['zeta', 'alpha', 'beta'])
})
})
describe('randomId', () => {
const originalRandomUUID = globalThis.crypto?.randomUUID
afterEach(() => {
if (globalThis.crypto) {
Object.defineProperty(globalThis.crypto, 'randomUUID', {
value: originalRandomUUID,
configurable: true,
writable: true,
})
}
})
it('uses crypto.randomUUID when available', () => {
Object.defineProperty(globalThis.crypto, 'randomUUID', {
value: () => '11111111-2222-4333-8444-555555555555',
configurable: true,
writable: true,
})
expect(randomId()).toBe('11111111-2222-4333-8444-555555555555')
})
it('falls back to a unique id when crypto.randomUUID is unavailable (non-secure context)', () => {
Object.defineProperty(globalThis.crypto, 'randomUUID', {
value: undefined,
configurable: true,
writable: true,
})
const first = randomId()
const second = randomId()
expect(first).toMatch(/^[a-z0-9]+-[a-z0-9]+$/)
expect(first).not.toBe(second)
})
})
describe('getRepoDisplayName', () => {
it('prefers name when present', () => {
expect(getRepoDisplayName({ name: 'Fork A', repoUrl: 'https://github.com/x/y.git' })).toBe('Fork A')
})
it('trims and ignores empty name', () => {
expect(getRepoDisplayName({ name: ' ', repoUrl: 'https://github.com/x/y.git' })).toBe('y')
})
it('falls back to repoUrl basename stripping .git', () => {
expect(getRepoDisplayName({ repoUrl: 'https://github.com/user/repo.git' })).toBe('repo')
})
it('falls back to sourcePath basename', () => {
expect(getRepoDisplayName({ sourcePath: '/home/user/projects/my-repo' })).toBe('my-repo')
})
it('falls back to the localPath basename', () => {
expect(getRepoDisplayName({ localPath: '/some/path' })).toBe('path')
})
it('falls back to Repository when all are empty', () => {
expect(getRepoDisplayName({})).toBe('Repository')
})
it('handles null values properly', () => {
expect(getRepoDisplayName({ repoUrl: null, localPath: null, sourcePath: null })).toBe('Repository')
})
})
describe('getRepoBranchLabel', () => {
it('prefers currentBranch', () => {
expect(getRepoBranchLabel({ currentBranch: 'feature/x', branch: 'main' })).toBe('feature/x')
})
it('falls back to branch', () => {
expect(getRepoBranchLabel({ branch: 'main' })).toBe('main')
})
it('returns null when neither is present', () => {
expect(getRepoBranchLabel({})).toBeNull()
})
})
describe('formatShortRelativeTime', () => {
const now = new Date('2026-01-01T12:00:00.000Z')
const at = (offsetMs: number) => new Date(now.getTime() - offsetMs)
it('formats each threshold boundary', () => {
expect(formatShortRelativeTime(at(30_000), now)).toBe('just now')
expect(formatShortRelativeTime(at(5 * 60_000), now)).toBe('5m ago')
expect(formatShortRelativeTime(at(3 * 60 * 60_000), now)).toBe('3h ago')
expect(formatShortRelativeTime(at(2 * 24 * 60 * 60_000), now)).toBe('2d ago')
expect(formatShortRelativeTime(at(14 * 24 * 60 * 60_000), now)).toBe('2w ago')
expect(formatShortRelativeTime(at(60 * 24 * 60 * 60_000), now)).toBe('2mo ago')
})
})