fix(offline): harden sync catch + add queue/sync test coverage + atomic transactions
This commit is contained in:
@@ -2,7 +2,15 @@
|
||||
// src/lib/offline/queue.test.ts
|
||||
import { describe, it, expect, beforeEach } from "vitest";
|
||||
import "fake-indexeddb/auto";
|
||||
import { enqueueAction, dequeueAction, getQueuedActions, markSynced, markConflict } from "./queue";
|
||||
import {
|
||||
enqueueAction,
|
||||
dequeueAction,
|
||||
getQueuedActions,
|
||||
markSynced,
|
||||
markConflict,
|
||||
markInFlight,
|
||||
markFailed,
|
||||
} from "./queue";
|
||||
|
||||
beforeEach(async () => {
|
||||
// Clear the queue between tests
|
||||
@@ -61,4 +69,45 @@ describe("offline queue", () => {
|
||||
expect(all[0].status).toBe("conflict");
|
||||
expect(all[0].error).toBe("stale updated_at");
|
||||
});
|
||||
|
||||
it("markInFlight transitions the action to in-flight and records an attempt", async () => {
|
||||
const a = await enqueueAction("markOrderReady", { orderId: "abc" });
|
||||
const before = await getQueuedActions();
|
||||
expect(before[0].attempts).toBe(0);
|
||||
expect(before[0].status).toBe("pending");
|
||||
expect(before[0].lastAttemptAt).toBeNull();
|
||||
|
||||
const beforeMs = Date.now();
|
||||
await markInFlight(a.id);
|
||||
const afterMs = Date.now();
|
||||
|
||||
const all = await getQueuedActions();
|
||||
expect(all).toHaveLength(1);
|
||||
expect(all[0].status).toBe("in-flight");
|
||||
expect(all[0].attempts).toBe(1);
|
||||
expect(typeof all[0].lastAttemptAt).toBe("number");
|
||||
expect(all[0].lastAttemptAt).not.toBeNull();
|
||||
expect(all[0].lastAttemptAt!).toBeGreaterThanOrEqual(beforeMs);
|
||||
expect(all[0].lastAttemptAt!).toBeLessThanOrEqual(afterMs);
|
||||
});
|
||||
|
||||
it("markFailed transitions the action to failed and increments attempts", async () => {
|
||||
const a = await enqueueAction("markOrderReady", { orderId: "abc" });
|
||||
// Pretend it has already been tried once
|
||||
await markInFlight(a.id);
|
||||
|
||||
const beforeMs = Date.now();
|
||||
await markFailed(a.id, "server returned 500");
|
||||
const afterMs = Date.now();
|
||||
|
||||
const all = await getQueuedActions();
|
||||
expect(all).toHaveLength(1);
|
||||
expect(all[0].status).toBe("failed");
|
||||
expect(all[0].attempts).toBe(2); // markInFlight set it to 1, markFailed adds 1
|
||||
expect(all[0].error).toBe("server returned 500");
|
||||
expect(typeof all[0].lastAttemptAt).toBe("number");
|
||||
expect(all[0].lastAttemptAt).not.toBeNull();
|
||||
expect(all[0].lastAttemptAt!).toBeGreaterThanOrEqual(beforeMs);
|
||||
expect(all[0].lastAttemptAt!).toBeLessThanOrEqual(afterMs);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user