main_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package main
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. )
  7. func clearEnv(t *testing.T) {
  8. t.Helper()
  9. t.Chdir(t.TempDir())
  10. for _, key := range []string{
  11. "WIKIJS_URL", "WIKIJS_API_KEY", "WIKIJS_ENABLE_EDIT", "WIKIJS_TIMEOUT",
  12. "MCP_TRANSPORT", "MCP_ADDR", "MCP_PATH", "MCP_STATELESS",
  13. "MCP_AUTH_TOKEN", "LOG_LEVEL", "ENV_FILE",
  14. } {
  15. t.Setenv(key, "")
  16. }
  17. }
  18. func TestVersionFlagNeedsNoCredentials(t *testing.T) {
  19. clearEnv(t)
  20. if err := run([]string{"--version"}); err != nil {
  21. t.Fatalf("run(--version) error = %v", err)
  22. }
  23. }
  24. func TestHelpNeedsNoCredentials(t *testing.T) {
  25. clearEnv(t)
  26. // Silence the usage text; flag.Parse writes it to the flag set output.
  27. stdout, stderr := os.Stdout, os.Stderr
  28. devNull, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
  29. if err != nil {
  30. t.Fatalf("open %s: %v", os.DevNull, err)
  31. }
  32. defer devNull.Close()
  33. os.Stdout, os.Stderr = devNull, devNull
  34. defer func() {
  35. os.Stdout, os.Stderr = stdout, stderr
  36. }()
  37. if err := run([]string{"--help"}); err != nil {
  38. t.Fatalf("run(--help) error = %v", err)
  39. }
  40. }
  41. func TestRunRequiresCredentials(t *testing.T) {
  42. clearEnv(t)
  43. if err := run(nil); err == nil {
  44. t.Fatal("run() error = nil, want missing credentials error")
  45. }
  46. }
  47. func TestFlagsOverrideTransport(t *testing.T) {
  48. clearEnv(t)
  49. t.Setenv("WIKIJS_URL", "https://wiki.example.com")
  50. t.Setenv("WIKIJS_API_KEY", "secret")
  51. t.Setenv("MCP_TRANSPORT", "http")
  52. if err := run([]string{"--transport", "sse"}); err == nil {
  53. t.Fatal("run() error = nil, want invalid transport error")
  54. }
  55. }
  56. func TestEnvFileFromArgs(t *testing.T) {
  57. tests := []struct {
  58. name string
  59. args []string
  60. wantPath string
  61. wantFound bool
  62. wantErr bool
  63. }{
  64. {name: "none"},
  65. {name: "separate", args: []string{"--env-file", "custom.env"}, wantPath: "custom.env", wantFound: true},
  66. {name: "equals", args: []string{"--env-file=custom.env"}, wantPath: "custom.env", wantFound: true},
  67. {name: "single dash", args: []string{"-env-file", "custom.env"}, wantPath: "custom.env", wantFound: true},
  68. {name: "single dash equals", args: []string{"-env-file=custom.env"}, wantPath: "custom.env", wantFound: true},
  69. {name: "last wins", args: []string{"--env-file=a.env", "--env-file", "b.env"}, wantPath: "b.env", wantFound: true},
  70. {name: "missing value", args: []string{"--env-file"}, wantFound: true, wantErr: true},
  71. {name: "empty value", args: []string{"--env-file="}, wantFound: true, wantErr: true},
  72. {name: "after terminator", args: []string{"--", "--env-file", "custom.env"}},
  73. {name: "other flags", args: []string{"--transport", "http"}},
  74. }
  75. for _, tt := range tests {
  76. t.Run(tt.name, func(t *testing.T) {
  77. path, found, err := envFileFromArgs(tt.args)
  78. if gotErr := err != nil; gotErr != tt.wantErr {
  79. t.Fatalf("envFileFromArgs() error = %v, wantErr %v", err, tt.wantErr)
  80. }
  81. if path != tt.wantPath {
  82. t.Errorf("path = %q, want %q", path, tt.wantPath)
  83. }
  84. if found != tt.wantFound {
  85. t.Errorf("found = %v, want %v", found, tt.wantFound)
  86. }
  87. })
  88. }
  89. }
  90. func TestEnvFileFlagOverridesEnvFile(t *testing.T) {
  91. clearEnv(t)
  92. t.Setenv("ENV_FILE", filepath.Join(t.TempDir(), "does-not-exist.env"))
  93. good := filepath.Join(t.TempDir(), "good.env")
  94. if err := os.WriteFile(good, []byte("# empty\n"), 0o600); err != nil {
  95. t.Fatalf("write env file: %v", err)
  96. }
  97. if err := run([]string{"--env-file", good, "--version"}); err != nil {
  98. t.Fatalf("run(--env-file ... --version) error = %v", err)
  99. }
  100. }