
import { expect } from '@playwright/test';
import { test } from '../support/fixtures/offline.fixture';

test.describe('Sync Action Replay', () => {
    test('should process pending actions when network restores', async ({ offlineControl, page }) => {
        // GIVEN: Offline queue has actions
        await page.goto('/'); // Ensure page is loaded to initialize DB
        await page.waitForFunction(() => (window as any).db !== undefined, { timeout: 10000 });

        await offlineControl.goOffline(page.context());

        // Seed queue via evaluate
        await page.evaluate(async () => {
            // @ts-ignore
            await window.db.syncQueue.add({
                action: 'saveDraft',
                payload: { draftData: { title: 'Pending Sync', content: '...' } },
                status: 'pending',
                createdAt: Date.now(),
                retries: 0
            });
        });

        // WHEN: Network restores
        await offlineControl.goOnline(page.context());

        // Trigger sync - The SyncManager listens to 'online' event.
        // Note: The 'online' event listeners might execute async.

        // Wait for processing
        // We can poll the DB
        await expect.poll(async () => {
            return await page.evaluate(async () => {
                // @ts-ignore
                return await window.db.syncQueue.where('status').equals('pending').count();
            });
        }, { timeout: 5000 }).toBe(0);
    });
});
