forked from OurHike/OurHike
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdownloadActivity.test.ts
More file actions
118 lines (105 loc) · 4.98 KB
/
Copy pathdownloadActivity.test.ts
File metadata and controls
118 lines (105 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { describe, it, expect } from 'vitest'
import { activeDownload, downloadPercent } from './downloadActivity'
import type { DownloadStatus } from '../screens/DownloadCard'
// The question the footer link asks: is anything happening right now, and how
// far along is it. What is worth asserting here is that "nothing" and
// "something" are told apart correctly - a bar over a download that has
// stopped is a phone claiming to be working, and silence over one that is
// running is the whole defect this exists to fix.
const DOWNLOADING: DownloadStatus = {
state: 'downloading',
receivedBytes: 40,
totalBytes: 100,
}
const CHECKING: DownloadStatus = { state: 'checking', checkedBytes: 25, totalBytes: 100 }
describe('activeDownload', () => {
it('reports nothing when nothing is moving', () => {
expect(activeDownload([])).toBeNull()
expect(activeDownload([{ state: 'not-downloaded' }])).toBeNull()
expect(
activeDownload([{ state: 'downloaded', totalBytes: 100, completedAt: new Date() }]),
).toBeNull()
})
it('reports a transfer, with what is here against what is owed', () => {
expect(activeDownload([DOWNLOADING])).toEqual({
kind: 'downloading',
doneBytes: 40,
totalBytes: 100,
})
})
it('says nothing about a download that has STOPPED', () => {
// "Stopped at 40 MB of 100" is the card's sentence, and it is not this
// one. A bar in the footer over a failed transfer would tell a hiker the
// phone is still working on something it has given up on - and the tap
// that would fix it is the one they would not make.
expect(
activeDownload([{ state: 'failed', receivedBytes: 40, totalBytes: 100 }]),
).toBeNull()
expect(activeDownload([{ state: 'hash-mismatch' }])).toBeNull()
expect(activeDownload([{ state: 'evicted', completedAt: null }])).toBeNull()
})
it('reports the phone reading its own disk as its own kind of wait', () => {
// #197's distinction, carried to the footer: checking looks exactly like a
// stalled transfer and asks the opposite thing of someone in a dead spot.
expect(activeDownload([CHECKING])).toEqual({
kind: 'checking',
doneBytes: 25,
totalBytes: 100,
})
})
it('lets a live transfer outrank a check', () => {
// lib/backgroundStatus.ts's precedence, for its reason: one figure in a
// footer, and the one spending signal is the one worth the room.
expect(activeDownload([CHECKING, DOWNLOADING])?.kind).toBe('downloading')
})
it('sums the sheets that are moving and ignores the ones that are not', () => {
// A hiker downloading two sheets is waiting on both, and two bars in a
// footer is a footer nobody reads. The resting sheet contributes nothing:
// counting a map somebody is not downloading towards a total would put the
// bar at a figure no transfer is working towards.
expect(
activeDownload([
DOWNLOADING,
{ state: 'downloading', receivedBytes: 10, totalBytes: 300 },
{ state: 'not-downloaded' },
]),
).toEqual({ kind: 'downloading', doneBytes: 50, totalBytes: 400 })
})
it('reports the canary before any archive has been asked for', () => {
// The step the whole tap used to disappear into: 12 MB of trail data, no
// archive in any state yet, and so nothing at all to report. It carries
// no figure on purpose - four fetches of unannounced size have no honest
// percentage, and inventing one would be worse than the word.
expect(activeDownload([{ state: 'not-downloaded' }], true)).toEqual({
kind: 'preparing',
})
})
it('lets a live transfer outrank a canary', () => {
// Two sheets, one already moving: a word must not replace a percentage.
expect(activeDownload([DOWNLOADING], true)?.kind).toBe('downloading')
expect(activeDownload([CHECKING], true)?.kind).toBe('checking')
})
it('counts a finished piece of a sheet that is still arriving', () => {
// Not this module's doing - lib/backgroundStatus.ts has already folded the
// archives of one sheet into one figure, which is exactly why this is fed
// sheet statuses and not archives. Asserted anyway, because the property
// it buys is the visible one: a bar that never travels backwards when a
// piece of a multi-archive sheet completes.
expect(
activeDownload([{ state: 'downloading', receivedBytes: 700, totalBytes: 1000 }]),
).toEqual({ kind: 'downloading', doneBytes: 700, totalBytes: 1000 })
})
})
describe('downloadPercent', () => {
it('rounds to a whole percent', () => {
expect(downloadPercent(1, 3)).toBe(33)
expect(downloadPercent(2, 3)).toBe(67)
})
it('calls an undeclared length the start of something, not the end', () => {
// A server that sent no content-length leaves totalBytes at zero. 0% is
// true of a transfer that has begun; a division by zero would render
// "NaN%", and 100% would be a finished download that is not finished.
expect(downloadPercent(0, 0)).toBe(0)
expect(downloadPercent(500, 0)).toBe(0)
})
})