all the haul
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
import { beforeEach, describe, expect, mock, test } from "bun:test";
|
||||
import { buildMockCompany } from "../../setup";
|
||||
|
||||
const getMock: any = mock(() => Promise.resolve({ data: [] as any[] }));
|
||||
|
||||
mock.module("../../../src/constants", () => ({
|
||||
prisma: new Proxy(
|
||||
{},
|
||||
{
|
||||
get() {
|
||||
return mock(() => Promise.resolve([]));
|
||||
},
|
||||
},
|
||||
),
|
||||
connectWiseApi: {
|
||||
get: getMock,
|
||||
post: mock(() => Promise.resolve({ data: {} })),
|
||||
put: mock(() => Promise.resolve({ data: {} })),
|
||||
patch: mock(() => Promise.resolve({ data: {} })),
|
||||
delete: mock(() => Promise.resolve({ data: {} })),
|
||||
},
|
||||
}));
|
||||
|
||||
function buildConfig(id: number) {
|
||||
return {
|
||||
id,
|
||||
name: `Config ${id}`,
|
||||
activeFlag: true,
|
||||
serialNumber: `SN-${id}`,
|
||||
type: { id: 9, name: "Firewall" },
|
||||
notes: "test notes",
|
||||
status: { id: 2, name: "Active" },
|
||||
questions: [
|
||||
{
|
||||
questionId: 11,
|
||||
question: "Hostname",
|
||||
answer: `host-${id}`,
|
||||
fieldType: "Text",
|
||||
},
|
||||
],
|
||||
_info: { lastUpdated: "2026-03-31T00:00:00Z" },
|
||||
};
|
||||
}
|
||||
|
||||
describe("CompanyController.fetchConfigurations", () => {
|
||||
beforeEach(() => {
|
||||
getMock.mockReset();
|
||||
});
|
||||
|
||||
test("fetches and transforms company configurations from ConnectWise", async () => {
|
||||
const { CompanyController } = await import(
|
||||
"../../../src/controllers/CompanyController"
|
||||
);
|
||||
|
||||
getMock.mockImplementation(() => Promise.resolve({ data: [buildConfig(1)] }));
|
||||
|
||||
const controller = new CompanyController(buildMockCompany({ id: 123 }) as any);
|
||||
const result = await controller.fetchConfigurations();
|
||||
|
||||
expect(getMock).toHaveBeenCalledTimes(1);
|
||||
const requestUrl = String((getMock.mock.calls[0] as any[])?.[0] ?? "");
|
||||
expect(requestUrl).toContain(
|
||||
"/company/configurations?page=1&pageSize=1000&conditions=company%2Fid%3D123",
|
||||
);
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
const first = result[0] as any;
|
||||
expect(first.id).toBe(1);
|
||||
expect(first.name).toBe("Config 1");
|
||||
expect(first.active).toBe(true);
|
||||
expect(first.serialNumber).toBe("SN-1");
|
||||
expect(first.type?.id).toBe(9);
|
||||
expect(first.status?.name).toBe("Active");
|
||||
expect(first.questions?.[0]?.question).toBe("Hostname");
|
||||
});
|
||||
|
||||
test("paginates when page is full", async () => {
|
||||
const { CompanyController } = await import(
|
||||
"../../../src/controllers/CompanyController"
|
||||
);
|
||||
|
||||
const firstPage = Array.from({ length: 1000 }, (_, i) => buildConfig(i + 1));
|
||||
const secondPage = [buildConfig(1001)];
|
||||
|
||||
getMock
|
||||
.mockImplementationOnce(() => Promise.resolve({ data: firstPage }))
|
||||
.mockImplementationOnce(() => Promise.resolve({ data: secondPage }));
|
||||
|
||||
const controller = new CompanyController(buildMockCompany({ id: 456 }) as any);
|
||||
const result = await controller.fetchConfigurations();
|
||||
|
||||
expect(getMock).toHaveBeenCalledTimes(2);
|
||||
const firstUrl = String((getMock.mock.calls[0] as any[])?.[0] ?? "");
|
||||
const secondUrl = String((getMock.mock.calls[1] as any[])?.[0] ?? "");
|
||||
expect(firstUrl).toContain("page=1");
|
||||
expect(secondUrl).toContain("page=2");
|
||||
expect(result).toHaveLength(1001);
|
||||
});
|
||||
|
||||
test("throws GenericError when CW request fails", async () => {
|
||||
const { CompanyController } = await import(
|
||||
"../../../src/controllers/CompanyController"
|
||||
);
|
||||
|
||||
const err: any = new Error("Unauthorized");
|
||||
err.isAxiosError = true;
|
||||
err.response = {
|
||||
status: 401,
|
||||
data: { message: "Unauthorized" },
|
||||
statusText: "Unauthorized",
|
||||
};
|
||||
|
||||
getMock.mockImplementation(() => Promise.reject(err));
|
||||
|
||||
const controller = new CompanyController(buildMockCompany({ id: 321 }) as any);
|
||||
|
||||
try {
|
||||
await controller.fetchConfigurations();
|
||||
expect(true).toBe(false);
|
||||
} catch (error: any) {
|
||||
expect(error.name).toBe("ConnectWiseFetchFailed");
|
||||
expect(error.status).toBe(401);
|
||||
expect(error.message).toBe(
|
||||
"Failed to fetch company configurations from ConnectWise",
|
||||
);
|
||||
expect(error.cause).toBe("Unauthorized");
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -72,83 +72,26 @@ describe("CwMemberController", () => {
|
||||
|
||||
test("returns trimmed name when lastName is empty", () => {
|
||||
const ctrl = new CwMemberController(
|
||||
buildMockCwMember({ lastName: "" }) as any,
|
||||
buildMockCwMember({ lastName: "" }) as any
|
||||
);
|
||||
expect(ctrl.fullName).toBe("John");
|
||||
});
|
||||
|
||||
test("returns trimmed name when firstName is empty", () => {
|
||||
const ctrl = new CwMemberController(
|
||||
buildMockCwMember({ firstName: "" }) as any,
|
||||
buildMockCwMember({ firstName: "" }) as any
|
||||
);
|
||||
expect(ctrl.fullName).toBe("Doe");
|
||||
});
|
||||
|
||||
test("falls back to identifier when both names are empty", () => {
|
||||
const ctrl = new CwMemberController(
|
||||
buildMockCwMember({ firstName: "", lastName: "" }) as any,
|
||||
buildMockCwMember({ firstName: "", lastName: "" }) as any
|
||||
);
|
||||
expect(ctrl.fullName).toBe("jdoe");
|
||||
});
|
||||
});
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// mapCwToDb (static)
|
||||
// -----------------------------------------------------------------
|
||||
describe("mapCwToDb", () => {
|
||||
test("maps CW member fields to DB schema", () => {
|
||||
const cwItem = {
|
||||
identifier: "jdoe",
|
||||
firstName: "John",
|
||||
lastName: "Doe",
|
||||
officeEmail: "jdoe@example.com",
|
||||
inactiveFlag: false,
|
||||
_info: { lastUpdated: "2026-02-01T12:00:00Z" },
|
||||
};
|
||||
|
||||
const result = CwMemberController.mapCwToDb(cwItem as any);
|
||||
expect(result.identifier).toBe("jdoe");
|
||||
expect(result.firstName).toBe("John");
|
||||
expect(result.lastName).toBe("Doe");
|
||||
expect(result.officeEmail).toBe("jdoe@example.com");
|
||||
expect(result.inactiveFlag).toBe(false);
|
||||
expect(result.cwLastUpdated).toEqual(new Date("2026-02-01T12:00:00Z"));
|
||||
});
|
||||
|
||||
test("handles null/missing fields with defaults", () => {
|
||||
const cwItem = {
|
||||
identifier: "empty",
|
||||
firstName: null,
|
||||
lastName: null,
|
||||
officeEmail: null,
|
||||
inactiveFlag: null,
|
||||
_info: null,
|
||||
};
|
||||
|
||||
const result = CwMemberController.mapCwToDb(cwItem as any);
|
||||
expect(result.firstName).toBe("");
|
||||
expect(result.lastName).toBe("");
|
||||
expect(result.officeEmail).toBeNull();
|
||||
expect(result.inactiveFlag).toBe(false);
|
||||
expect(result.cwLastUpdated).toBeInstanceOf(Date);
|
||||
});
|
||||
|
||||
test("handles undefined _info.lastUpdated", () => {
|
||||
const cwItem = {
|
||||
identifier: "test",
|
||||
firstName: "A",
|
||||
lastName: "B",
|
||||
officeEmail: null,
|
||||
inactiveFlag: false,
|
||||
_info: {},
|
||||
};
|
||||
|
||||
const result = CwMemberController.mapCwToDb(cwItem as any);
|
||||
// Without lastUpdated, falls through to new Date()
|
||||
expect(result.cwLastUpdated).toBeInstanceOf(Date);
|
||||
});
|
||||
});
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// toJson
|
||||
// -----------------------------------------------------------------
|
||||
@@ -173,7 +116,7 @@ describe("CwMemberController", () => {
|
||||
|
||||
test("includes fullName in JSON", () => {
|
||||
const ctrl = new CwMemberController(
|
||||
buildMockCwMember({ firstName: "", lastName: "" }) as any,
|
||||
buildMockCwMember({ firstName: "", lastName: "" }) as any
|
||||
);
|
||||
expect(ctrl.toJson().fullName).toBe("jdoe");
|
||||
});
|
||||
|
||||
@@ -164,10 +164,9 @@ describe("OpportunityController", () => {
|
||||
|
||||
test("maps type, stage, status references", () => {
|
||||
const result = OpportunityController.mapCwToDb(cwOpportunity);
|
||||
expect(result.typeName).toBe("New Business");
|
||||
expect(result.typeCwId).toBe(1);
|
||||
expect(result.stageName).toBe("Proposal");
|
||||
expect(result.statusName).toBe("Active");
|
||||
expect(result.typeId).toBe(1);
|
||||
expect(result.stageId).toBe(2);
|
||||
expect(result.statusId).toBe(3);
|
||||
});
|
||||
|
||||
test("maps null references to null", () => {
|
||||
|
||||
Reference in New Issue
Block a user