import { __resetApiConfigStoreForTests, configureApi, getBaseUrl, getDefaultBaseUrl, getStoragePrefix, resetBaseUrl, setBaseUrl, } from "../api-config"; // `Platform.OS` defaults to native under the babel-preset-expo jest preset, // so getSessionToken / storeSessionToken / secureStore behave as native here. // The test focuses on the URL + prefix state machine which is platform-agnostic. beforeEach(() => { __resetApiConfigStoreForTests(); }); describe("configureApi", () => { it("captures the apiBaseUrl and storagePrefix on first call", () => { configureApi({ apiBaseUrl: "https://api.example.com", storagePrefix: "demo" }); expect(getBaseUrl()).toBe("https://api.example.com"); expect(getDefaultBaseUrl()).toBe("https://api.example.com"); expect(getStoragePrefix()).toBe("demo"); }); it("locks the storage prefix after the first call", () => { configureApi({ apiBaseUrl: "https://a.example.com", storagePrefix: "first" }); configureApi({ apiBaseUrl: "https://b.example.com", storagePrefix: "second" }); expect(getStoragePrefix()).toBe("first"); }); it("throws when getBaseUrl is called before configure", () => { expect(() => getBaseUrl()).toThrow(/configureApi/); expect(() => getDefaultBaseUrl()).toThrow(/configureApi/); }); }); describe("setBaseUrl / resetBaseUrl", () => { beforeEach(() => { configureApi({ apiBaseUrl: "https://default.example.com", storagePrefix: "demo" }); }); it("accepts https URLs", async () => { await setBaseUrl("https://override.example.com"); expect(getBaseUrl()).toBe("https://override.example.com"); }); it("accepts http://localhost", async () => { await setBaseUrl("http://localhost:8080/api"); expect(getBaseUrl()).toBe("http://localhost:8080/api"); }); it("rejects non-HTTPS, non-localhost URLs", async () => { await expect(setBaseUrl("http://example.com")).rejects.toThrow(/HTTPS/); }); it("rejects malformed URLs", async () => { await expect(setBaseUrl("not-a-url")).rejects.toThrow(/Invalid/); }); it("reset restores the configured default", async () => { await setBaseUrl("https://override.example.com"); await resetBaseUrl(); expect(getBaseUrl()).toBe("https://default.example.com"); }); });