Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,226 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
createImportDryRunPreview,
|
||||
createRedactedParserDiagnostic,
|
||||
detectImportFormatFromContentType,
|
||||
detectImportFormatFromExtension,
|
||||
isSupportedImportFormat,
|
||||
redactDiagnosticMessage,
|
||||
summarizeRowResults,
|
||||
type ImportParserOutput,
|
||||
type ImportRowResult,
|
||||
} from "./index.js";
|
||||
|
||||
describe("imports parser contracts", () => {
|
||||
it("guards supported formats explicitly", () => {
|
||||
expect(isSupportedImportFormat("csv")).toBe(true);
|
||||
expect(isSupportedImportFormat("json")).toBe(true);
|
||||
expect(isSupportedImportFormat("xlsx")).toBe(true);
|
||||
expect(isSupportedImportFormat("xml")).toBe(false);
|
||||
expect(isSupportedImportFormat("CSV")).toBe(false);
|
||||
expect(isSupportedImportFormat("")).toBe(false);
|
||||
});
|
||||
|
||||
it("detects csv/json/xlsx formats from extensions and content types", () => {
|
||||
expect(detectImportFormatFromExtension("members.CSV")).toBe("csv");
|
||||
expect(detectImportFormatFromExtension("members.json")).toBe("json");
|
||||
expect(detectImportFormatFromExtension("exports/Members.XLSX")).toBe("xlsx");
|
||||
expect(detectImportFormatFromExtension("xlsx")).toBe("xlsx");
|
||||
expect(detectImportFormatFromContentType("text/csv; charset=utf-8")).toBe("csv");
|
||||
expect(detectImportFormatFromContentType("application/json; charset=utf-8")).toBe("json");
|
||||
expect(detectImportFormatFromContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")).toBe("xlsx");
|
||||
});
|
||||
|
||||
it("rejects unknown and ambiguous formats", () => {
|
||||
expect(detectImportFormatFromExtension("payload.unsupported")).toBeUndefined();
|
||||
expect(detectImportFormatFromExtension("members.csv.zip")).toBeUndefined();
|
||||
expect(detectImportFormatFromExtension(undefined)).toBeUndefined();
|
||||
expect(detectImportFormatFromContentType("application/octet-stream")).toBeUndefined();
|
||||
expect(detectImportFormatFromContentType("text/plain")).toBeUndefined();
|
||||
expect(detectImportFormatFromContentType(undefined)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("summarizes accepted and rejected row results", () => {
|
||||
const rows: ImportRowResult[] = [
|
||||
{ ok: true, rowNumber: 1, normalized: { email: "a@example.com" } },
|
||||
{ ok: true, rowNumber: 2, normalized: { email: "b@example.com", tags: ["member", "paid"] } },
|
||||
{
|
||||
ok: false,
|
||||
rowNumber: 3,
|
||||
errors: [{ severity: "error", code: "missing_email", message: "Email is required." }],
|
||||
},
|
||||
];
|
||||
|
||||
expect(summarizeRowResults(rows)).toEqual({ totalRows: 3, acceptedRows: 2, rejectedRows: 1 });
|
||||
expect(summarizeRowResults([])).toEqual({ totalRows: 0, acceptedRows: 0, rejectedRows: 0 });
|
||||
});
|
||||
|
||||
it("redacts secrets and body-like diagnostics without leaking raw values", () => {
|
||||
const secretRedacted = redactDiagnosticMessage("token=abc123 password:super-secret authorization=BearerToken");
|
||||
const bodyRedacted = redactDiagnosticMessage("body={raw:'payload'} rawRows=[secret]");
|
||||
|
||||
expect(secretRedacted).toContain("token= [REDACTED]");
|
||||
expect(secretRedacted).toContain("password: [REDACTED]");
|
||||
expect(secretRedacted).toContain("authorization= [REDACTED]");
|
||||
expect(bodyRedacted).toContain("body= [REDACTED]");
|
||||
expect(bodyRedacted).toContain("rawRows= [REDACTED]");
|
||||
expect(secretRedacted).not.toContain("abc123");
|
||||
expect(secretRedacted).not.toContain("super-secret");
|
||||
expect(bodyRedacted).not.toContain("{raw:'payload'}");
|
||||
expect(bodyRedacted).not.toContain("[secret]");
|
||||
expect(redactDiagnosticMessage("x".repeat(10), 5)).toBe("xxxx…");
|
||||
expect(createRedactedParserDiagnostic("parse_failed", "apiKey=secret-value", { rowNumber: 3 })).toEqual({
|
||||
severity: "error",
|
||||
code: "parse_failed",
|
||||
message: "apiKey= [REDACTED]",
|
||||
rowNumber: 3,
|
||||
});
|
||||
});
|
||||
|
||||
it("creates accepted mapped dry-run previews", () => {
|
||||
const output = createImportDryRunPreview({
|
||||
format: "csv",
|
||||
fields: [{ targetField: "email", required: true, label: "Email" }],
|
||||
mappings: [
|
||||
{ sourceColumn: "Email Address", targetField: "email", required: true, label: "Email" },
|
||||
{ sourceColumn: "Member Name", targetField: "name" },
|
||||
],
|
||||
rows: [
|
||||
{ rowNumber: 10, values: { "Email Address": "a@example.com", "Member Name": "Ada", Ignored: "secret=not-mapped" } },
|
||||
{ rowNumber: 11, values: { "Email Address": "b@example.com", "Member Name": "Ben" } },
|
||||
],
|
||||
});
|
||||
|
||||
expect(output.summary).toMatchObject({ format: "csv", totalRows: 2, acceptedRows: 2, rejectedRows: 0, diagnostics: [] });
|
||||
expect(output.rows).toEqual([
|
||||
{ ok: true, rowNumber: 10, normalized: { email: "a@example.com", name: "Ada" }, warnings: undefined },
|
||||
{ ok: true, rowNumber: 11, normalized: { email: "b@example.com", name: "Ben" }, warnings: undefined },
|
||||
]);
|
||||
expect(output.mappedRows).toEqual([
|
||||
{ rowNumber: 10, normalized: { email: "a@example.com", name: "Ada" }, sourceColumns: ["Email Address", "Member Name"] },
|
||||
{ rowNumber: 11, normalized: { email: "b@example.com", name: "Ben" }, sourceColumns: ["Email Address", "Member Name"] },
|
||||
]);
|
||||
expect(JSON.stringify(output)).not.toContain("not-mapped");
|
||||
});
|
||||
|
||||
it("rejects missing required mapped values deterministically", () => {
|
||||
const output = createImportDryRunPreview({
|
||||
format: "csv",
|
||||
fields: [{ targetField: "email", required: true, label: "Email" }],
|
||||
mappings: [{ sourceColumn: "Email Address", targetField: "email", required: true, label: "Email" }],
|
||||
rows: [
|
||||
{ rowNumber: 1, values: { "Email Address": "valid@example.com" } },
|
||||
{ rowNumber: 2, values: { "Email Address": " " } },
|
||||
{ rowNumber: 3, values: {} },
|
||||
],
|
||||
});
|
||||
|
||||
expect(output.summary).toMatchObject({ totalRows: 3, acceptedRows: 1, rejectedRows: 2 });
|
||||
expect(output.rows[1]).toMatchObject({
|
||||
ok: false,
|
||||
rowNumber: 2,
|
||||
errors: [{ severity: "error", code: "missing_required_field", message: "Email is required for import preview.", rowNumber: 2, column: "email" }],
|
||||
});
|
||||
expect(output.rows[2]).toMatchObject({
|
||||
ok: false,
|
||||
rowNumber: 3,
|
||||
errors: [{ severity: "error", code: "missing_required_field", message: "Email is required for import preview.", rowNumber: 3, column: "email" }],
|
||||
});
|
||||
expect(output.errorReport).toMatchObject({ rejectedRows: 2 });
|
||||
});
|
||||
|
||||
it("ignores unmapped columns and invalid mapping entries", () => {
|
||||
const output = createImportDryRunPreview({
|
||||
format: "json",
|
||||
fields: [{ targetField: "email", required: true }],
|
||||
mappings: [
|
||||
{ sourceColumn: "email", targetField: "email" },
|
||||
{ sourceColumn: "ignored", targetField: "" },
|
||||
{ sourceColumn: "", targetField: "emptySource" },
|
||||
],
|
||||
rows: [{ values: { email: "a@example.com", ignored: "password:super-secret", extra: "rawBody={secret}" } }],
|
||||
});
|
||||
|
||||
expect(output.summary).toMatchObject({ format: "json", totalRows: 1, acceptedRows: 1, rejectedRows: 0 });
|
||||
expect(output.rows[0]).toMatchObject({ ok: true, rowNumber: 1, normalized: { email: "a@example.com" } });
|
||||
expect(JSON.stringify(output)).not.toContain("super-secret");
|
||||
expect(JSON.stringify(output)).not.toContain("rawBody");
|
||||
});
|
||||
|
||||
it("redacts secret-like required values in preview diagnostics", () => {
|
||||
const output = createImportDryRunPreview({
|
||||
format: "csv",
|
||||
fields: [{ targetField: "apiToken", required: true, label: "token=secret-value" }],
|
||||
mappings: [{ sourceColumn: "Token", targetField: "apiToken" }],
|
||||
rows: [{ rowNumber: 7, values: { Token: "" } }],
|
||||
});
|
||||
|
||||
const serialized = JSON.stringify(output);
|
||||
|
||||
expect(output.summary.diagnostics[0]).toMatchObject({
|
||||
code: "missing_required_field",
|
||||
message: "token= [REDACTED] is required for import preview.",
|
||||
rowNumber: 7,
|
||||
column: "apiToken",
|
||||
valuePreview: "",
|
||||
});
|
||||
expect(serialized).not.toContain("secret-value");
|
||||
expect(serialized).not.toContain("body=");
|
||||
expect(serialized).not.toContain("rawFile");
|
||||
});
|
||||
|
||||
it("represents row-level validation errors without raw file buffers or request bodies", () => {
|
||||
const output: ImportParserOutput = {
|
||||
rows: [
|
||||
{ ok: true, rowNumber: 1, normalized: { email: "valid@example.com", active: true, score: 7, tags: ["new"] } },
|
||||
{
|
||||
ok: false,
|
||||
rowNumber: 2,
|
||||
rejectedValues: { email: "", active: null },
|
||||
errors: [
|
||||
{
|
||||
severity: "error",
|
||||
code: "required",
|
||||
message: "Email is required.",
|
||||
rowNumber: 2,
|
||||
column: "email",
|
||||
valuePreview: "",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
summary: {
|
||||
format: "csv",
|
||||
totalRows: 2,
|
||||
acceptedRows: 1,
|
||||
rejectedRows: 1,
|
||||
diagnostics: [{ severity: "warning", code: "blank_row", message: "Skipped blank row.", rowNumber: 3 }],
|
||||
},
|
||||
errorReport: {
|
||||
diagnostics: [{ severity: "error", code: "required", message: "Email is required.", rowNumber: 2, column: "email" }],
|
||||
rejectedRows: 1,
|
||||
errorReportKey: "imports/reports/import-job-1.json",
|
||||
},
|
||||
};
|
||||
|
||||
const serialized = JSON.stringify(output);
|
||||
|
||||
expect(output.rows[1]).toMatchObject({ ok: false, rowNumber: 2 });
|
||||
expect(serialized).not.toContain("bytes");
|
||||
expect(serialized).not.toContain("buffer");
|
||||
expect(serialized).not.toContain("requestBody");
|
||||
expect(serialized).not.toContain("rawFile");
|
||||
});
|
||||
|
||||
it("keeps parser contracts free of database, storage, framework, and prisma imports", () => {
|
||||
const sourcePath = path.join(path.dirname(fileURLToPath(import.meta.url)), "./index.ts");
|
||||
const source = readFileSync(sourcePath, "utf8");
|
||||
|
||||
expect(source).not.toMatch(/from\s+["'](?:@toolctrl\/db|@toolctrl\/storage|react|react-router|@prisma\/client|\.prisma|prisma)["']/);
|
||||
expect(source).not.toMatch(/import\s+["'](?:@toolctrl\/db|@toolctrl\/storage|react|react-router|@prisma\/client|\.prisma|prisma)["']/);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user