Files
opencode-manager/ocm-cli/test/progress.test.ts
T
Chris Scott 01e50a7019 feat: add bundle-based OCM mirror sync (#289)
* feat: add bundle-based OCM mirror sync

* feat: add OpenCode model editor and HEAD-safe bundle import

* fix: consolidate discovery-cache, batch bundle refs, remove dead code (#290)

* fix: consolidate discovery-cache, batch bundle refs, remove dead code

* refactor: consolidate discovery cache into generic discoverCached, extract spawnGit helper

* feat: extract project-id-resolver, add CLI full-fallback prompt, wire lint into root scripts

- Extract shared project-id-resolver for origin URL matching (replaces local-repo duplicates)
- Add promptYesNo / confirmFullFallback to ocm CLI before slow full mirror
- Backend: consolidate opencode-workspaces routes with project-id-resolver
- Frontend: fix schedules prompt/global page navigation
- CLI: add eslint config, lint/lint:fix scripts, fix no-fallthrough in ocm.ts
- Root: wire CLI into build, test, typecheck, lint, lint:fix chains
- Fix test lint issues (_code, const, require→import)

* fix: replace polynomial regex with linear-time string ops in gitRemoteParts

* feat: add divergence guards for push/pull, streaming bundle upload, server-side ancestry check

- Backend: GET /:repoId/mirror/head (head+branch+dirty) and GET /:repoId/mirror/contains/:sha (ancestry check)
- CLI: checkPushDivergence warns before overwriting server commits; checkPullDivergence warns before discarding local commits; both require confirm or --force
- Bundle upload now streams from disk instead of buffering entire file in memory
- Version visible in --help and default (no-arg) output
- Version bump 0.1.5 → 0.2.0

* fix: show connecting message before fetching repos in default command

* fix: update default command messages to reflect actual matching behavior
2026-06-28 11:57:38 -04:00

178 lines
5.3 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { formatBytes, createProgressReporter } from '../src/progress'
import type { ProgressSink } from '../src/progress'
describe('formatBytes', () => {
it('returns "0 B" for 0', () => {
expect(formatBytes(0)).toBe('0 B')
})
it('formats values below 1024 as whole bytes', () => {
expect(formatBytes(1)).toBe('1 B')
expect(formatBytes(500)).toBe('500 B')
expect(formatBytes(1023)).toBe('1023 B')
})
it('formats values at 1024 as KB with one decimal', () => {
expect(formatBytes(1024)).toBe('1.0 KB')
expect(formatBytes(1536)).toBe('1.5 KB')
})
it('formats values at 1 MB as MB with one decimal', () => {
expect(formatBytes(1_048_576)).toBe('1.0 MB')
expect(formatBytes(13_000_000)).toBe('12.4 MB')
})
it('formats values at 1 GB as GB with one decimal', () => {
expect(formatBytes(1_073_741_824)).toBe('1.0 GB')
expect(formatBytes(1_500_000_000)).toBe('1.4 GB')
})
})
describe('createProgressReporter', () => {
function createSink(isTTY: boolean): { sink: ProgressSink; writes: string[] } {
const writes: string[] = []
const sink: ProgressSink = {
write(chunk: string) {
writes.push(chunk)
},
isTTY,
}
return { sink, writes }
}
describe('TTY tick', () => {
it('output begins with carriage return and clear line and uses spinner frames', () => {
let fakeTime = 100_000
const now = () => fakeTime
const { sink, writes } = createSink(true)
const reporter = createProgressReporter('Test', sink, now)
// First tick at t=100000
reporter.tick(500)
expect(writes).toHaveLength(1)
expect(writes[0]).toMatch(/^\r\x1b\[K/)
// First frame is '⠋'
expect(writes[0]).toContain('⠋')
expect(writes[0]).toContain('500 B')
// Advance time past throttle window
fakeTime += 100
reporter.tick(1024)
expect(writes).toHaveLength(2)
// Second frame is '⠙'
expect(writes[1]).toContain('⠙')
expect(writes[1]).toContain('1.0 KB')
})
it('throttles renders to at least 80ms apart', () => {
let fakeTime = 100_000
const now = () => fakeTime
const { sink, writes } = createSink(true)
const reporter = createProgressReporter('Test', sink, now)
reporter.tick(100)
expect(writes).toHaveLength(1)
fakeTime += 40
reporter.tick(200)
expect(writes).toHaveLength(1)
fakeTime += 40
reporter.tick(300)
expect(writes).toHaveLength(2)
})
it('wraps spinner frames and never renders undefined after 10+ throttled ticks', () => {
let fakeTime = 100_000
const now = () => fakeTime
const { sink, writes } = createSink(true)
const reporter = createProgressReporter('Test', sink, now)
// Render 12 ticks, each 100ms apart (past the 80ms throttle)
for (let i = 0; i < 12; i++) {
reporter.tick(i * 100)
fakeTime += 100
}
expect(writes).toHaveLength(12)
// No write should contain "undefined"
for (const w of writes) {
expect(w).not.toContain('undefined')
}
// After 10 frames, the 11th write cycles back to the first frame '⠋'
// writes[10] is the 11th rendered tick → frameIndex = (0 + 10) % 10 = 0 → '⠋'
expect(writes[10]).toContain('⠋')
// writes[11] is the 12th rendered tick → frameIndex = (0 + 11) % 10 = 1 → '⠙'
expect(writes[11]).toContain('⠙')
})
})
describe('non-TTY tick', () => {
it('writes a line at most once per 1000ms', () => {
let fakeTime = 100_000
const now = () => fakeTime
const { sink, writes } = createSink(false)
const reporter = createProgressReporter('Test', sink, now)
reporter.tick(500)
expect(writes).toHaveLength(1)
expect(writes[0]).toBe('Test: 500 B\n')
// Too soon
fakeTime += 500
reporter.tick(1024)
expect(writes).toHaveLength(1)
// After 1000ms total
fakeTime += 500
reporter.tick(2048)
expect(writes).toHaveLength(2)
expect(writes[1]).toBe('Test: 2.0 KB\n')
})
})
describe('done()', () => {
it('clears the progress line on TTY', () => {
const { sink, writes } = createSink(true)
const reporter = createProgressReporter('Test', sink)
reporter.done()
expect(writes).toHaveLength(1)
expect(writes[0]).toBe('\r\x1b[K')
})
it('is a no-op on non-TTY', () => {
const { sink, writes } = createSink(false)
const reporter = createProgressReporter('Test', sink)
reporter.done()
expect(writes).toHaveLength(0)
})
it('is idempotent — calling done() twice on TTY only writes once', () => {
const { sink, writes } = createSink(true)
const reporter = createProgressReporter('Test', sink)
reporter.done()
reporter.done()
expect(writes).toHaveLength(1)
})
})
describe('finished guard', () => {
it('tick is a no-op after done()', () => {
const fakeTime = 100_000
const now = () => fakeTime
const { sink, writes } = createSink(true)
const reporter = createProgressReporter('Test', sink, now)
reporter.done()
const before = writes.length
reporter.tick(999)
expect(writes.length).toBe(before)
})
})
})