|
| 1 | +// Copyright (c) 2021-2026 Rustam Gilyazov and Contributors. |
| 2 | +// |
| 3 | +// This program is free software: you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU Affero General Public License as published by |
| 5 | +// the Free Software Foundation, either version 3 of the License, or |
| 6 | +// (at your option) any later version. |
| 7 | +// |
| 8 | +// This program is distributed in the hope that it will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +// GNU Affero General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU Affero General Public License |
| 14 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +package wspcfg |
| 17 | + |
| 18 | +import ( |
| 19 | + "flag" |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/rusq/slackdump/v4/auth" |
| 24 | +) |
| 25 | + |
| 26 | +func TestSetWspFlags_bundledBrowser(t *testing.T) { |
| 27 | + tests := []struct { |
| 28 | + name string |
| 29 | + args []string |
| 30 | + want bool |
| 31 | + }{ |
| 32 | + {name: "default is false", args: []string{}, want: false}, |
| 33 | + {name: "-bundled-browser sets true", args: []string{"-bundled-browser"}, want: true}, |
| 34 | + {name: "-bundled-browser=true", args: []string{"-bundled-browser=true"}, want: true}, |
| 35 | + {name: "-bundled-browser=false", args: []string{"-bundled-browser=false"}, want: false}, |
| 36 | + } |
| 37 | + for _, tt := range tests { |
| 38 | + t.Run(tt.name, func(t *testing.T) { |
| 39 | + BundledBrowser = false |
| 40 | + fs := flag.NewFlagSet("test", flag.ContinueOnError) |
| 41 | + SetWspFlags(fs) |
| 42 | + if err := fs.Parse(tt.args); err != nil { |
| 43 | + t.Fatalf("Parse() error = %v", err) |
| 44 | + } |
| 45 | + if BundledBrowser != tt.want { |
| 46 | + t.Errorf("BundledBrowser = %v, want %v", BundledBrowser, tt.want) |
| 47 | + } |
| 48 | + }) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestRodAuthOptions_WiresBundledBrowserPolarity(t *testing.T) { |
| 53 | + origHeadless := rodWithHeadlessTimeout |
| 54 | + origUA := rodWithUserAgent |
| 55 | + origBundled := rodWithBundledBrowser |
| 56 | + origAuto := rodWithInteractiveAuto |
| 57 | + defer func() { |
| 58 | + rodWithHeadlessTimeout = origHeadless |
| 59 | + rodWithUserAgent = origUA |
| 60 | + rodWithBundledBrowser = origBundled |
| 61 | + rodWithInteractiveAuto = origAuto |
| 62 | + }() |
| 63 | + |
| 64 | + var ( |
| 65 | + gotHeadless time.Duration |
| 66 | + gotUA string |
| 67 | + gotBundled bool |
| 68 | + gotAuto bool |
| 69 | + ) |
| 70 | + rodWithHeadlessTimeout = func(d time.Duration) auth.Option { |
| 71 | + gotHeadless = d |
| 72 | + return nil |
| 73 | + } |
| 74 | + rodWithUserAgent = func(s string) auth.Option { |
| 75 | + gotUA = s |
| 76 | + return nil |
| 77 | + } |
| 78 | + rodWithBundledBrowser = func(b bool) auth.Option { |
| 79 | + gotBundled = b |
| 80 | + return nil |
| 81 | + } |
| 82 | + rodWithInteractiveAuto = func(b bool) auth.Option { |
| 83 | + gotAuto = b |
| 84 | + return nil |
| 85 | + } |
| 86 | + |
| 87 | + HeadlessTimeout = 77 * time.Second |
| 88 | + RODUserAgent = "ua-test" |
| 89 | + BundledBrowser = true |
| 90 | + opts := RodAuthOptions() |
| 91 | + |
| 92 | + if len(opts) != 4 { |
| 93 | + t.Fatalf("RodAuthOptions() len = %d, want 4", len(opts)) |
| 94 | + } |
| 95 | + if gotHeadless != 77*time.Second { |
| 96 | + t.Errorf("headless timeout = %v, want %v", gotHeadless, 77*time.Second) |
| 97 | + } |
| 98 | + if gotUA != "ua-test" { |
| 99 | + t.Errorf("user agent = %q, want %q", gotUA, "ua-test") |
| 100 | + } |
| 101 | + if !gotBundled { |
| 102 | + t.Errorf("bundled browser = %v, want true", gotBundled) |
| 103 | + } |
| 104 | + if gotAuto { |
| 105 | + t.Errorf("interactive auto = %v, want false", gotAuto) |
| 106 | + } |
| 107 | +} |
0 commit comments