feat: settings and CSV export
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9cb7fe8c81
commit
8201496bb6
8 files changed
+528
No files matched your search
@@ -0,0 +1,37 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { opsToCsv } from "@/server/csv";
|
||||
import { op } from "./fixtures";
|
||||
|
||||
const users = [
|
||||
{ id: "H", firstName: "H" },
|
||||
{ id: "C", firstName: "C" },
|
||||
];
|
||||
const categories = { courses: { name: "Courses" }, salaire: { name: "Salaire" } };
|
||||
|
||||
describe("opsToCsv", () => {
|
||||
const groceries = op({ kind: "EXPENSE", amount: 8640, accountUserId: "C", label: "Carrefour; Bio", categoryId: "courses", date: "2026-10-27" });
|
||||
|
||||
it("starts with a BOM and a French header", () => {
|
||||
const csv = opsToCsv([groceries], users, categories, {});
|
||||
expect(csv.startsWith("\uFEFFDate;Type;Libellé;Catégorie;Montant;Payé par;Statut;Part H %;Part C %;Affecté à;Note\r\n")).toBe(true);
|
||||
});
|
||||
|
||||
it("quotes fields with separators and uses comma decimals", () => {
|
||||
const lines = opsToCsv([groceries], users, categories, {}).split("\r\n");
|
||||
expect(lines[1]).toBe('27/10/2026;Dépense;"Carrefour; Bio";Courses;-86,40;C;Réel;50;50;;');
|
||||
});
|
||||
|
||||
it("writes incomes positive, planned status, allocation label and escaped notes", () => {
|
||||
const refund = {
|
||||
...op({ kind: "INCOME" as const, amount: 20000, accountUserId: "H", label: "Employeur", categoryId: "salaire", status: "PLANNED" as const, allocatedToRecurringId: "rent", date: "2026-10-05" }),
|
||||
note: 'part "loyer"',
|
||||
};
|
||||
const lines = opsToCsv([refund], users, categories, { rent: "Loyer" }).split("\r\n");
|
||||
expect(lines[1]).toBe('05/10/2026;Entrée;Employeur;Salaire;200,00;H;Prévu;50;50;Loyer;"part ""loyer"""');
|
||||
});
|
||||
|
||||
it("excludes skipped operations", () => {
|
||||
const skipped = op({ kind: "EXPENSE", amount: 100, accountUserId: "H", status: "SKIPPED" });
|
||||
expect(opsToCsv([skipped], users, categories, {}).split("\r\n").filter(Boolean)).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
import { beforeEach, describe, expect, it } from "vitest";
|
||||
import { hashPassword, verifyPassword } from "@/server/auth";
|
||||
import { prisma } from "@/server/db";
|
||||
import { archiveCategory, changePassword, saveCategory, setDefaultSplit, updateUser } from "@/server/services/settings";
|
||||
import { makeUsers, resetDb } from "./helpers";
|
||||
|
||||
let H: string;
|
||||
let C: string;
|
||||
|
||||
beforeEach(async () => {
|
||||
await resetDb();
|
||||
const u = await makeUsers();
|
||||
H = u.h.id;
|
||||
C = u.c.id;
|
||||
});
|
||||
|
||||
describe("settings", () => {
|
||||
it("renames a person and rejects a login already taken", async () => {
|
||||
expect(await updateUser(H, { firstName: "Hugues", color: "#111111", login: "hugues" })).toEqual({ ok: true });
|
||||
expect((await prisma.user.findUniqueOrThrow({ where: { id: H } })).firstName).toBe("Hugues");
|
||||
expect(await updateUser(C, { firstName: "Léa", color: "#db2777", login: "hugues" })).toEqual({
|
||||
ok: false,
|
||||
error: "Cet identifiant est déjà utilisé.",
|
||||
});
|
||||
});
|
||||
|
||||
it("changes a password only with the current one", async () => {
|
||||
await prisma.user.update({ where: { id: H }, data: { passwordHash: await hashPassword("ancien-mdp") } });
|
||||
expect(await changePassword(H, "faux", "nouveau-mdp")).toEqual({ ok: false, error: "Mot de passe actuel incorrect." });
|
||||
expect(await changePassword(H, "ancien-mdp", "court")).toEqual({ ok: false, error: "Le mot de passe doit faire au moins 8 caractères." });
|
||||
expect(await changePassword(H, "ancien-mdp", "nouveau-mdp")).toEqual({ ok: true });
|
||||
const user = await prisma.user.findUniqueOrThrow({ where: { id: H } });
|
||||
expect(await verifyPassword("nouveau-mdp", user.passwordHash)).toBe(true);
|
||||
});
|
||||
|
||||
it("stores a valid default split only", async () => {
|
||||
expect(await setDefaultSplit({ [H]: 6000, [C]: 4000 })).toEqual({ ok: true });
|
||||
expect((await prisma.setting.findUniqueOrThrow({ where: { id: 1 } })).defaultSplit).toEqual({ [H]: 6000, [C]: 4000 });
|
||||
expect(await setDefaultSplit({ [H]: 6000, [C]: 3000 })).toEqual({ ok: false, error: "La répartition doit totaliser 100 %." });
|
||||
});
|
||||
|
||||
it("creates, renames and archives categories", async () => {
|
||||
const r = await saveCategory({ name: "Animaux", emoji: "🐶", kind: "EXPENSE" });
|
||||
if (!r.ok) throw new Error(r.error);
|
||||
await saveCategory({ id: r.id, name: "Chat", emoji: "🐱", kind: "EXPENSE" });
|
||||
await archiveCategory(r.id, true);
|
||||
const cat = await prisma.category.findUniqueOrThrow({ where: { id: r.id } });
|
||||
expect([cat.name, cat.emoji, cat.archived]).toEqual(["Chat", "🐱", true]);
|
||||
expect(await saveCategory({ name: "Courses", emoji: "🛒", kind: "EXPENSE" })).toEqual({ ok: false, error: "Cette catégorie existe déjà." });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user