1+ package main
2+
3+ import (
4+ "bytes"
5+ "os"
6+ "runtime"
7+ "strings"
8+ "testing"
9+
10+ "github.com/nilock/tuido/utils"
11+ )
12+
13+ func TestShowVersionInfo (t * testing.T ) {
14+ // Capture stdout
15+ oldStdout := os .Stdout
16+ r , w , _ := os .Pipe ()
17+ os .Stdout = w
18+
19+ showVersionInfo ()
20+
21+ w .Close ()
22+ os .Stdout = oldStdout
23+
24+ var buf bytes.Buffer
25+ buf .ReadFrom (r )
26+ output := buf .String ()
27+
28+ // Test that output contains expected components
29+ expectedVersion := utils .Version ()
30+ expectedPlatform := runtime .GOOS + "/" + runtime .GOARCH
31+ expectedAsset := utils .BuildAssetName (expectedVersion , runtime .GOOS , runtime .GOARCH )
32+
33+ if ! strings .Contains (output , expectedVersion ) {
34+ t .Errorf ("Output should contain version %s, got: %s" , expectedVersion , output )
35+ }
36+
37+ if ! strings .Contains (output , expectedPlatform ) {
38+ t .Errorf ("Output should contain platform %s, got: %s" , expectedPlatform , output )
39+ }
40+
41+ if ! strings .Contains (output , expectedAsset ) {
42+ t .Errorf ("Output should contain asset name %s, got: %s" , expectedAsset , output )
43+ }
44+
45+ // Test that output has the expected structure
46+ lines := strings .Split (strings .TrimSpace (output ), "\n " )
47+ if len (lines ) < 3 {
48+ t .Errorf ("Expected at least 3 lines of output, got %d: %s" , len (lines ), output )
49+ }
50+
51+ // Test first line format
52+ if ! strings .HasPrefix (lines [0 ], "tuido " ) {
53+ t .Errorf ("First line should start with 'tuido ', got: %s" , lines [0 ])
54+ }
55+
56+ // Test second line format
57+ if ! strings .HasPrefix (lines [1 ], "Platform: " ) {
58+ t .Errorf ("Second line should start with 'Platform: ', got: %s" , lines [1 ])
59+ }
60+
61+ // Test third line format
62+ if ! strings .HasPrefix (lines [2 ], "Asset: " ) {
63+ t .Errorf ("Third line should start with 'Asset: ', got: %s" , lines [2 ])
64+ }
65+
66+ // Test fourth line format (Available or error message)
67+ if len (lines ) >= 4 {
68+ if ! strings .HasPrefix (lines [3 ], "Available: " ) {
69+ t .Errorf ("Fourth line should start with 'Available: ', got: %s" , lines [3 ])
70+ }
71+ }
72+ }
73+
74+ func TestVersionInfoContainsExpectedAssetName (t * testing.T ) {
75+ version := utils .Version ()
76+ goos := runtime .GOOS
77+ goarch := runtime .GOARCH
78+
79+ expectedAsset := utils .BuildAssetName (version , goos , goarch )
80+
81+ // Capture stdout
82+ oldStdout := os .Stdout
83+ r , w , _ := os .Pipe ()
84+ os .Stdout = w
85+
86+ showVersionInfo ()
87+
88+ w .Close ()
89+ os .Stdout = oldStdout
90+
91+ var buf bytes.Buffer
92+ buf .ReadFrom (r )
93+ output := buf .String ()
94+
95+ if ! strings .Contains (output , expectedAsset ) {
96+ t .Errorf ("Version info should contain asset name %s, but output was: %s" , expectedAsset , output )
97+ }
98+ }
99+
100+ func TestVersionInfoResilience (t * testing.T ) {
101+ // This test ensures that even if network calls fail,
102+ // the basic version info is still displayed
103+
104+ // Capture stdout
105+ oldStdout := os .Stdout
106+ r , w , _ := os .Pipe ()
107+ os .Stdout = w
108+
109+ showVersionInfo ()
110+
111+ w .Close ()
112+ os .Stdout = oldStdout
113+
114+ var buf bytes.Buffer
115+ buf .ReadFrom (r )
116+ output := buf .String ()
117+
118+ // Even if network fails, we should still get basic info
119+ if ! strings .Contains (output , "tuido" ) {
120+ t .Error ("Output should always contain 'tuido' even on network failure" )
121+ }
122+
123+ if ! strings .Contains (output , "Platform:" ) {
124+ t .Error ("Output should always contain 'Platform:' even on network failure" )
125+ }
126+
127+ if ! strings .Contains (output , "Asset:" ) {
128+ t .Error ("Output should always contain 'Asset:' even on network failure" )
129+ }
130+
131+ // Should either show "Available:" with asset info or error message
132+ hasAvailable := strings .Contains (output , "Available:" )
133+ if ! hasAvailable {
134+ t .Error ("Output should contain 'Available:' line with either asset info or error message" )
135+ }
136+ }
0 commit comments