XuqmGroup-RNSDK/packages/update/scripts/native-baseline.test.mjs

97 行
3.8 KiB
JavaScript

import assert from 'node:assert/strict'
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import path from 'node:path'
import test from 'node:test'
import { computeNativeBaseline } from './native-baseline.mjs'
function fixture() {
const root = mkdtempSync(path.join(tmpdir(), 'xuqm-native-baseline-'))
mkdirSync(path.join(root, 'android', 'app', 'src', 'main', 'java'), { recursive: true })
mkdirSync(path.join(root, 'ios'), { recursive: true })
mkdirSync(path.join(root, 'node_modules', 'runtime-library'), { recursive: true })
mkdirSync(path.join(root, 'src'), { recursive: true })
writeFileSync(
path.join(root, 'package.json'),
JSON.stringify({ dependencies: { 'runtime-library': '^1.0.0' } }),
)
writeFileSync(
path.join(root, 'node_modules', 'runtime-library', 'package.json'),
JSON.stringify({ name: 'runtime-library', version: '1.0.0' }),
)
writeFileSync(path.join(root, 'android', 'settings.gradle'), 'rootProject.name = "fixture"')
writeFileSync(path.join(root, 'ios', 'App.swift'), 'struct App {}')
writeFileSync(path.join(root, 'src', 'business.ts'), 'export const value = 1')
return root
}
test('native source changes the baseline while ordinary JS does not', () => {
const root = fixture()
try {
const initial = computeNativeBaseline(root)
writeFileSync(path.join(root, 'src', 'business.ts'), 'export const value = 2')
assert.equal(computeNativeBaseline(root), initial)
writeFileSync(path.join(root, 'android', 'app', 'src', 'main', 'java', 'Main.kt'), 'class Main')
assert.notEqual(computeNativeBaseline(root), initial)
} finally {
rmSync(root, { recursive: true, force: true })
}
})
test('runtime dependencies and packaged assets require a new full-app baseline', () => {
const root = fixture()
try {
const initial = computeNativeBaseline(root, 'android')
writeFileSync(path.join(root, 'src', 'logo.png'), 'first-image')
const withAsset = computeNativeBaseline(root, 'android')
assert.notEqual(withAsset, initial)
writeFileSync(
path.join(root, 'node_modules', 'runtime-library', 'package.json'),
JSON.stringify({ name: 'runtime-library', version: '1.1.0' }),
)
assert.notEqual(computeNativeBaseline(root, 'android'), withAsset)
} finally {
rmSync(root, { recursive: true, force: true })
}
})
test('native baselines are platform-specific', () => {
const root = fixture()
try {
const android = computeNativeBaseline(root, 'android')
const ios = computeNativeBaseline(root, 'ios')
assert.match(android, /^android-sha256:/)
assert.match(ios, /^ios-sha256:/)
assert.notEqual(android, ios)
writeFileSync(path.join(root, 'ios', 'App.swift'), 'struct App { let value = 1 }')
assert.equal(computeNativeBaseline(root, 'android'), android)
assert.notEqual(computeNativeBaseline(root, 'ios'), ios)
} finally {
rmSync(root, { recursive: true, force: true })
}
})
test('native source from a local file dependency changes the full-app baseline', () => {
const root = fixture()
try {
const dependencyRoot = path.join(root, 'local-update-sdk')
const nativeFile = path.join(dependencyRoot, 'android', 'src', 'main', 'java', 'Bridge.java')
mkdirSync(path.dirname(nativeFile), { recursive: true })
writeFileSync(nativeFile, 'final class Bridge {}')
writeFileSync(
path.join(root, 'package.json'),
JSON.stringify({
dependencies: { 'runtime-library': '^1.0.0', '@xuqm/local': 'file:./local-update-sdk' },
}),
)
const initial = computeNativeBaseline(root, 'android')
writeFileSync(nativeFile, 'final class Bridge { int apiLevel = 2; }')
assert.notEqual(computeNativeBaseline(root, 'android'), initial)
} finally {
rmSync(root, { recursive: true, force: true })
}
})