Files
shanxia2.0/web-admin/scripts/app-build-meta.test.ts
T

62 lines
1.8 KiB
TypeScript

// web-admin/scripts/app-build-meta.test.ts
import assert from 'node:assert/strict'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { describe, it } from 'node:test'
import {
createAppBuildMeta,
readGitShort,
versionJsonText,
writeVersionJson
} from './app-build-meta.ts'
describe('createAppBuildMeta', () => {
it('joins git short hash and timestamp', () => {
assert.deepEqual(createAppBuildMeta('3.0.2', 1730000000000, 'a1b2c3d'), {
version: '3.0.2',
buildId: 'a1b2c3d-1730000000000'
})
})
it('uses nogit when the hash is empty', () => {
assert.equal(createAppBuildMeta('3.0.2', 5, ' ').buildId, 'nogit-5')
})
it('changes buildId when the timestamp changes', () => {
const a = createAppBuildMeta('3.0.2', 1, 'abc')
const b = createAppBuildMeta('3.0.2', 2, 'abc')
assert.notEqual(a.buildId, b.buildId)
})
})
describe('readGitShort', () => {
it('trims command output', () => {
assert.equal(readGitShort(() => 'abc123\n'), 'abc123')
})
it('returns nogit when the command throws or is blank', () => {
assert.equal(
readGitShort(() => {
throw new Error('no git')
}),
'nogit'
)
assert.equal(readGitShort(() => ' '), 'nogit')
})
})
describe('version.json', () => {
it('writes pretty json with a trailing newline', () => {
const text = versionJsonText({ version: '3.0.2', buildId: 'abc-1' })
assert.equal(text, '{\n "version": "3.0.2",\n "buildId": "abc-1"\n}\n')
})
it('writes dist/version.json', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'app-ver-'))
writeVersionJson(dir, { version: '3.0.3', buildId: 'def-9' })
const raw = fs.readFileSync(path.join(dir, 'version.json'), 'utf8')
assert.deepEqual(JSON.parse(raw), { version: '3.0.3', buildId: 'def-9' })
})
})