File size: 4,433 Bytes
a5784e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
 * AuthManager Component Tests
 */

import { describe, it, expect, vi, beforeEach } from "vitest";

// Define types locally to avoid module resolution issues
interface AuthFileInfo {
  name: string;
  path: string;
  size_bytes: number;
  is_active: boolean;
}

interface AuthFilesResponse {
  saved_files: AuthFileInfo[];
  active_file: string | null;
}

describe("AuthManager Logic", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  describe("Data Fetching", () => {
    it("returns loading state structure", () => {
      const result = { data: undefined, isLoading: true, error: null };
      expect(result.isLoading).toBe(true);
    });

    it("returns auth files when loaded", () => {
      const mockData: AuthFilesResponse = {
        saved_files: [
          {
            name: "auth1.json",
            path: "/data/auth1.json",
            size_bytes: 1024,
            is_active: true,
          },
          {
            name: "auth2.json",
            path: "/data/auth2.json",
            size_bytes: 2048,
            is_active: false,
          },
        ],
        active_file: "auth1.json",
      };

      const result = { data: mockData, isLoading: false, error: null };
      expect(result.data.saved_files).toHaveLength(2);
      expect(result.data.active_file).toBe("auth1.json");
    });

    it("handles empty file list", () => {
      const mockData: AuthFilesResponse = {
        saved_files: [],
        active_file: null,
      };

      const result = { data: mockData, isLoading: false, error: null };
      expect(result.data.saved_files).toHaveLength(0);
      expect(result.data.active_file).toBeNull();
    });
  });

  describe("Mutations", () => {
    it("activateAuthFile mutation accepts filename", () => {
      const filename = "auth2.json";
      expect(typeof filename).toBe("string");
    });

    it("deactivateAuth mutation structure", () => {
      const mutation = { isPending: false };
      expect(mutation.isPending).toBe(false);
    });

    it("shows pending state during activation", () => {
      const mutation = { isPending: true };
      expect(mutation.isPending).toBe(true);
    });
  });

  describe("File List Processing", () => {
    it("extracts saved_files array correctly", () => {
      const data: AuthFilesResponse = {
        saved_files: [
          {
            name: "a.json",
            path: "/a.json",
            size_bytes: 100,
            is_active: false,
          },
        ],
        active_file: null,
      };

      const savedFiles = data.saved_files;
      expect(savedFiles).toHaveLength(1);
    });

    it("handles undefined data gracefully", () => {
      const data: AuthFilesResponse | undefined = undefined;
      const savedFiles = data?.saved_files || [];
      expect(savedFiles).toHaveLength(0);
    });

    it("identifies active file correctly", () => {
      const files = [
        { name: "auth1.json", is_active: true },
        { name: "auth2.json", is_active: false },
      ];

      const activeFile = files.find((f) => f.is_active);
      expect(activeFile?.name).toBe("auth1.json");
    });

    it("formats file size in KB", () => {
      const sizeBytes = 2048;
      const sizeKB = (sizeBytes / 1024).toFixed(1);
      expect(sizeKB).toBe("2.0");
    });

    it("handles very small files", () => {
      const sizeBytes = 100;
      const sizeKB = (sizeBytes / 1024).toFixed(1);
      expect(sizeKB).toBe("0.1");
    });
  });

  describe("Active State Logic", () => {
    it("determines if deactivate button should show", () => {
      const activeFile: string | null = "auth1.json";
      const shouldShowDeactivate = activeFile !== null;
      expect(shouldShowDeactivate).toBe(true);
    });

    it("hides deactivate button when no active file", () => {
      const activeFile: string | null = null;
      const shouldShowDeactivate = activeFile !== null;
      expect(shouldShowDeactivate).toBe(false);
    });

    it("determines if activate button should show for inactive files", () => {
      const file = { name: "auth2.json", is_active: false };
      const shouldShowActivate = !file.is_active;
      expect(shouldShowActivate).toBe(true);
    });

    it("hides activate button for active file", () => {
      const file = { name: "auth1.json", is_active: true };
      const shouldShowActivate = !file.is_active;
      expect(shouldShowActivate).toBe(false);
    });
  });
});