Files
bare-operating-system/packages/bare-os-coreutils/test/baretop-incremental.test.mjs
T
2026-04-05 17:38:46 -04:00

33 lines
1.1 KiB
JavaScript

import test from 'brittle'
import { readFile } from 'node:fs/promises'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { createContext, runInContext } from 'node:vm'
const __dirname = dirname(fileURLToPath(import.meta.url))
const composePath = join(__dirname, '../lib/baretop-compose.js')
test('incremental line-diff patch is smaller than full frame when one line changes', async (t) => {
const compose = await readFile(composePath, 'utf8')
const ctx = createContext({})
runInContext(compose, ctx)
const n = 48
const prev = Array.from({ length: n }, (_, i) => 'row-' + i + ' ' + 'x'.repeat(40))
const next = prev.slice()
next[22] = 'row-22 ' + 'CHANGED'.repeat(6)
const full = next.join('\r\n') + '\r\n'
const cup = (r, c) => '\x1b[' + r + ';' + c + 'H'
const { patch, nch } = ctx.bareTopBuildLinePatch(prev, next, cup, {
useLineHash: false
})
t.is(nch, 1)
t.ok(
patch.length < full.length,
'expected patch bytes < full frame (' +
patch.length +
' vs ' +
full.length +
')'
)
})