The source name typed during configuration was saved, but the next import looks the source and its imported files up by the subfolder name, so a renamed source looked new: format guessed again, every file pre-checked, and a second source created under the folder name. The name field becomes a read-only SourceNameDisplay showing the subfolder name with a hint, and executeImport writes selectedSource.folder_name on create and update, whatever config.name holds, behind a selectedSource guard. The import card sentence no longer asks to check the source name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
195 lines
7.5 KiB
TypeScript
195 lines
7.5 KiB
TypeScript
// useImportWizard — `executeImport` announces its end on the data channel (#396).
|
|
//
|
|
// No renderer in this project, and the reducer alone cannot reach the import:
|
|
// `executeImport` is a callback of the hook. React's three hooks used by
|
|
// `useImportWizard` are stubbed so the hook runs as a plain function on a
|
|
// prepared state, and the services it calls are mocked. What is pinned here is
|
|
// the ADR 0022 contract: one last notification once the import is over,
|
|
// succeeded or not, after its inserts and file statuses.
|
|
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const harness = vi.hoisted(() => ({
|
|
state: null as unknown,
|
|
dispatch: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("react", async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import("react")>();
|
|
return {
|
|
...actual,
|
|
useReducer: () => [harness.state, harness.dispatch],
|
|
useCallback: (fn: unknown) => fn,
|
|
useEffect: () => undefined,
|
|
};
|
|
});
|
|
|
|
vi.mock("@tauri-apps/api/core", () => ({ invoke: vi.fn(async () => "hash-of-file") }));
|
|
vi.mock("../services/db", () => ({ getDb: vi.fn(), withTransaction: vi.fn() }));
|
|
vi.mock("../services/dataEvents", () => ({
|
|
notifyDataChanged: vi.fn(),
|
|
subscribeDataChanged: vi.fn(() => () => undefined),
|
|
}));
|
|
vi.mock("../services/userPreferenceService", () => ({
|
|
getImportFolder: vi.fn(async () => null),
|
|
setImportFolder: vi.fn(),
|
|
}));
|
|
vi.mock("../services/importSourceService", () => ({
|
|
getAllSources: vi.fn(async () => []),
|
|
getSourceByName: vi.fn(async () => null),
|
|
createSource: vi.fn(async () => 7),
|
|
updateSource: vi.fn(async () => undefined),
|
|
}));
|
|
vi.mock("../services/importedFileService", () => ({
|
|
existsByHash: vi.fn(async () => null),
|
|
createImportedFile: vi.fn(async () => 11),
|
|
updateFileStatus: vi.fn(async () => undefined),
|
|
getFilesBySourceId: vi.fn(async () => []),
|
|
}));
|
|
vi.mock("../services/transactionService", () => ({
|
|
insertBatch: vi.fn(async (rows: unknown[]) => rows.length),
|
|
findDuplicates: vi.fn(async () => []),
|
|
}));
|
|
vi.mock("../services/categorizationService", () => ({
|
|
categorizeBatch: vi.fn(async (descriptions: string[]) =>
|
|
descriptions.map(() => ({ category_id: null, supplier_id: null })),
|
|
),
|
|
}));
|
|
vi.mock("../services/importConfigTemplateService", () => ({
|
|
getAllTemplates: vi.fn(async () => []),
|
|
createTemplate: vi.fn(),
|
|
updateTemplate: vi.fn(),
|
|
deleteTemplate: vi.fn(),
|
|
}));
|
|
|
|
import { useImportWizard, initialWizardState, type WizardState } from "./useImportWizard";
|
|
import { notifyDataChanged } from "../services/dataEvents";
|
|
import { createSource, updateSource } from "../services/importSourceService";
|
|
import { createImportedFile, updateFileStatus } from "../services/importedFileService";
|
|
import { insertBatch } from "../services/transactionService";
|
|
import type { ImportSource, ParsedRow } from "../shared/types";
|
|
|
|
const rows: ParsedRow[] = [1, 2].map((i) => ({
|
|
rowIndex: i,
|
|
raw: [`2026-09-0${i}`, `Achat ${i}`, `-${i}`],
|
|
parsed: { date: `2026-09-0${i}`, description: `Achat ${i}`, amount: -i },
|
|
sourceFilename: "releve.csv",
|
|
}));
|
|
|
|
/** The wizard on its "confirm" step, ready to import two rows of one file. */
|
|
const readyToImport = (): WizardState => ({
|
|
...initialWizardState,
|
|
step: "confirm",
|
|
sourceConfig: { ...initialWizardState.sourceConfig, name: "Desjardins" },
|
|
selectedSource: {
|
|
folder_name: "Desjardins",
|
|
folder_path: "/releves/Desjardins",
|
|
files: [
|
|
{ filename: "releve.csv", file_path: "/releves/Desjardins/releve.csv", size_bytes: 10, modified_at: "" },
|
|
],
|
|
},
|
|
selectedFiles: [
|
|
{ filename: "releve.csv", file_path: "/releves/Desjardins/releve.csv", size_bytes: 10, modified_at: "" },
|
|
],
|
|
parsedPreview: rows,
|
|
previewHeaders: ["date", "description", "amount"],
|
|
duplicateResult: { fileAlreadyImported: false, duplicateRows: [], newRows: rows },
|
|
excludedDuplicateIndices: new Set(),
|
|
});
|
|
|
|
/** The order in which a mock was first called, to compare with the notification. */
|
|
const order = (mock: unknown): number => vi.mocked(mock as () => void).mock.invocationCallOrder[0];
|
|
|
|
beforeEach(() => {
|
|
vi.mocked(notifyDataChanged).mockClear();
|
|
vi.mocked(createSource).mockClear();
|
|
vi.mocked(updateSource).mockClear();
|
|
vi.mocked(createImportedFile).mockClear();
|
|
vi.mocked(updateFileStatus).mockClear();
|
|
vi.mocked(insertBatch).mockClear();
|
|
harness.dispatch.mockClear();
|
|
harness.state = readyToImport();
|
|
});
|
|
|
|
describe("executeImport — final data-change notification (#396)", () => {
|
|
it("notifies once at the end of a successful import, after the inserts", async () => {
|
|
await useImportWizard().executeImport();
|
|
|
|
expect(insertBatch).toHaveBeenCalledTimes(1);
|
|
expect(notifyDataChanged).toHaveBeenCalledTimes(1);
|
|
expect(order(notifyDataChanged)).toBeGreaterThan(order(insertBatch));
|
|
expect(harness.dispatch).toHaveBeenCalledWith({ type: "SET_STEP", payload: "report" });
|
|
});
|
|
|
|
it("notifies once at the end when the inserts fail, after the file statuses are settled", async () => {
|
|
vi.mocked(insertBatch).mockRejectedValueOnce(new Error("database is locked"));
|
|
|
|
await useImportWizard().executeImport();
|
|
|
|
expect(updateFileStatus).toHaveBeenCalledWith(11, "error", 0, "Error: database is locked");
|
|
expect(notifyDataChanged).toHaveBeenCalledTimes(1);
|
|
expect(order(notifyDataChanged)).toBeGreaterThan(order(updateFileStatus));
|
|
});
|
|
|
|
it("notifies once at the end when the import fails before any insert", async () => {
|
|
vi.mocked(createSource).mockRejectedValueOnce(new Error("CHECK constraint failed"));
|
|
|
|
await useImportWizard().executeImport();
|
|
|
|
expect(insertBatch).not.toHaveBeenCalled();
|
|
expect(harness.dispatch).toHaveBeenCalledWith({ type: "SET_STEP", payload: "confirm" });
|
|
expect(notifyDataChanged).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("does not notify when there is nothing to import (no duplicate check yet)", async () => {
|
|
harness.state = { ...readyToImport(), duplicateResult: null };
|
|
|
|
await useImportWizard().executeImport();
|
|
|
|
expect(createSource).not.toHaveBeenCalled();
|
|
expect(notifyDataChanged).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("executeImport — a source is named after its subfolder (#426)", () => {
|
|
/** A name typed in the configuration that differs from the subfolder. */
|
|
const renamed = (): WizardState => {
|
|
const base = readyToImport();
|
|
return { ...base, sourceConfig: { ...base.sourceConfig, name: "Mon compte chèques" } };
|
|
};
|
|
|
|
it("creates a new source under the subfolder name, not the configured name", async () => {
|
|
harness.state = renamed();
|
|
|
|
await useImportWizard().executeImport();
|
|
|
|
expect(createSource).toHaveBeenCalledTimes(1);
|
|
expect(vi.mocked(createSource).mock.calls[0][0]).toMatchObject({ name: "Desjardins" });
|
|
expect(updateSource).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("updates an existing source under the subfolder name, not the configured name", async () => {
|
|
harness.state = {
|
|
...renamed(),
|
|
existingSource: { id: 3, name: "Desjardins" } as ImportSource,
|
|
};
|
|
|
|
await useImportWizard().executeImport();
|
|
|
|
expect(updateSource).toHaveBeenCalledTimes(1);
|
|
expect(vi.mocked(updateSource).mock.calls[0][0]).toBe(3);
|
|
expect(vi.mocked(updateSource).mock.calls[0][1]).toMatchObject({ name: "Desjardins" });
|
|
expect(createSource).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("writes nothing without a selected source", async () => {
|
|
harness.state = { ...renamed(), selectedSource: null };
|
|
|
|
await useImportWizard().executeImport();
|
|
|
|
expect(createSource).not.toHaveBeenCalled();
|
|
expect(updateSource).not.toHaveBeenCalled();
|
|
expect(insertBatch).not.toHaveBeenCalled();
|
|
expect(harness.dispatch).not.toHaveBeenCalled();
|
|
});
|
|
});
|