Files
2026-07-13 22:16:02 +08:00

82 lines
2.3 KiB
JavaScript

const assert = require('node:assert/strict')
const fs = require('node:fs')
const os = require('node:os')
const path = require('node:path')
const test = require('node:test')
const { install } = require('./install')
function writeExecutable(file, content) {
fs.writeFileSync(file, content, { mode: 0o755 })
}
async function captureLogs(t, fn) {
const oldLog = console.log
const logs = []
console.log = (...args) => logs.push(args.join(' '))
t.after(() => {
console.log = oldLog
})
await fn()
console.log = oldLog
return logs.join('\n')
}
test('postinstall installs skills from the npm package skills directory when binary already exists', async (t) => {
const temp = fs.mkdtempSync(path.join(os.tmpdir(), 'mediakit-postinstall-'))
const bin = path.join(temp, 'bin')
fs.mkdirSync(bin)
const log = path.join(temp, 'calls.jsonl')
writeExecutable(
path.join(bin, 'npx'),
`#!/usr/bin/env node
const fs = require('node:fs')
fs.appendFileSync(${JSON.stringify(log)}, JSON.stringify({ cmd: 'npx', args: process.argv.slice(2) }) + '\\n')
process.exit(0)
`,
)
t.after(() => fs.rmSync(temp, { recursive: true, force: true }))
const oldPath = process.env.PATH
const oldHome = process.env.HOME
const oldSkip = process.env.MEDIAKIT_CLI_SKIP_DOWNLOAD
process.env.PATH = `${bin}${path.delimiter}${oldPath || ''}`
process.env.HOME = temp
delete process.env.MEDIAKIT_CLI_SKIP_DOWNLOAD
t.after(() => {
process.env.PATH = oldPath
process.env.HOME = oldHome
if (oldSkip === undefined) {
delete process.env.MEDIAKIT_CLI_SKIP_DOWNLOAD
} else {
process.env.MEDIAKIT_CLI_SKIP_DOWNLOAD = oldSkip
}
})
const output = await captureLogs(t, () => install())
const calls = fs
.readFileSync(log, 'utf8')
.trim()
.split('\n')
.map((line) => JSON.parse(line))
assert.equal(calls.length, 1)
assert.deepEqual(calls[0].args, [
'-y',
'skills',
'add',
path.join(__dirname, '..', 'skills'),
'-g',
'-y',
])
assert.match(output, /Installing skills \.\.\./)
assert.match(output, /✓ Skills installed/)
const state = JSON.parse(
fs.readFileSync(path.join(temp, '.mediakit', 'skills-state.json'), 'utf8'),
)
assert.equal(state.package_name, '@volcengine/mediakit-cli')
assert.equal(state.version, '0.1.7')
assert.equal(state.skills_dir, path.join(__dirname, '..', 'skills'))
})