main 6af0a61

preflight_handler_test.go RawHistoryFolder

1package handlers
2
3import (
4	"context"
5	"testing"
6
7	"github.com/bnema/archup/internal/application/commands"
8	"github.com/bnema/archup/internal/domain/ports/mocks"
9	"go.uber.org/mock/gomock"
10)
11
12func TestPreflightHandler_Handle(t *testing.T) {
13	ctrl := gomock.NewController(t)
14	defer ctrl.Finish()
15
16	mockFS := mocks.NewMockFileSystem(ctrl)
17	mockExec := mocks.NewMockCommandExecutor(ctrl)
18	mockLogger := mocks.NewMockLogger(ctrl)
19
20	// Expect basic logging calls
21	mockLogger.EXPECT().Info(gomock.Any()).AnyTimes()
22	mockLogger.EXPECT().Info(gomock.Any(), gomock.Any()).AnyTimes()
23	mockLogger.EXPECT().Info(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
24	mockLogger.EXPECT().Warn(gomock.Any()).AnyTimes()
25	mockLogger.EXPECT().Warn(gomock.Any(), gomock.Any()).AnyTimes()
26	mockLogger.EXPECT().Error(gomock.Any(), gomock.Any()).AnyTimes()
27	mockLogger.EXPECT().Error(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
28
29	// Mock filesystem and executor responses
30	mockFS.EXPECT().Exists(gomock.Any()).DoAndReturn(func(path string) (bool, error) {
31		switch path {
32		case "/etc/arch-release":
33			return true, nil
34		case "/sys/firmware/efi/efivars":
35			return true, nil
36		default:
37			return false, nil
38		}
39	}).AnyTimes()
40	mockExec.EXPECT().Execute(gomock.Any(), "id", "-u").Return([]byte("0\n"), nil).Times(1)
41	mockExec.EXPECT().Execute(gomock.Any(), "uname", "-m").Return([]byte("x86_64\n"), nil).Times(1)
42	mockExec.EXPECT().Execute(gomock.Any(), "bootctl", "status").Return([]byte("Secure Boot: disabled"), nil).Times(2)
43	mockExec.EXPECT().Execute(gomock.Any(), "ping", "-c", "1", "archlinux.org").Return([]byte{}, nil).Times(1)
44	mockExec.EXPECT().Execute(gomock.Any(), "grep", "model name", "/proc/cpuinfo").Return([]byte("model name  : Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz\n"), nil).Times(1)
45
46	handler := NewPreflightHandler(mockFS, mockExec, mockLogger)
47	result, err := handler.Handle(context.Background(), commands.PreflightCommand{})
48
49	if err != nil {
50		t.Fatalf("expected no error, got %v", err)
51	}
52
53	if !result.ChecksPassed {
54		t.Error("expected checks to pass")
55	}
56
57	if result.SystemInfo.Architecture != "x86_64" {
58		t.Errorf("expected architecture x86_64, got %s", result.SystemInfo.Architecture)
59	}
60
61	if !result.SystemInfo.IsUEFI {
62		t.Error("expected UEFI boot to be detected")
63	}
64}
65
66func TestPreflightHandler_NotRoot(t *testing.T) {
67	ctrl := gomock.NewController(t)
68	defer ctrl.Finish()
69
70	mockFS := mocks.NewMockFileSystem(ctrl)
71	mockExec := mocks.NewMockCommandExecutor(ctrl)
72	mockLogger := mocks.NewMockLogger(ctrl)
73
74	mockLogger.EXPECT().Info(gomock.Any()).AnyTimes()
75	mockLogger.EXPECT().Info(gomock.Any(), gomock.Any()).AnyTimes()
76	mockLogger.EXPECT().Info(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
77	mockLogger.EXPECT().Warn(gomock.Any()).AnyTimes()
78	mockLogger.EXPECT().Warn(gomock.Any(), gomock.Any()).AnyTimes()
79	mockLogger.EXPECT().Error(gomock.Any(), gomock.Any()).AnyTimes()
80	mockLogger.EXPECT().Error(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
81	mockFS.EXPECT().Exists(gomock.Any()).DoAndReturn(func(path string) (bool, error) {
82		switch path {
83		case "/etc/arch-release":
84			return true, nil
85		default:
86			return false, nil
87		}
88	}).AnyTimes()
89	mockExec.EXPECT().Execute(gomock.Any(), "id", "-u").Return([]byte("1000\n"), nil).Times(1)
90	mockExec.EXPECT().Execute(gomock.Any(), "bootctl", "status").Return([]byte("Secure Boot: disabled"), nil).AnyTimes()
91	mockExec.EXPECT().Execute(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
92	mockExec.EXPECT().Execute(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
93
94	handler := NewPreflightHandler(mockFS, mockExec, mockLogger)
95	result, err := handler.Handle(context.Background(), commands.PreflightCommand{})
96
97	if err != nil {
98		t.Fatalf("expected no error, got %v", err)
99	}
100
101	if result.ChecksPassed {
102		t.Error("expected checks to fail when not running as root")
103	}
104
105	if len(result.CriticalErrors) == 0 {
106		t.Error("expected critical errors to be recorded")
107	}
108}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 0

    ↑↓ moveenter openesc close