import test from 'brittle'
import { pickUpdate, compareSemver, parseSha256 } from '../app/update-pick.js'
const BASE = 'https://7rnx.net/git'
const row = (version, file, repo = 'cthuloid') => ({ repo, version, path: repo + '/versions/' + version + '/' + file })
const release = (v) => [row('android-v' + v, 'dist/cthuloid-lights.apk'), row('android-v' + v, 'dist/cthuloid-lights.apk.sha256')]
test('newest complete release by semver, not by list position', (t) => {
// The forge lists newest first; 0.10.0 > 0.9.0 numerically, not as text.
const files = [...release('0.9.0'), ...release('0.10.0'), ...release('0.2.0')]
const u = pickUpdate(files, '0.1.0', BASE)
t.is(u.version, '0.10.0')
t.is(u.apkUrl, BASE + '/releases/cthuloid/android-v0.10.0/dist/cthuloid-lights.apk')
t.is(u.shaUrl, BASE + '/releases/cthuloid/android-v0.10.0/dist/cthuloid-lights.apk.sha256')
})
test('a release missing its .sha256 (half replicated) is skipped', (t) => {
const files = [...release('0.1.1'), row('android-v0.1.2', 'dist/cthuloid-lights.apk')]
t.is(pickUpdate(files, '0.1.0', BASE).version, '0.1.1')
})
test('nothing newer than installed -> null', (t) => {
t.is(pickUpdate([...release('0.1.2'), ...release('0.1.1')], '0.1.2', BASE), null)
t.is(pickUpdate(release('0.1.1'), '0.1.2', BASE), null)
t.is(pickUpdate([], '0.1.0', BASE), null)
})
test('other repos, branch builds and non-semver tags are ignored', (t) => {
const files = [
...release('0.1.1'),
row('android-v9.0.0', 'dist/cthuloid-lights.apk', 'other'), row('android-v9.0.0', 'dist/cthuloid-lights.apk.sha256', 'other'),
row('master-d85fb4c', 'dist/skipped.txt'),
row('android-v2.0', 'dist/cthuloid-lights.apk'), row('android-v2.0', 'dist/cthuloid-lights.apk.sha256'),
row('android-v3.0.0-rc1', 'dist/cthuloid-lights.apk'), row('android-v3.0.0-rc1', 'dist/cthuloid-lights.apk.sha256')
]
t.is(pickUpdate(files, '0.1.0', BASE).version, '0.1.1')
})
test('an unparseable installed version never offers an update', (t) => {
t.is(pickUpdate(release('0.1.1'), 'dev', BASE), null)
t.is(pickUpdate(release('0.1.1'), null, BASE), null)
})
test('compareSemver', (t) => {
t.ok(compareSemver('0.1.10', '0.1.9') > 0)
t.ok(compareSemver('1.0.0', '0.99.99') > 0)
t.is(compareSemver('0.1.2', '0.1.2'), 0)
t.ok(compareSemver('0.1.2', '0.2.0') < 0)
})
test('parseSha256 reads sha256sum output', (t) => {
const hex = '9d541c3029794698fc1f0dd25a8c33cc397214b537d324078dda6dc3a5ce002f'
t.is(parseSha256(hex + ' cthuloid-lights.apk\n'), hex)
t.is(parseSha256(hex.toUpperCase()), hex)
t.is(parseSha256('not a hash'), null)
t.is(parseSha256(''), null)
})