File size: 4,921 Bytes
c09f67c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { beforeEach, describe, expect, mock, test } from "bun:test";
import {
  createInboxListResponse,
  createMinimalInboxResponse,
  createValidInboxResponse,
} from "../factories/inbox";
import { createTestContext } from "../helpers/test-context";

// Create local mocks for inbox tests
const mockGetInbox = mock(() => createInboxListResponse());
const mockGetInboxById = mock(
  () => null as ReturnType<typeof createValidInboxResponse> | null,
);
const mockUpdateInbox = mock(() => ({}));
const mockDeleteInbox = mock(() => ({}));
const mockDeleteInboxMany = mock(() => [] as Array<{ id: string }>);
const mockGetInboxByStatus = mock(() => [] as any[]);

// Mock the module
mock.module("@midday/db/queries", () => ({
  getInbox: mockGetInbox,
  getInboxById: mockGetInboxById,
  createInbox: mock(() => ({})),
  updateInbox: mockUpdateInbox,
  deleteInbox: mockDeleteInbox,
  deleteInboxMany: mockDeleteInboxMany,
  getInboxByStatus: mockGetInboxByStatus,
  getInboxSearch: mock(() => []),
  getInboxBlocklist: mock(() => []),
  createInboxBlocklist: mock(() => ({})),
  deleteInboxBlocklist: mock(() => ({})),
  checkInboxAttachments: mock(() => []),
  matchTransaction: mock(() => ({})),
  unmatchTransaction: mock(() => ({})),
  confirmSuggestedMatch: mock(() => ({})),
  declineSuggestedMatch: mock(() => ({})),
  deleteInboxEmbedding: mock(() => ({})),
}));

// Import after mocking
const { createCallerFactory } = await import("../../trpc/init");
const { inboxRouter } = await import("../../trpc/routers/inbox");

// Create a test caller
const createCaller = createCallerFactory(inboxRouter);

describe("tRPC: inbox.get", () => {
  beforeEach(() => {
    mockGetInbox.mockReset();
    mockGetInbox.mockImplementation(() => createInboxListResponse());
  });

  test("returns inbox list", async () => {
    mockGetInbox.mockImplementation(() =>
      createInboxListResponse([createValidInboxResponse()]),
    );

    const caller = createCaller(createTestContext());
    const result = await caller.get({});

    expect(result.data).toHaveLength(1);
    expect(result.meta.hasNextPage).toBe(false);
  });

  test("handles minimal inbox data", async () => {
    mockGetInbox.mockImplementation(() =>
      createInboxListResponse([createMinimalInboxResponse()]),
    );

    const caller = createCaller(createTestContext());
    const result = await caller.get({});

    expect(result.data[0]!.transactionId).toBeNull();
  });

  test("handles empty list", async () => {
    mockGetInbox.mockImplementation(() => createInboxListResponse([]));

    const caller = createCaller(createTestContext());
    const result = await caller.get({});

    expect(result.data).toHaveLength(0);
  });
});

describe("tRPC: inbox.getById", () => {
  beforeEach(() => {
    mockGetInboxById.mockReset();
  });

  test("returns single inbox item", async () => {
    mockGetInboxById.mockImplementation(() => createValidInboxResponse());

    const caller = createCaller(createTestContext());
    const result = await caller.getById({
      id: "a1b2c3d4-5e6f-4a7b-8c9d-0e1f2a3b4c5d",
    });

    expect(result?.id).toBe("a1b2c3d4-5e6f-4a7b-8c9d-0e1f2a3b4c5d");
  });

  test("returns null for non-existent item", async () => {
    mockGetInboxById.mockImplementation(() => null);

    const caller = createCaller(createTestContext());
    const result = await caller.getById({
      id: "b2c3d4e5-6f7a-4b8c-9d0e-1f2a3b4c5d6e",
    });

    expect(result).toBeNull();
  });
});

describe("tRPC: inbox.update", () => {
  beforeEach(() => {
    mockUpdateInbox.mockReset();
    mockUpdateInbox.mockImplementation(() => createValidInboxResponse());
  });

  test("updates inbox item successfully", async () => {
    const caller = createCaller(createTestContext());
    const result = await caller.update({
      id: "a1b2c3d4-5e6f-4a7b-8c9d-0e1f2a3b4c5d",
      status: "done", // Use valid enum value
    });

    expect(result).toBeDefined();
  });
});

describe("tRPC: inbox.delete", () => {
  beforeEach(() => {
    mockDeleteInbox.mockReset();
    mockDeleteInbox.mockImplementation(() => ({
      id: "a1b2c3d4-5e6f-4a7b-8c9d-0e1f2a3b4c5d",
    }));
  });

  test("deletes inbox item successfully", async () => {
    const caller = createCaller(createTestContext());
    // inbox.delete returns void, so we just check it doesn't throw
    await caller.delete({ id: "a1b2c3d4-5e6f-4a7b-8c9d-0e1f2a3b4c5d" });
    expect(mockDeleteInbox).toHaveBeenCalled();
  });
});

describe("tRPC: inbox.getByStatus", () => {
  beforeEach(() => {
    mockGetInboxByStatus.mockReset();
    mockGetInboxByStatus.mockImplementation(() => [
      { id: "item-1", displayName: "Test", status: "pending" },
    ]);
  });

  test("returns inbox items by status", async () => {
    const caller = createCaller(createTestContext());
    const result = await caller.getByStatus({ status: "pending" });

    expect(result).toHaveLength(1);
  });
});