Spaces:
Sleeping

File size: 2,824 Bytes
7f81110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { describe, it, expect, vi } from "vitest";
import { RunSyncJobUseCase } from "../src/modules/shopify/application/useCases/sync/RunSyncJobUseCase";
import { TenantContext } from "../src/libs/shared/domain/valueObjects/TenantContext";
import { ShopifySyncJob } from "../src/modules/shopify/domain/entities/ShopifySyncJob";

describe("RunSyncJobUseCase", () => {
    it("should update fullCursor in place and call saveCursor with all cursors", async () => {
        const mockStagingRepo: any = {
            getCursor: vi.fn().mockResolvedValue({ productsCursor: "p_old", customersCursor: null, ordersCursor: null }),
            saveCursor: vi.fn().mockResolvedValue(undefined),
            findByExternalId: vi.fn().mockResolvedValue(null),
            upsert: vi.fn(),
            countPendingByTenant: vi.fn().mockResolvedValue({ embedPending: 0, enrichPending: 0 })
        };
        const mockShopifyClient: any = {
            fetchProducts: vi.fn().mockResolvedValue({ items: [], nextCursor: "p_new", hasNextPage: false }),
            fetchCustomers: vi.fn().mockResolvedValue({ items: [], nextCursor: "c_new", hasNextPage: false }),
            fetchOrders: vi.fn().mockResolvedValue({ items: [], nextCursor: "o_new", hasNextPage: false }),
        };
        const mockConnectorRepo: any = {
            getCredentials: vi.fn().mockResolvedValue({}),
            updateLastSyncAt: vi.fn()
        };
        const mockSyncJobRepo: any = {
            markCompleted: vi.fn()
        };
        const mockChangeDetection: any = {
            computeHash: vi.fn(),
            hasChanged: vi.fn(),
            needsImageEnrichment: vi.fn()
        };
        const mockMarkStale: any = {
            execute: vi.fn()
        };

        const useCase = new RunSyncJobUseCase(
            {} as any,
            mockShopifyClient,
            mockStagingRepo,
            mockConnectorRepo,
            mockSyncJobRepo,
            mockChangeDetection,
            mockMarkStale
        );

        const job: any = { id: "job1", tenantId: "t1", progress: {} };
        await useCase.execute(job);

        // Verify saveCursor calls
        const saveCursorCalls = mockStagingRepo.saveCursor.mock.calls;

        // After products sync
        expect(saveCursorCalls[0][2]).toEqual({
            productsCursor: "p_new",
            customersCursor: null,
            ordersCursor: null
        });

        // After customers sync
        expect(saveCursorCalls[1][2]).toEqual({
            productsCursor: "p_new",
            customersCursor: "c_new",
            ordersCursor: null
        });

        // After orders sync
        expect(saveCursorCalls[2][2]).toEqual({
            productsCursor: "p_new",
            customersCursor: "c_new",
            ordersCursor: "o_new"
        });
    });
});