1package ui
2
3import (
4 "testing"
5
6 "github.com/stretchr/testify/require"
7
8 "github.com/bnema/dumber/internal/application/port"
9 "github.com/bnema/dumber/internal/ui/component"
10)
11
12func TestDownloadEventAdapterToastSpecLifecycle(t *testing.T) {
13 adapter := newDownloadEventAdapter(nil)
14 event := port.DownloadEvent{
15 Filename: "archlinux.iso",
16 Destination: "/tmp/archlinux.iso",
17 }
18
19 started := adapter.toastSpecForEvent(port.DownloadEvent{
20 Type: port.DownloadEventStarted,
21 Filename: event.Filename,
22 Destination: event.Destination,
23 })
24 require.True(t, started.show)
25 require.Equal(t, component.ToastInfo, started.level)
26 require.Equal(t, 0, started.duration)
27 require.Equal(t, "Download started: archlinux.iso (0%)", started.message)
28
29 progress := adapter.toastSpecForEvent(port.DownloadEvent{
30 Type: port.DownloadEventProgress,
31 Filename: event.Filename,
32 Destination: event.Destination,
33 Progress: 0.37,
34 })
35 require.True(t, progress.show)
36 require.Equal(t, component.ToastInfo, progress.level)
37 require.Equal(t, 0, progress.duration)
38 require.Equal(t, "Downloading: archlinux.iso (37%)", progress.message)
39
40 finished := adapter.toastSpecForEvent(port.DownloadEvent{
41 Type: port.DownloadEventFinished,
42 Filename: event.Filename,
43 Destination: event.Destination,
44 })
45 require.True(t, finished.show)
46 require.Equal(t, component.ToastSuccess, finished.level)
47 require.Equal(t, downloadToastTerminalDuration, finished.duration)
48 require.Equal(t, "Download complete: archlinux.iso", finished.message)
49}
50
51func TestDownloadEventAdapterToastSpecSuppressesDuplicateProgress(t *testing.T) {
52 adapter := newDownloadEventAdapter(nil)
53 event := port.DownloadEvent{
54 Filename: "ubuntu.iso",
55 Destination: "/tmp/ubuntu.iso",
56 }
57
58 adapter.toastSpecForEvent(port.DownloadEvent{
59 Type: port.DownloadEventStarted,
60 Filename: event.Filename,
61 Destination: event.Destination,
62 })
63
64 first := adapter.toastSpecForEvent(port.DownloadEvent{
65 Type: port.DownloadEventProgress,
66 Filename: event.Filename,
67 Destination: event.Destination,
68 Progress: 0.12,
69 })
70 require.True(t, first.show)
71
72 duplicate := adapter.toastSpecForEvent(port.DownloadEvent{
73 Type: port.DownloadEventProgress,
74 Filename: event.Filename,
75 Destination: event.Destination,
76 Progress: 0.12,
77 })
78 require.False(t, duplicate.show)
79}
80
81func TestDownloadProgressPercentRoundsReasonably(t *testing.T) {
82 require.Equal(t, 0, downloadProgressPercent(0))
83 require.Equal(t, 1, downloadProgressPercent(0.0051))
84 require.Equal(t, 37, downloadProgressPercent(0.37))
85 require.Equal(t, 100, downloadProgressPercent(1.2))
86}