many changes
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
/**
|
||||
* Device ID service tests
|
||||
*
|
||||
* Tests the service layer that integrates with the Rust backend.
|
||||
* The Rust backend handles UUID generation and database storage.
|
||||
*
|
||||
* TRACES: UR-009 | DR-011
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
@@ -18,7 +23,7 @@ describe("Device ID Service", () => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should retrieve existing device ID from backend", async () => {
|
||||
it("should retrieve device ID from backend", async () => {
|
||||
const mockDeviceId = "550e8400-e29b-41d4-a716-446655440000";
|
||||
(invoke as any).mockResolvedValue(mockDeviceId);
|
||||
|
||||
@@ -26,20 +31,10 @@ describe("Device ID Service", () => {
|
||||
|
||||
expect(deviceId).toBe(mockDeviceId);
|
||||
expect(invoke).toHaveBeenCalledWith("device_get_id");
|
||||
expect(invoke).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("should generate and store new device ID if none exists", async () => {
|
||||
(invoke as any).mockResolvedValueOnce(null); // No existing ID
|
||||
(invoke as any).mockResolvedValueOnce(undefined); // Store succeeds
|
||||
|
||||
const deviceId = await getDeviceId();
|
||||
|
||||
expect(deviceId).toMatch(/^[a-f0-9\-]{36}$/); // UUID format
|
||||
expect(invoke).toHaveBeenCalledWith("device_get_id");
|
||||
expect(invoke).toHaveBeenCalledWith("device_set_id", { deviceId: expect.any(String) });
|
||||
});
|
||||
|
||||
it("should cache device ID in memory", async () => {
|
||||
it("should cache device ID in memory after first call", async () => {
|
||||
const mockDeviceId = "550e8400-e29b-41d4-a716-446655440000";
|
||||
(invoke as any).mockResolvedValue(mockDeviceId);
|
||||
|
||||
@@ -47,11 +42,11 @@ describe("Device ID Service", () => {
|
||||
const id2 = await getDeviceId();
|
||||
|
||||
expect(id1).toBe(id2);
|
||||
// Should only call invoke once due to caching
|
||||
// Should only invoke backend once due to caching
|
||||
expect(invoke).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("should return cached device ID synchronously", async () => {
|
||||
it("should return cached device ID synchronously after initialization", async () => {
|
||||
const mockDeviceId = "550e8400-e29b-41d4-a716-446655440000";
|
||||
(invoke as any).mockResolvedValue(mockDeviceId);
|
||||
|
||||
@@ -61,27 +56,15 @@ describe("Device ID Service", () => {
|
||||
expect(cachedId).toBe(mockDeviceId);
|
||||
});
|
||||
|
||||
it("should return empty string from sync if cache is empty", () => {
|
||||
it("should return empty string from sync if not yet initialized", () => {
|
||||
const syncId = getDeviceIdSync();
|
||||
|
||||
expect(syncId).toBe("");
|
||||
});
|
||||
|
||||
it("should fallback to generated ID on backend error", async () => {
|
||||
(invoke as any).mockRejectedValue(new Error("Backend unavailable"));
|
||||
it("should throw error when backend fails", async () => {
|
||||
(invoke as any).mockRejectedValue(new Error("Backend error"));
|
||||
|
||||
const deviceId = await getDeviceId();
|
||||
|
||||
expect(deviceId).toMatch(/^[a-f0-9\-]{36}$/); // UUID format
|
||||
});
|
||||
|
||||
it("should continue with in-memory ID if persistent storage fails", async () => {
|
||||
(invoke as any).mockResolvedValueOnce(null); // No existing ID
|
||||
(invoke as any).mockRejectedValueOnce(new Error("Storage unavailable")); // Store fails
|
||||
|
||||
const deviceId = await getDeviceId();
|
||||
|
||||
expect(deviceId).toMatch(/^[a-f0-9\-]{36}$/); // UUID format
|
||||
await expect(getDeviceId()).rejects.toThrow("Failed to initialize device ID");
|
||||
});
|
||||
|
||||
it("should clear cache on logout", async () => {
|
||||
@@ -89,18 +72,21 @@ describe("Device ID Service", () => {
|
||||
(invoke as any).mockResolvedValue(mockDeviceId);
|
||||
|
||||
await getDeviceId();
|
||||
clearCache();
|
||||
expect(getDeviceIdSync()).toBe(mockDeviceId);
|
||||
|
||||
clearCache();
|
||||
expect(getDeviceIdSync()).toBe("");
|
||||
});
|
||||
|
||||
it("should generate unique device IDs", async () => {
|
||||
(invoke as any).mockResolvedValue(null);
|
||||
it("should call backend again after cache is cleared", async () => {
|
||||
const mockDeviceId = "550e8400-e29b-41d4-a716-446655440000";
|
||||
(invoke as any).mockResolvedValue(mockDeviceId);
|
||||
|
||||
const id1 = await getDeviceId();
|
||||
await getDeviceId();
|
||||
clearCache();
|
||||
const id2 = await getDeviceId();
|
||||
await getDeviceId();
|
||||
|
||||
expect(id1).not.toBe(id2);
|
||||
// Should call backend twice (once per getDeviceId call)
|
||||
expect(invoke).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user