mirror of https://codeberg.org/pzp/pzp-sync.git
fix algorithm wantRange plus test it
This commit is contained in:
parent
20be886872
commit
574d43f1a6
|
@ -72,10 +72,14 @@ class Algorithm {
|
||||||
#wantNewestRange(localHaveRange, remoteHaveRange, count) {
|
#wantNewestRange(localHaveRange, remoteHaveRange, count) {
|
||||||
const [minLocalHave, maxLocalHave] = localHaveRange
|
const [minLocalHave, maxLocalHave] = localHaveRange
|
||||||
const [minRemoteHave, maxRemoteHave] = remoteHaveRange
|
const [minRemoteHave, maxRemoteHave] = remoteHaveRange
|
||||||
if (maxRemoteHave <= maxLocalHave) return EMPTY_RANGE
|
if (maxRemoteHave < minLocalHave) return EMPTY_RANGE
|
||||||
const maxWant = maxRemoteHave
|
const maxWant = maxRemoteHave
|
||||||
const size = Math.max(maxWant - maxLocalHave, count)
|
const size = Math.max(maxWant - maxLocalHave, count)
|
||||||
const minWant = Math.max(maxWant - size, minRemoteHave)
|
const minWant = Math.max(
|
||||||
|
maxWant - size + 1,
|
||||||
|
maxLocalHave - size + 1,
|
||||||
|
minRemoteHave
|
||||||
|
)
|
||||||
return [minWant, maxWant]
|
return [minWant, maxWant]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,12 +92,18 @@ class Algorithm {
|
||||||
wantRange(localHave, remoteHave, goal) {
|
wantRange(localHave, remoteHave, goal) {
|
||||||
if (!goal) return EMPTY_RANGE
|
if (!goal) return EMPTY_RANGE
|
||||||
if (isEmptyRange(remoteHave)) return EMPTY_RANGE
|
if (isEmptyRange(remoteHave)) return EMPTY_RANGE
|
||||||
if (goal.type === 'all' || goal.type === 'set' || goal.type === 'record') {
|
|
||||||
return this.#wantAllRange(localHave, remoteHave)
|
switch (goal.type) {
|
||||||
} else if (goal.type === 'newest') {
|
case 'all':
|
||||||
return this.#wantNewestRange(localHave, remoteHave, goal.count)
|
return this.#wantAllRange(localHave, remoteHave)
|
||||||
|
|
||||||
|
case 'newest':
|
||||||
|
return this.#wantNewestRange(localHave, remoteHave, goal.count)
|
||||||
|
|
||||||
|
case 'none':
|
||||||
|
default:
|
||||||
|
return EMPTY_RANGE
|
||||||
}
|
}
|
||||||
return EMPTY_RANGE
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
const test = require('node:test')
|
||||||
|
const assert = require('node:assert')
|
||||||
|
const p = require('node:util').promisify
|
||||||
|
const Algorithm = require('../lib/algorithm')
|
||||||
|
|
||||||
|
test('want-range for goal=newest-3', async (t) => {
|
||||||
|
const algo = new Algorithm({ db: null })
|
||||||
|
const goal = { type: 'newest', count: 3 }
|
||||||
|
|
||||||
|
assert.deepStrictEqual(algo.wantRange([2, 4], [1, 3], goal), [2, 3])
|
||||||
|
assert.deepStrictEqual(algo.wantRange([2, 4], [1, 5], goal), [3, 5])
|
||||||
|
assert.deepStrictEqual(algo.wantRange([1, 3], [2, 4], goal), [2, 4])
|
||||||
|
assert.deepStrictEqual(algo.wantRange([1, 5], [2, 4], goal), [3, 4])
|
||||||
|
assert.deepStrictEqual(algo.wantRange([1, 3], [4, 6], goal), [4, 6])
|
||||||
|
assert.deepStrictEqual(algo.wantRange([4, 6], [1, 3], goal), [1, 0])
|
||||||
|
assert.deepStrictEqual(algo.wantRange([1, 3], [6, 7], goal), [6, 7])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('want-range for goal=all', async (t) => {
|
||||||
|
const algo = new Algorithm({ db: null })
|
||||||
|
const goal = { type: 'all' }
|
||||||
|
|
||||||
|
assert.deepStrictEqual(algo.wantRange([2, 4], [1, 3], goal), [1, 3])
|
||||||
|
assert.deepStrictEqual(algo.wantRange([2, 4], [1, 5], goal), [1, 5])
|
||||||
|
assert.deepStrictEqual(algo.wantRange([1, 3], [2, 4], goal), [2, 4])
|
||||||
|
assert.deepStrictEqual(algo.wantRange([1, 5], [2, 4], goal), [2, 4])
|
||||||
|
assert.deepStrictEqual(algo.wantRange([1, 3], [4, 6], goal), [4, 6])
|
||||||
|
assert.deepStrictEqual(algo.wantRange([4, 6], [1, 3], goal), [1, 3])
|
||||||
|
assert.deepStrictEqual(algo.wantRange([1, 3], [6, 7], goal), [6, 7])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('want-range for goal=record', async (t) => {
|
||||||
|
const algo = new Algorithm({ db: null })
|
||||||
|
const goal = { type: 'record' }
|
||||||
|
|
||||||
|
assert.deepStrictEqual(algo.wantRange([2, 4], [1, 3], goal), [2, 3])
|
||||||
|
assert.deepStrictEqual(algo.wantRange([2, 4], [1, 5], goal), [2, 5])
|
||||||
|
assert.deepStrictEqual(algo.wantRange([1, 3], [2, 4], goal), [2, 4])
|
||||||
|
assert.deepStrictEqual(algo.wantRange([1, 5], [2, 4], goal), [2, 4])
|
||||||
|
assert.deepStrictEqual(algo.wantRange([1, 3], [4, 6], goal), [4, 6])
|
||||||
|
assert.deepStrictEqual(algo.wantRange([4, 6], [1, 3], goal), [1, 0])
|
||||||
|
assert.deepStrictEqual(algo.wantRange([1, 3], [6, 7], goal), [6, 7])
|
||||||
|
})
|
Loading…
Reference in New Issue