test(auth): add comprehensive tests for GLM auth persistence fix

Add 26 new unit tests covering:
- isFirstTimeInstall() legacy config detection (8 tests)
- loadSettingsFromFile() path expansion (5 tests)
- ProfileDetector detectProfileType() (4 tests)
- expandPath() cross-platform handling (10 tests)

Test coverage for issue #195:
- Legacy config.json/profiles.json detection
- Tilde expansion to home directory
- Environment variable expansion (${VAR}, $VAR, %VAR%)
- Mixed path separator normalization
- Corrupted config graceful handling

Files:
- tests/unit/commands/setup-command.test.ts
- tests/unit/auth/profile-detector.test.ts
- tests/unit/utils/expand-path.test.ts
This commit is contained in:
kaitranntt committed 2025-12-24 18:33:52 -05:00
1 parent adb6222bc6
commit 92a79aa78b
7 files changed
+369 -5

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
import { expect, test, describe, beforeEach, afterEach } from "bun:test";
import * as path from "path";
import * as os from "os";
import { expandPath } from "../../../src/utils/helpers";
describe("expandPath", () => {
const originalEnv = { ...process.env };
const HOME = os.homedir();
beforeEach(() => {
process.env.TEST_HOME = "/custom/home";
process.env.TEST_VAR = "foo";
});
afterEach(() => {
process.env = { ...originalEnv };
});
test("1. Tilde expansion: ~/path -> /home/user/path", () => {
expect(expandPath("~/test/file.txt")).toBe(path.join(HOME, "test/file.txt"));
});
test("2. Windows tilde with backslash: ~\\path", () => {
// expandPath handles both ~/ and ~\ regardless of platform
expect(expandPath("~\\test\\file.txt")).toBe(path.join(HOME, "test/file.txt"));
});
test("3. Environment variable expansion: ${VAR}/path", () => {
expect(expandPath("${TEST_HOME}/file.txt")).toBe(path.normalize("/custom/home/file.txt"));
});
test("4. Dollar sign env vars: $VAR/path", () => {
expect(expandPath("$TEST_HOME/file.txt")).toBe(path.normalize("/custom/home/file.txt"));
});
test("5. Windows %VAR% expansion: %VAR%\\path (simulated)", () => {
// We can't easily mock process.platform if it's not win32,
// but the function check process.platform === 'win32'
if (process.platform === 'win32') {
expect(expandPath("%TEST_HOME%\\file.txt")).toBe(path.normalize("/custom/home/file.txt"));
} else {
// Should remain unchanged on non-windows (but separators normalized)
expect(expandPath("%TEST_HOME%\\file.txt")).toBe(path.normalize("%TEST_HOME%/file.txt"));
}
});
test("6. Mixed path separators normalization", () => {
const result = expandPath("path/to\\some/file");
expect(result).toBe(path.normalize("path/to/some/file"));
});
test("7. Nested env vars: ${HOME}/${VAR}/path", () => {
expect(expandPath("${TEST_HOME}/${TEST_VAR}/file.txt")).toBe(path.normalize("/custom/home/foo/file.txt"));
});
test("8. Empty/null path handling", () => {
expect(expandPath("")).toBe(".");
});
test("9. Already absolute paths stay unchanged", () => {
const absPath = "/absolute/path";
expect(expandPath(absPath)).toBe(path.normalize(absPath));
});
test("10. Undefined env vars -> empty string", () => {
expect(expandPath("${UNDEFINED_VAR}/file.txt")).toBe(path.normalize("/file.txt"));
expect(expandPath("$UNDEFINED_VAR/file.txt")).toBe(path.normalize("/file.txt"));
});
});