Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ name: CI
on:
push:
branches: [ main ]
tags:
- 'v*'
pull_request:
branches: [ main ]

permissions:
contents: read

jobs:
quality-gates:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -49,8 +54,10 @@ jobs:

release:
needs: quality-gates
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') && github.repository == 'IndexGrid/offline-first-sync-queue'
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- name: Create Release Draft
Expand Down
73 changes: 73 additions & 0 deletions apps/web/src/lib/sync/__tests__/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,77 @@ describe('runSyncOnce', () => {
const recoveredItem = await db.get('syncQueue', '1');
expect(recoveredItem?.status).toBe('SYNCED');
});

it('should transition to DEAD_LETTER after max retries', async () => {
const db = await getDB();
const now = Date.now();

const tx = db.transaction('syncQueue', 'readwrite');
await tx.objectStore('syncQueue').add({
id: '1',
externalId: 'ext-dead',
entityType: 'order',
status: 'PENDING',
nextAttemptAt: now - 1000,
createdAt: now - 2000,
payload: { data: 'fail' },
url: 'v1/pos/sync',
method: 'POST',
op: 'UPSERT',
retryCount: 10 // Max retries reached
});
await tx.done;

const mockFetch = vi.fn().mockResolvedValue({
ok: true,
json: () => Promise.resolve({
results: [{ externalId: 'ext-dead', status: 'error', reason: 'permanent_fail' }]
})
});

const result = await runSyncOnce({
batchSize: 10,
fetchImpl: mockFetch as unknown as typeof fetch,
maxRetries: 10
});

expect(result.dead).toBe(1);

const item = await db.get('syncQueue', '1');
expect(item?.status).toBe('DEAD_LETTER');
expect(item?.lastError).toBe('permanent_fail');
});

it('should handle network errors with exponential backoff', async () => {
const db = await getDB();
const now = Date.now();

const tx = db.transaction('syncQueue', 'readwrite');
await tx.objectStore('syncQueue').add({
id: '1',
externalId: 'ext-net',
entityType: 'order',
status: 'PENDING',
nextAttemptAt: now - 1000,
createdAt: now - 2000,
payload: { data: 'net' },
url: 'v1/pos/sync',
method: 'POST',
op: 'UPSERT',
retryCount: 0
});
await tx.done;

const mockFetch = vi.fn().mockRejectedValue(new Error('Network timeout'));

await runSyncOnce({
batchSize: 10,
fetchImpl: mockFetch as unknown as typeof fetch
});

const item = await db.get('syncQueue', '1');
expect(item?.status).toBe('RETRYABLE_ERROR');
expect(item?.retryCount).toBe(1);
expect(item?.nextAttemptAt).toBeGreaterThan(now);
});
});
Loading