MCPcopy Create free account
hub / github.com/diillson/chatcli / TestHandleVersionCommand

Function TestHandleVersionCommand

cli/cli_test.go:39–93  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

37}
38
39func TestHandleVersionCommand(t *testing.T) {
40 logger, _ := zap.NewDevelopment()
41 cliInstance := &ChatCLI{logger: logger}
42 handler := NewCommandHandler(cliInstance)
43
44 originalCheckImpl := version.CheckLatestVersionImpl
45 originalBuildImpl := version.GetBuildInfoImpl
46
47 mockChecker := new(mockVersionChecker)
48
49 version.CheckLatestVersionImpl = func(ctx context.Context) (string, bool, error) {
50 return mockChecker.Check(ctx)
51 }
52 version.GetBuildInfoImpl = func() (string, string, string) {
53 return "1.25.0", "abc1234", "2024-09-15"
54 }
55 defer func() {
56 version.CheckLatestVersionImpl = originalCheckImpl
57 version.GetBuildInfoImpl = originalBuildImpl
58 }()
59
60 testCases := []struct {
61 name string
62 mockLatest string
63 mockUpdate bool
64 mockErr error
65 expectOut string
66 }{
67 {"Update available", "1.26.0", true, nil, "Disponível! Atualize"},
68 {"No update", "1.25.0", false, nil, "Você está na versão mais recente."},
69 {"With error", "", false, errors.New("network error"), "Não foi possível verificar: network error"},
70 }
71
72 for _, tc := range testCases {
73 t.Run(tc.name, func(t *testing.T) {
74 mockChecker.On("Check", mock.Anything).Return(tc.mockLatest, tc.mockUpdate, tc.mockErr).Once()
75
76 oldStdout := os.Stdout
77 r, w, _ := os.Pipe()
78 os.Stdout = w
79 defer func() { os.Stdout = oldStdout }()
80
81 handler.handleVersionCommand(context.Background())
82
83 w.Close()
84 out, _ := io.ReadAll(r)
85
86 cleanOut := stripANSI(string(out))
87 normalized := normalizeSpaces(cleanOut)
88
89 assert.Contains(t, normalized, tc.expectOut)
90 mockChecker.AssertExpectations(t)
91 })
92 }
93}

Callers

nothing calls this directly

Calls 7

handleVersionCommandMethod · 0.95
NewCommandHandlerFunction · 0.85
stripANSIFunction · 0.70
normalizeSpacesFunction · 0.70
CheckMethod · 0.65
RunMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected