| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/config" |
| 10 | ) |
| 11 | |
| 12 | // TestUpsertEnvFile proves a new key is appended, an existing key is replaced in |
| 13 | // place, comments/other lines survive, and the process env is updated. |
| 14 | func TestUpsertEnvFile(t *testing.T) { |
| 15 | path := filepath.Join(t.TempDir(), "credentials") |
| 16 | if err := os.WriteFile(path, []byte("# comment\nFOO=old\nBAR=keep\n"), 0o644); err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | |
| 20 | if err := upsertEnvFile(path, "FOO", "new"); err != nil { |
| 21 | t.Fatalf("replace: %v", err) |
| 22 | } |
| 23 | if err := upsertEnvFile(path, "BAZ", "added"); err != nil { |
| 24 | t.Fatalf("append: %v", err) |
| 25 | } |
| 26 | |
| 27 | b, _ := os.ReadFile(path) |
| 28 | got := string(b) |
| 29 | for _, want := range []string{"# comment", "FOO=new", "BAR=keep", "BAZ=added"} { |
| 30 | if !strings.Contains(got, want) { |
| 31 | t.Errorf("missing %q in:\n%s", want, got) |
| 32 | } |
| 33 | } |
| 34 | if strings.Contains(got, "FOO=old") { |
| 35 | t.Errorf("old value should be replaced:\n%s", got) |
| 36 | } |
| 37 | if os.Getenv("FOO") != "new" || os.Getenv("BAZ") != "added" { |
| 38 | t.Errorf("process env not updated: FOO=%q BAZ=%q", os.Getenv("FOO"), os.Getenv("BAZ")) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func TestRemoveEnvFileDeletesKeyAndUnsetsProcessEnv(t *testing.T) { |
| 43 | path := filepath.Join(t.TempDir(), "credentials") |
| 44 | if err := os.WriteFile(path, []byte("# comment\nFOO=old\nexport BAR=remove\nBAZ=keep\n"), 0o644); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | t.Setenv("BAR", "remove") |
| 48 | |
| 49 | if err := removeEnvFile(path, "BAR"); err != nil { |
| 50 | t.Fatalf("remove: %v", err) |
| 51 | } |
| 52 | |
| 53 | b, _ := os.ReadFile(path) |
| 54 | got := string(b) |
| 55 | for _, want := range []string{"# comment", "FOO=old", "BAZ=keep"} { |
| 56 | if !strings.Contains(got, want) { |
| 57 | t.Errorf("missing %q in:\n%s", want, got) |
| 58 | } |
| 59 | } |
| 60 | if strings.Contains(got, "BAR=") { |
| 61 | t.Errorf("removed key should be absent:\n%s", got) |
| 62 | } |
| 63 | if _, ok := os.LookupEnv("BAR"); ok { |
| 64 | t.Errorf("process env BAR should be unset") |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestLegacyHomeEnvProviderKeyIsNotPromoted(t *testing.T) { |
| 69 | home := isolateDesktopUserDirs(t) |
| 70 | homeEnv := filepath.Join(home, ".env") |
| 71 | if err := os.WriteFile(homeEnv, []byte("DEEPSEEK_API_KEY=sk-test\nNPM_TOKEN=secret\n"), 0o600); err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | |
| 75 | if _, err := config.LoadForRoot(t.TempDir()); err != nil { |
| 76 | t.Fatalf("LoadForRoot: %v", err) |
| 77 | } |
| 78 | if data, err := os.ReadFile(config.UserCredentialsPath()); err == nil && strings.Contains(string(data), "DEEPSEEK_API_KEY") { |
| 79 | t.Errorf("legacy ~/.env provider key must not be imported:\n%s", data) |
| 80 | } |
| 81 | rest, _ := os.ReadFile(homeEnv) |
| 82 | if !strings.Contains(string(rest), "DEEPSEEK_API_KEY=sk-test") || !strings.Contains(string(rest), "NPM_TOKEN=secret") { |
| 83 | t.Errorf("legacy ~/.env should be left untouched:\n%s", rest) |
| 84 | } |
| 85 | } |
| 86 |