app-build-meta.test.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // web-admin/scripts/app-build-meta.test.ts
  2. import assert from 'node:assert/strict'
  3. import fs from 'node:fs'
  4. import os from 'node:os'
  5. import path from 'node:path'
  6. import { describe, it } from 'node:test'
  7. import {
  8. createAppBuildMeta,
  9. readGitShort,
  10. versionJsonText,
  11. writeVersionJson
  12. } from './app-build-meta.ts'
  13. describe('createAppBuildMeta', () => {
  14. it('joins git short hash and timestamp', () => {
  15. assert.deepEqual(createAppBuildMeta('3.0.2', 1730000000000, 'a1b2c3d'), {
  16. version: '3.0.2',
  17. buildId: 'a1b2c3d-1730000000000'
  18. })
  19. })
  20. it('uses nogit when the hash is empty', () => {
  21. assert.equal(createAppBuildMeta('3.0.2', 5, ' ').buildId, 'nogit-5')
  22. })
  23. it('changes buildId when the timestamp changes', () => {
  24. const a = createAppBuildMeta('3.0.2', 1, 'abc')
  25. const b = createAppBuildMeta('3.0.2', 2, 'abc')
  26. assert.notEqual(a.buildId, b.buildId)
  27. })
  28. })
  29. describe('readGitShort', () => {
  30. it('trims command output', () => {
  31. assert.equal(readGitShort(() => 'abc123\n'), 'abc123')
  32. })
  33. it('returns nogit when the command throws or is blank', () => {
  34. assert.equal(
  35. readGitShort(() => {
  36. throw new Error('no git')
  37. }),
  38. 'nogit'
  39. )
  40. assert.equal(readGitShort(() => ' '), 'nogit')
  41. })
  42. })
  43. describe('version.json', () => {
  44. it('writes pretty json with a trailing newline', () => {
  45. const text = versionJsonText({ version: '3.0.2', buildId: 'abc-1' })
  46. assert.equal(text, '{\n "version": "3.0.2",\n "buildId": "abc-1"\n}\n')
  47. })
  48. it('writes dist/version.json', () => {
  49. const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'app-ver-'))
  50. writeVersionJson(dir, { version: '3.0.3', buildId: 'def-9' })
  51. const raw = fs.readFileSync(path.join(dir, 'version.json'), 'utf8')
  52. assert.deepEqual(JSON.parse(raw), { version: '3.0.3', buildId: 'def-9' })
  53. })
  54. })