CI (SIAX Cloud) / sonar (pull_request) Skipped
masterplan-lock / check (pull_request) Successful in 7s
masterplan-lock / check (push) Successful in 9s
CI (SIAX Cloud) / integration (pull_request) Successful in 35s
CI (SIAX Cloud) / quality (pull_request) Failing after 39s
Security Scan (SIAX Cloud) / security (push) Successful in 1m12s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 1m14s
CI (SIAX Cloud) / security (pull_request) Successful in 1m18s
CI (SIAX Cloud) / integration (push) Skipped
CI (SIAX Cloud) / security (push) Successful in 21s
CI (SIAX Cloud) / sonar (push) Skipped
CI (SIAX Cloud) / quality (push) Failing after 37s
301 lines
14 KiB
TypeScript
301 lines
14 KiB
TypeScript
import { describe, it, expect, vi, afterEach } from "vitest";
|
|
import { OdooBillsAdapter, matchPartnerViaFabric, type OdooBillPayload } from "@/src/adapters/odoo-bills-adapter";
|
|
import { FabricOneLakeAdapter } from "@/src/adapters/fabric-onelake-adapter";
|
|
import type { WritebackAdapterContext } from "@/src/adapters/writeback-adapter";
|
|
|
|
function jsonResponse(body: unknown, opts: { status?: number } = {}) {
|
|
return {
|
|
ok: (opts.status ?? 200) < 400,
|
|
status: opts.status ?? 200,
|
|
json: async () => body,
|
|
text: async () => JSON.stringify(body),
|
|
} as unknown as Response;
|
|
}
|
|
|
|
const CTX: WritebackAdapterContext = {
|
|
tenant_id: "t_protosell",
|
|
project_id: "proj_ap",
|
|
actor_id: "actor1",
|
|
correlation_id: "corr-1",
|
|
};
|
|
|
|
function basePayload(overrides: Partial<OdooBillPayload> = {}): OdooBillPayload {
|
|
return {
|
|
vendor_name: "Acme AB",
|
|
vendor_org_nr: "556123-4567",
|
|
invoice_number: "INV-2026-001",
|
|
amount_gross: 1234.5,
|
|
amount_excl_vat: 987.6,
|
|
vat: 246.9,
|
|
currency: "SEK",
|
|
invoice_date: "2026-07-01",
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe("OdooBillsAdapter — isConfigured", () => {
|
|
it("is not configured without a baseUrl", () => {
|
|
const adapter = new OdooBillsAdapter({ baseUrl: "", apiKey: "key" });
|
|
expect(adapter.isConfigured()).toBe(false);
|
|
});
|
|
|
|
it("is not configured with a baseUrl but no apiKey and no allowUnauthenticated flag", () => {
|
|
const adapter = new OdooBillsAdapter({ baseUrl: "https://odoo.example.com" });
|
|
expect(adapter.isConfigured()).toBe(false);
|
|
});
|
|
|
|
it("is configured with baseUrl + apiKey", () => {
|
|
const adapter = new OdooBillsAdapter({ baseUrl: "https://odoo.example.com", apiKey: "key" });
|
|
expect(adapter.isConfigured()).toBe(true);
|
|
});
|
|
|
|
it("is configured with baseUrl + explicit allowUnauthenticated, without an apiKey", () => {
|
|
const adapter = new OdooBillsAdapter({ baseUrl: "https://odoo.example.com", allowUnauthenticated: true });
|
|
expect(adapter.isConfigured()).toBe(true);
|
|
});
|
|
|
|
it("write() returns ok:false when not configured, without attempting a network call", async () => {
|
|
const fetchMock = vi.fn();
|
|
const adapter = new OdooBillsAdapter({ baseUrl: "", fetchImpl: fetchMock as unknown as typeof fetch });
|
|
const result = await adapter.write({ recordType: "vendor_bill", payload: basePayload() as unknown as Record<string, unknown>, idempotencyKey: "k1" }, CTX);
|
|
expect(result.ok).toBe(false);
|
|
expect(result.error).toBe("odoo_not_configured");
|
|
expect(fetchMock).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("OdooBillsAdapter — guards ported verbatim from invoice-bridge.ts", () => {
|
|
afterEach(() => vi.restoreAllMocks());
|
|
|
|
it("refuses to post a payment reminder (is_reminder: true) — mirrors 'Påminnelse postas inte till Odoo'", async () => {
|
|
const fetchMock = vi.fn();
|
|
const adapter = new OdooBillsAdapter({ baseUrl: "https://odoo.example.com", apiKey: "key", fetchImpl: fetchMock as unknown as typeof fetch });
|
|
const result = await adapter.write(
|
|
{ recordType: "vendor_bill", payload: basePayload({ is_reminder: true }) as unknown as Record<string, unknown>, idempotencyKey: "k1" },
|
|
CTX,
|
|
);
|
|
expect(result).toEqual({ ok: false, error: "is_reminder" });
|
|
expect(fetchMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("refuses to post a detected duplicate (duplicate_flag: true) — mirrors 'Dubblett postas inte till Odoo'", async () => {
|
|
const fetchMock = vi.fn();
|
|
const adapter = new OdooBillsAdapter({ baseUrl: "https://odoo.example.com", apiKey: "key", fetchImpl: fetchMock as unknown as typeof fetch });
|
|
const result = await adapter.write(
|
|
{ recordType: "vendor_bill", payload: basePayload({ duplicate_flag: true }) as unknown as Record<string, unknown>, idempotencyKey: "k1" },
|
|
CTX,
|
|
);
|
|
expect(result).toEqual({ ok: false, error: "duplicate" });
|
|
expect(fetchMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("treats a payload with existing_external_id as already posted — idempotent no-op, no network call, mirrors 'Redan bokförd i Odoo... hoppar över'", async () => {
|
|
const fetchMock = vi.fn();
|
|
const adapter = new OdooBillsAdapter({ baseUrl: "https://odoo.example.com", apiKey: "key", fetchImpl: fetchMock as unknown as typeof fetch });
|
|
const result = await adapter.write(
|
|
{ recordType: "vendor_bill", payload: basePayload({ existing_external_id: "999" }) as unknown as Record<string, unknown>, idempotencyKey: "k1" },
|
|
CTX,
|
|
);
|
|
expect(result).toEqual({ ok: true, externalId: "999" });
|
|
expect(fetchMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("rejects a payload missing required fields without attempting a network call", async () => {
|
|
const fetchMock = vi.fn();
|
|
const adapter = new OdooBillsAdapter({ baseUrl: "https://odoo.example.com", apiKey: "key", fetchImpl: fetchMock as unknown as typeof fetch });
|
|
const result = await adapter.write(
|
|
{ recordType: "vendor_bill", payload: { vendor_name: "Acme AB" }, idempotencyKey: "k1" },
|
|
CTX,
|
|
);
|
|
expect(result.ok).toBe(false);
|
|
expect(result.error).toBe("invalid_payload_missing_required_fields");
|
|
expect(fetchMock).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("OdooBillsAdapter — real POST shape (ported from invoice-bridge.ts's pushInvoiceToOdoo)", () => {
|
|
afterEach(() => vi.restoreAllMocks());
|
|
|
|
it("POSTs account.move with itemized lines when payload.lines is present", async () => {
|
|
let capturedUrl = "";
|
|
let capturedInit: RequestInit | undefined;
|
|
const fetchMock = vi.fn(async (url: string, init?: RequestInit) => {
|
|
capturedUrl = url;
|
|
capturedInit = init;
|
|
return jsonResponse({ ok: true, odoo_id: 4242 });
|
|
});
|
|
const adapter = new OdooBillsAdapter({ baseUrl: "https://odoo.example.com", apiKey: "sekret", fetchImpl: fetchMock as unknown as typeof fetch });
|
|
|
|
const payload = basePayload({
|
|
partner_id: 77,
|
|
lines: [{ description: "Konsulttjänst", quantity: 2, unit_price: 500 }],
|
|
});
|
|
const result = await adapter.write({ recordType: "vendor_bill", payload: payload as unknown as Record<string, unknown>, idempotencyKey: "k1" }, CTX);
|
|
|
|
expect(result).toEqual({ ok: true, externalId: "4242" });
|
|
expect(capturedUrl).toBe("https://odoo.example.com/api/odoo/invoices/write");
|
|
expect((capturedInit?.headers as Record<string, string>).Authorization).toBe("Bearer sekret");
|
|
const body = JSON.parse(capturedInit!.body as string);
|
|
expect(body.model).toBe("account.move");
|
|
expect(body.invoice.partner_id).toBe(77);
|
|
expect(body.invoice.ref).toBe("INV-2026-001");
|
|
expect(body.invoice.invoice_line_ids).toEqual([{ name: "Konsulttjänst", quantity: 2, price_unit: 500 }]);
|
|
});
|
|
|
|
it("synthesizes a single summary line from description/vendor_name when payload.lines is absent — ported verbatim from processInboxToOdoo's line-synthesis fallback", async () => {
|
|
let capturedInit: RequestInit | undefined;
|
|
const fetchMock = vi.fn(async (_url: string, init?: RequestInit) => {
|
|
capturedInit = init;
|
|
return jsonResponse({ ok: true, odoo_id: 100 });
|
|
});
|
|
const adapter = new OdooBillsAdapter({ baseUrl: "https://odoo.example.com", apiKey: "key", fetchImpl: fetchMock as unknown as typeof fetch });
|
|
|
|
const payload = basePayload({ description: "Städtjänster juni", amount_excl_vat: 800 });
|
|
await adapter.write({ recordType: "vendor_bill", payload: payload as unknown as Record<string, unknown>, idempotencyKey: "k1" }, CTX);
|
|
|
|
const body = JSON.parse(capturedInit!.body as string);
|
|
expect(body.invoice.invoice_line_ids).toEqual([{ name: "Städtjänster juni", quantity: 1, price_unit: 800 }]);
|
|
});
|
|
|
|
it("falls back to vendor_name, then a synthesized 'Faktura <number>' string, when description is absent", async () => {
|
|
let capturedInit: RequestInit | undefined;
|
|
const fetchMock = vi.fn(async (_url: string, init?: RequestInit) => {
|
|
capturedInit = init;
|
|
return jsonResponse({ ok: true, odoo_id: 101 });
|
|
});
|
|
const adapter = new OdooBillsAdapter({ baseUrl: "https://odoo.example.com", apiKey: "key", fetchImpl: fetchMock as unknown as typeof fetch });
|
|
|
|
const payload = basePayload({ vendor_name: "", invoice_number: "INV-777", amount_excl_vat: 50 });
|
|
await adapter.write({ recordType: "vendor_bill", payload: payload as unknown as Record<string, unknown>, idempotencyKey: "k1" }, CTX);
|
|
|
|
const body = JSON.parse(capturedInit!.body as string);
|
|
expect(body.invoice.invoice_line_ids[0].name).toBe("Faktura INV-777");
|
|
});
|
|
|
|
it("returns ok:false with an odoo_http_<status> error on a non-2xx response", async () => {
|
|
const fetchMock = vi.fn(async () => jsonResponse({}, { status: 500 }));
|
|
const adapter = new OdooBillsAdapter({ baseUrl: "https://odoo.example.com", apiKey: "key", fetchImpl: fetchMock as unknown as typeof fetch });
|
|
const result = await adapter.write({ recordType: "vendor_bill", payload: basePayload() as unknown as Record<string, unknown>, idempotencyKey: "k1" }, CTX);
|
|
expect(result).toEqual({ ok: false, error: "odoo_http_500" });
|
|
});
|
|
|
|
it("returns ok:false when Odoo responds 200 but ok:false/no odoo_id", async () => {
|
|
const fetchMock = vi.fn(async () => jsonResponse({ ok: false, error: "odoo_validation_error" }));
|
|
const adapter = new OdooBillsAdapter({ baseUrl: "https://odoo.example.com", apiKey: "key", fetchImpl: fetchMock as unknown as typeof fetch });
|
|
const result = await adapter.write({ recordType: "vendor_bill", payload: basePayload() as unknown as Record<string, unknown>, idempotencyKey: "k1" }, CTX);
|
|
expect(result).toEqual({ ok: false, error: "odoo_validation_error" });
|
|
});
|
|
|
|
it("returns ok:false on a network error, without throwing", async () => {
|
|
const fetchMock = vi.fn(async () => {
|
|
throw new Error("ECONNREFUSED");
|
|
});
|
|
const adapter = new OdooBillsAdapter({ baseUrl: "https://odoo.example.com", apiKey: "key", fetchImpl: fetchMock as unknown as typeof fetch });
|
|
const result = await adapter.write({ recordType: "vendor_bill", payload: basePayload() as unknown as Record<string, unknown>, idempotencyKey: "k1" }, CTX);
|
|
expect(result.ok).toBe(false);
|
|
expect(result.error).toContain("odoo_network_error");
|
|
});
|
|
});
|
|
|
|
describe("matchPartnerViaFabric — ported from invoice-bridge.ts's matchInvoiceToOdooPartner", () => {
|
|
it("matches by digits-only comparison so '556123-4567' resolves the same as a plain digit string", async () => {
|
|
const queryMock = vi.fn(async () => ({
|
|
rows: [{ id: 42, name: "Acme AB", vat: "SE5561234567 01" }],
|
|
rowCount: 1,
|
|
durationMs: 1,
|
|
}));
|
|
const fabric = { query: queryMock } as unknown as FabricOneLakeAdapter;
|
|
|
|
const result = await matchPartnerViaFabric(fabric, "556123-4567");
|
|
expect(result).toEqual({ partner_id: 42, partner_name: "Acme AB" });
|
|
expect(queryMock).toHaveBeenCalledWith(expect.stringContaining("odoo_export__res_partner"), { digits: "5561234567" });
|
|
});
|
|
|
|
it("returns {} (no match) when the org-nr has no digits at all", async () => {
|
|
const queryMock = vi.fn();
|
|
const fabric = { query: queryMock } as unknown as FabricOneLakeAdapter;
|
|
const result = await matchPartnerViaFabric(fabric, "N/A");
|
|
expect(result).toEqual({});
|
|
expect(queryMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("degrades to {} (no match) rather than throwing when the warehouse query fails — mirrors invoice-bridge.ts's own catch-all", async () => {
|
|
const queryMock = vi.fn(async () => {
|
|
throw new Error("warehouse unreachable");
|
|
});
|
|
const fabric = { query: queryMock } as unknown as FabricOneLakeAdapter;
|
|
const result = await matchPartnerViaFabric(fabric, "556123-4567");
|
|
expect(result).toEqual({});
|
|
});
|
|
|
|
it("returns {} when no partner row matches", async () => {
|
|
const queryMock = vi.fn(async () => ({ rows: [], rowCount: 0, durationMs: 1 }));
|
|
const fabric = { query: queryMock } as unknown as FabricOneLakeAdapter;
|
|
const result = await matchPartnerViaFabric(fabric, "556123-4567");
|
|
expect(result).toEqual({});
|
|
});
|
|
});
|
|
|
|
describe("OdooBillsAdapter — partner resolution wired into write()", () => {
|
|
afterEach(() => vi.restoreAllMocks());
|
|
|
|
it("uses payload.partner_id directly when already set, without calling matchPartner", async () => {
|
|
const matchPartner = vi.fn();
|
|
const fetchMock = vi.fn(async () => jsonResponse({ ok: true, odoo_id: 1 }));
|
|
const adapter = new OdooBillsAdapter({
|
|
baseUrl: "https://odoo.example.com",
|
|
apiKey: "key",
|
|
matchPartner,
|
|
fetchImpl: fetchMock as unknown as typeof fetch,
|
|
});
|
|
await adapter.write(
|
|
{ recordType: "vendor_bill", payload: basePayload({ partner_id: 55 }) as unknown as Record<string, unknown>, idempotencyKey: "k1" },
|
|
CTX,
|
|
);
|
|
expect(matchPartner).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("calls the injected matchPartner when partner_id is absent but vendor_org_nr is present, and forwards the resolved partner_id to Odoo", async () => {
|
|
const matchPartner = vi.fn(async () => ({ partner_id: 99, partner_name: "Acme AB" }));
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- AUD0-ingest-body är fri nyttolast
|
|
let capturedBody: any;
|
|
const fetchMock = vi.fn(async (_url: string, init?: RequestInit) => {
|
|
capturedBody = JSON.parse(init!.body as string);
|
|
return jsonResponse({ ok: true, odoo_id: 1 });
|
|
});
|
|
const adapter = new OdooBillsAdapter({
|
|
baseUrl: "https://odoo.example.com",
|
|
apiKey: "key",
|
|
matchPartner,
|
|
fetchImpl: fetchMock as unknown as typeof fetch,
|
|
});
|
|
await adapter.write(
|
|
{ recordType: "vendor_bill", payload: basePayload() as unknown as Record<string, unknown>, idempotencyKey: "k1" },
|
|
CTX,
|
|
);
|
|
expect(matchPartner).toHaveBeenCalledWith("556123-4567", CTX);
|
|
expect(capturedBody.invoice.partner_id).toBe(99);
|
|
});
|
|
|
|
it("posts without a partner_id when no match is found — 'skickar utan partner-koppling'", async () => {
|
|
const matchPartner = vi.fn(async () => ({}));
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- AUD0-ingest-body är fri nyttolast
|
|
let capturedBody: any;
|
|
const fetchMock = vi.fn(async (_url: string, init?: RequestInit) => {
|
|
capturedBody = JSON.parse(init!.body as string);
|
|
return jsonResponse({ ok: true, odoo_id: 1 });
|
|
});
|
|
const adapter = new OdooBillsAdapter({
|
|
baseUrl: "https://odoo.example.com",
|
|
apiKey: "key",
|
|
matchPartner,
|
|
fetchImpl: fetchMock as unknown as typeof fetch,
|
|
});
|
|
await adapter.write(
|
|
{ recordType: "vendor_bill", payload: basePayload() as unknown as Record<string, unknown>, idempotencyKey: "k1" },
|
|
CTX,
|
|
);
|
|
expect(capturedBody.invoice.partner_id).toBeUndefined();
|
|
});
|
|
});
|