|
| 1 | +// Copyright 2025. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package ui |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/Adembc/lazyssh/internal/core/domain" |
| 22 | +) |
| 23 | + |
| 24 | +func TestServersDifferIgnoresTransientFields(t *testing.T) { |
| 25 | + sf := &ServerForm{} |
| 26 | + |
| 27 | + // Create two servers with identical config but different transient fields |
| 28 | + server1 := domain.Server{ |
| 29 | + Alias: "test-server", |
| 30 | + Host: "example.com", |
| 31 | + User: "testuser", |
| 32 | + Port: 22, |
| 33 | + PingStatus: "up", |
| 34 | + PingLatency: 100 * time.Millisecond, |
| 35 | + LastSeen: time.Now(), |
| 36 | + PinnedAt: time.Now(), |
| 37 | + SSHCount: 5, |
| 38 | + } |
| 39 | + |
| 40 | + server2 := domain.Server{ |
| 41 | + Alias: "test-server", |
| 42 | + Host: "example.com", |
| 43 | + User: "testuser", |
| 44 | + Port: 22, |
| 45 | + PingStatus: "down", // Different transient field |
| 46 | + PingLatency: 200 * time.Millisecond, // Different transient field |
| 47 | + LastSeen: time.Now().Add(1 * time.Hour), // Different metadata field |
| 48 | + PinnedAt: time.Now().Add(2 * time.Hour), // Different metadata field |
| 49 | + SSHCount: 10, // Different metadata field |
| 50 | + } |
| 51 | + |
| 52 | + // Should not detect differences since only transient/metadata fields differ |
| 53 | + if sf.serversDiffer(server1, server2) { |
| 54 | + t.Error("serversDiffer should ignore transient and metadata fields") |
| 55 | + } |
| 56 | + |
| 57 | + // Now change a real config field |
| 58 | + server2.Port = 2222 |
| 59 | + |
| 60 | + // Should detect the difference now |
| 61 | + if !sf.serversDiffer(server1, server2) { |
| 62 | + t.Error("serversDiffer should detect differences in non-transient fields") |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestServersDifferDetectsRealChanges(t *testing.T) { |
| 67 | + sf := &ServerForm{} |
| 68 | + |
| 69 | + server1 := domain.Server{ |
| 70 | + Alias: "test-server", |
| 71 | + Host: "example.com", |
| 72 | + User: "testuser", |
| 73 | + Port: 22, |
| 74 | + } |
| 75 | + |
| 76 | + testCases := []struct { |
| 77 | + name string |
| 78 | + modify func(*domain.Server) |
| 79 | + expect bool |
| 80 | + }{ |
| 81 | + { |
| 82 | + name: "No changes", |
| 83 | + modify: func(s *domain.Server) {}, |
| 84 | + expect: false, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "Changed Host", |
| 88 | + modify: func(s *domain.Server) { s.Host = "different.com" }, |
| 89 | + expect: true, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "Changed User", |
| 93 | + modify: func(s *domain.Server) { s.User = "otheruser" }, |
| 94 | + expect: true, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "Changed Port", |
| 98 | + modify: func(s *domain.Server) { s.Port = 2222 }, |
| 99 | + expect: true, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "Added IdentityFile", |
| 103 | + modify: func(s *domain.Server) { s.IdentityFiles = []string{"~/.ssh/id_rsa"} }, |
| 104 | + expect: true, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "Changed ProxyJump", |
| 108 | + modify: func(s *domain.Server) { s.ProxyJump = "jumphost" }, |
| 109 | + expect: true, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "Changed only PingStatus (transient)", |
| 113 | + modify: func(s *domain.Server) { s.PingStatus = "checking" }, |
| 114 | + expect: false, |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "Changed only LastSeen (metadata)", |
| 118 | + modify: func(s *domain.Server) { s.LastSeen = time.Now().Add(1 * time.Hour) }, |
| 119 | + expect: false, |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + for _, tc := range testCases { |
| 124 | + t.Run(tc.name, func(t *testing.T) { |
| 125 | + server2 := server1 // Copy |
| 126 | + tc.modify(&server2) |
| 127 | + result := sf.serversDiffer(server1, server2) |
| 128 | + if result != tc.expect { |
| 129 | + t.Errorf("Expected %v but got %v for test case %s", tc.expect, result, tc.name) |
| 130 | + } |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
0 commit comments