108 lines
5.0 KiB
JavaScript
108 lines
5.0 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import { createHash } from 'node:crypto'
|
|
import fs from 'node:fs/promises'
|
|
import path from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
import React from 'react'
|
|
import { renderToStaticMarkup } from 'react-dom/server'
|
|
import { parse } from '@babel/parser'
|
|
import traverseModule from '@babel/traverse'
|
|
import {
|
|
RecoveredVirtualizer,
|
|
useRecoveredToolVirtualizer,
|
|
} from '../../src/renderer/src/workbuddy/conversation/recovered-virtualizer/index.mjs'
|
|
|
|
const traverse = traverseModule.default
|
|
const workspaceRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..')
|
|
const artifactDirectory = path.join(workspaceRoot, 'src/renderer/src/workbuddy/conversation/recovered-virtualizer')
|
|
const manifest = JSON.parse(await fs.readFile(path.join(artifactDirectory, 'manifest.json'), 'utf8'))
|
|
const digest = value => createHash('sha256').update(value).digest('hex')
|
|
|
|
assert.equal(manifest.kind, 'rea-recovered-tool-virtualizer')
|
|
assert.equal(manifest.evidence.evidence_id, 'ev_717fa5e846e64d67ac19c8925d121bff632c0d3aefb3603b10858559cabb2dcb')
|
|
assert.equal(manifest.evidence.export_sha256.length, 64)
|
|
const evidenceBytes = await fs.readFile(path.join(workspaceRoot, manifest.evidence.export_path))
|
|
assert.equal(evidenceBytes.length, manifest.evidence.export_bytes)
|
|
assert.equal(digest(evidenceBytes), manifest.evidence.export_sha256)
|
|
const evidence = JSON.parse(evidenceBytes.toString('utf8'))
|
|
assert.equal(evidence.evidence_id, manifest.evidence.evidence_id)
|
|
|
|
for (const artifact of manifest.evidence.selected_artifacts) {
|
|
const matches = evidence.items.filter(item => item.source?.artifact?.sha256 === artifact.sha256 &&
|
|
item.file_path === artifact.path)
|
|
assert.equal(matches.length, 1, `REA source identity for ${artifact.fileName}`)
|
|
const source = matches[0].source.artifact.text
|
|
assert.equal(Buffer.byteLength(source, 'utf8'), artifact.bytes, `REA source bytes for ${artifact.fileName}`)
|
|
assert.equal(digest(source), artifact.sha256, `REA source SHA-256 for ${artifact.fileName}`)
|
|
}
|
|
|
|
for (const output of manifest.outputs) {
|
|
const outputBytes = await fs.readFile(path.join(artifactDirectory, output.path))
|
|
assert.equal(outputBytes.length, output.bytes, `generated bytes for ${output.path}`)
|
|
assert.equal(digest(outputBytes), output.sha256, `generated SHA-256 for ${output.path}`)
|
|
}
|
|
|
|
assert.deepEqual(manifest.imports.unresolved, [])
|
|
assert.deepEqual(manifest.imports.output, ['react', 'react-dom'])
|
|
assert.deepEqual(manifest.rolldown_runtime_closure.external_imports, [])
|
|
assert.deepEqual(manifest.rolldown_runtime_closure.restored_helpers.map(helper => helper.export).sort(), ['__esmMin', '__toESM'])
|
|
assert.equal(manifest.rolldown_runtime_closure.source_sha256,
|
|
manifest.evidence.selected_artifacts.find(artifact => artifact.fileName === 'rolldown-runtime-BJCASTsb.js').sha256)
|
|
|
|
const recoveredEngine = await fs.readFile(path.join(artifactDirectory, 'index.mjs'), 'utf8')
|
|
const recoveredAst = parse(recoveredEngine, { sourceType: 'module' })
|
|
let recoveredProgram
|
|
traverse(recoveredAst, {
|
|
Program(programPath) {
|
|
programPath.scope.crawl()
|
|
recoveredProgram = programPath
|
|
programPath.stop()
|
|
},
|
|
})
|
|
assert.deepEqual(recoveredAst.program.body.filter(statement => statement.type === 'ImportDeclaration')
|
|
.map(statement => statement.source.value).sort(), ['react', 'react-dom'])
|
|
for (const binding of Object.values(manifest.rolldown_runtime_closure.renamed_bindings)) {
|
|
assert.ok(recoveredProgram.scope.getBinding(binding), `recovered Rolldown binding ${binding}`)
|
|
}
|
|
for (const helper of manifest.rolldown_runtime_closure.restored_helpers) {
|
|
const adapter = recoveredProgram.scope.getBinding(helper.local)
|
|
assert.ok(adapter, `Rolldown adapter ${helper.local}`)
|
|
assert.equal(adapter.path.node.init.name, helper.binding)
|
|
}
|
|
|
|
let ssrItems = []
|
|
let ssrTotalSize = 0
|
|
function VirtualizedProbe() {
|
|
const virtualizer = useRecoveredToolVirtualizer({
|
|
count: 80,
|
|
getScrollElement: () => null,
|
|
estimateSize: () => 20,
|
|
overscan: 12,
|
|
initialRect: { width: 0, height: 260 },
|
|
})
|
|
ssrItems = virtualizer.getVirtualItems().map(item => item.index)
|
|
ssrTotalSize = virtualizer.getTotalSize()
|
|
return React.createElement('div', { 'data-count': ssrItems.length })
|
|
}
|
|
|
|
renderToStaticMarkup(React.createElement(VirtualizedProbe))
|
|
assert.equal(ssrTotalSize, 1600)
|
|
assert.deepEqual(ssrItems, Array.from({ length: 25 }, (_, index) => index))
|
|
|
|
const core = new RecoveredVirtualizer({
|
|
count: 100,
|
|
getScrollElement: () => null,
|
|
estimateSize: () => 20,
|
|
overscan: 12,
|
|
initialRect: { width: 0, height: 260 },
|
|
})
|
|
assert.deepEqual(core.getVirtualItems().map(item => item.index), Array.from({ length: 25 }, (_, index) => index))
|
|
|
|
core.scrollOffset = 500
|
|
assert.deepEqual(core.getVirtualItems().map(item => item.index), Array.from({ length: 37 }, (_, index) => index + 13))
|
|
|
|
core.scrollOffset = 1500
|
|
assert.deepEqual(core.getVirtualItems().map(item => item.index), Array.from({ length: 37 }, (_, index) => index + 63))
|
|
|
|
console.log('Recovered WorkBuddy tool virtualizer Evidence, output digests, imports, SSR and scroll ranges passed.')
|