Extract JavaScript UI framework functionality into dedicated testdrive-jsui capability while maintaining 100% functionality preservation and integrating JavaScript tests into the main Python test suite. Phase 1 (Foundation Setup) - COMPLETED: - Created capability directory structure with proper Python package layout - Configured pyproject.toml with Node.js subprocess dependencies - Set up package.json with Jest + JSDOM testing framework - Implemented Python-JavaScript bridge for seamless test integration - Created comprehensive capability Makefile with all testing targets - Added detailed README documentation for capability usage Phase 2 (Integration Layer) - COMPLETED: - Built Python test wrappers for JavaScript test execution via subprocess - Integrated with pytest discovery system for unified test experience - Added capability targets to main Makefile delegation system - Verified test integration works with main test suite Phase 3 (Safe Migration) - COMPLETED: - Copied (not moved) all JavaScript files to capability using safe copy-first approach - Migrated 4 core JavaScript components and 11 test files (2,840+ lines) - Verified all tests work in new location (11 Python tests + 7 JavaScript tests passing) - Maintained dual-track testing capability for safety during transition Phase 4 (Framework Enhancement) - COMPLETED: - Enhanced testing framework with Python integration and coverage reporting - Achieved 59% Python test coverage and 100% JavaScript test coverage - Added performance benchmarking and component documentation Phase 5 (Production Integration) - COMPLETED: - Added standard 'test' target to capability Makefile for discovery system compatibility - Integrated JavaScript tests into main Makefile with new targets: * test-js: Run JavaScript UI tests * test-all: Run all tests (Python + JavaScript + Capabilities) - Updated help documentation to include new testing workflows - Verified capability auto-discovery works via 'make test-capabilities' Key Achievements: - Zero-risk migration completed with copy-first safety approach - Full Python-JavaScript test integration with 18 total passing tests - JavaScript UI framework successfully extracted to dedicated capability - Enhanced CI/CD integration with unified test command interface - Clean architecture enabling future JavaScript framework evolution Testing Status: - ✅ All Python integration tests passing (11/11) - ✅ All JavaScript component tests passing (7/7) - ✅ Capability discovery integration working - ✅ Main test suite integration complete - ✅ Test coverage reporting functional (59% Python, 100% JavaScript) 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
122 lines
3.8 KiB
JavaScript
122 lines
3.8 KiB
JavaScript
'use strict'
|
|
|
|
const hexify = char => {
|
|
const h = char.charCodeAt(0).toString(16).toUpperCase()
|
|
return '0x' + (h.length % 2 ? '0' : '') + h
|
|
}
|
|
|
|
const parseError = (e, txt, context) => {
|
|
if (!txt) {
|
|
return {
|
|
message: e.message + ' while parsing empty string',
|
|
position: 0,
|
|
}
|
|
}
|
|
const badToken = e.message.match(/^Unexpected token (.) .*position\s+(\d+)/i)
|
|
const errIdx = badToken ? +badToken[2]
|
|
: e.message.match(/^Unexpected end of JSON.*/i) ? txt.length - 1
|
|
: null
|
|
|
|
const msg = badToken ? e.message.replace(/^Unexpected token ./, `Unexpected token ${
|
|
JSON.stringify(badToken[1])
|
|
} (${hexify(badToken[1])})`)
|
|
: e.message
|
|
|
|
if (errIdx !== null && errIdx !== undefined) {
|
|
const start = errIdx <= context ? 0
|
|
: errIdx - context
|
|
|
|
const end = errIdx + context >= txt.length ? txt.length
|
|
: errIdx + context
|
|
|
|
const slice = (start === 0 ? '' : '...') +
|
|
txt.slice(start, end) +
|
|
(end === txt.length ? '' : '...')
|
|
|
|
const near = txt === slice ? '' : 'near '
|
|
|
|
return {
|
|
message: msg + ` while parsing ${near}${JSON.stringify(slice)}`,
|
|
position: errIdx,
|
|
}
|
|
} else {
|
|
return {
|
|
message: msg + ` while parsing '${txt.slice(0, context * 2)}'`,
|
|
position: 0,
|
|
}
|
|
}
|
|
}
|
|
|
|
class JSONParseError extends SyntaxError {
|
|
constructor (er, txt, context, caller) {
|
|
context = context || 20
|
|
const metadata = parseError(er, txt, context)
|
|
super(metadata.message)
|
|
Object.assign(this, metadata)
|
|
this.code = 'EJSONPARSE'
|
|
this.systemError = er
|
|
Error.captureStackTrace(this, caller || this.constructor)
|
|
}
|
|
get name () { return this.constructor.name }
|
|
set name (n) {}
|
|
get [Symbol.toStringTag] () { return this.constructor.name }
|
|
}
|
|
|
|
const kIndent = Symbol.for('indent')
|
|
const kNewline = Symbol.for('newline')
|
|
// only respect indentation if we got a line break, otherwise squash it
|
|
// things other than objects and arrays aren't indented, so ignore those
|
|
// Important: in both of these regexps, the $1 capture group is the newline
|
|
// or undefined, and the $2 capture group is the indent, or undefined.
|
|
const formatRE = /^\s*[{\[]((?:\r?\n)+)([\s\t]*)/
|
|
const emptyRE = /^(?:\{\}|\[\])((?:\r?\n)+)?$/
|
|
|
|
const parseJson = (txt, reviver, context) => {
|
|
const parseText = stripBOM(txt)
|
|
context = context || 20
|
|
try {
|
|
// get the indentation so that we can save it back nicely
|
|
// if the file starts with {" then we have an indent of '', ie, none
|
|
// otherwise, pick the indentation of the next line after the first \n
|
|
// If the pattern doesn't match, then it means no indentation.
|
|
// JSON.stringify ignores symbols, so this is reasonably safe.
|
|
// if the string is '{}' or '[]', then use the default 2-space indent.
|
|
const [, newline = '\n', indent = ' '] = parseText.match(emptyRE) ||
|
|
parseText.match(formatRE) ||
|
|
[, '', '']
|
|
|
|
const result = JSON.parse(parseText, reviver)
|
|
if (result && typeof result === 'object') {
|
|
result[kNewline] = newline
|
|
result[kIndent] = indent
|
|
}
|
|
return result
|
|
} catch (e) {
|
|
if (typeof txt !== 'string' && !Buffer.isBuffer(txt)) {
|
|
const isEmptyArray = Array.isArray(txt) && txt.length === 0
|
|
throw Object.assign(new TypeError(
|
|
`Cannot parse ${isEmptyArray ? 'an empty array' : String(txt)}`
|
|
), {
|
|
code: 'EJSONPARSE',
|
|
systemError: e,
|
|
})
|
|
}
|
|
|
|
throw new JSONParseError(e, parseText, context, parseJson)
|
|
}
|
|
}
|
|
|
|
// Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
|
|
// because the buffer-to-string conversion in `fs.readFileSync()`
|
|
// translates it to FEFF, the UTF-16 BOM.
|
|
const stripBOM = txt => String(txt).replace(/^\uFEFF/, '')
|
|
|
|
module.exports = parseJson
|
|
parseJson.JSONParseError = JSONParseError
|
|
|
|
parseJson.noExceptions = (txt, reviver) => {
|
|
try {
|
|
return JSON.parse(stripBOM(txt), reviver)
|
|
} catch (e) {}
|
|
}
|