first commit
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 1m4s
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 1m4s
This commit is contained in:
commit
6fc0a01c56
51 changed files with 11319 additions and 0 deletions
148
tests/domain.test.js
Normal file
148
tests/domain.test.js
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import {
|
||||
compareRecords,
|
||||
defaultSettings,
|
||||
emptyNutrients,
|
||||
recordSchema,
|
||||
totalNutrients,
|
||||
} from "../shared/domain.js";
|
||||
import { openDatabase } from "../server/db.js";
|
||||
const sample = (
|
||||
id = "entry-1",
|
||||
time = "2026-10-20T10:00:00.000Z",
|
||||
data = {},
|
||||
) => ({
|
||||
id,
|
||||
type: "entry",
|
||||
updatedAt: time,
|
||||
deleted: false,
|
||||
data: {
|
||||
date: "2026-10-20",
|
||||
meal: "Breakfast",
|
||||
name: "Oats",
|
||||
serving: "40 g",
|
||||
quantity: 1,
|
||||
nutrients: { ...emptyNutrients(), calories: 150, protein: 5 },
|
||||
...data,
|
||||
},
|
||||
});
|
||||
|
||||
test("nutrition totals honor fractional servings without changing source foods", () => {
|
||||
const entry = sample().data;
|
||||
const total = totalNutrients([
|
||||
{ ...entry, quantity: 1.5 },
|
||||
{ ...entry, quantity: 0.5 },
|
||||
]);
|
||||
assert.equal(total.calories, 300);
|
||||
assert.equal(total.protein, 10);
|
||||
assert.equal(entry.nutrients.calories, 150);
|
||||
});
|
||||
test("schemas reject impossible dates, negative nutrition, unknown fields, and reserved IDs", () => {
|
||||
assert.equal(recordSchema.safeParse(sample()).success, true);
|
||||
assert.equal(recordSchema.safeParse(sample("settings")).success, false);
|
||||
assert.equal(
|
||||
recordSchema.safeParse(sample("x", undefined, { date: "2026-02-30" }))
|
||||
.success,
|
||||
false,
|
||||
);
|
||||
assert.equal(
|
||||
recordSchema.safeParse(
|
||||
sample("x", undefined, {
|
||||
nutrients: { ...emptyNutrients(), protein: -1 },
|
||||
}),
|
||||
).success,
|
||||
false,
|
||||
);
|
||||
assert.equal(
|
||||
recordSchema.safeParse(sample("x", undefined, { admin: true })).success,
|
||||
false,
|
||||
);
|
||||
assert.equal(
|
||||
recordSchema.safeParse({
|
||||
id: "settings",
|
||||
type: "settings",
|
||||
updatedAt: new Date().toISOString(),
|
||||
deleted: false,
|
||||
data: defaultSettings,
|
||||
}).success,
|
||||
true,
|
||||
);
|
||||
});
|
||||
test("same-millisecond conflicts resolve identically in either arrival order", () => {
|
||||
const a = sample();
|
||||
const b = sample("entry-1", a.updatedAt, { name: "Banana" });
|
||||
assert.equal(
|
||||
Math.sign(compareRecords(a, b)),
|
||||
-Math.sign(compareRecords(b, a)),
|
||||
);
|
||||
const left = openDatabase(":memory:");
|
||||
const right = openDatabase(":memory:");
|
||||
left.merge([a]);
|
||||
left.merge([b]);
|
||||
right.merge([b]);
|
||||
right.merge([a]);
|
||||
assert.deepEqual(left.all(), right.all());
|
||||
left.close();
|
||||
right.close();
|
||||
});
|
||||
test("independent offline additions survive, latest edits win, stale writes cannot resurrect deletions", () => {
|
||||
const store = openDatabase(":memory:");
|
||||
const first = sample();
|
||||
const newer = {
|
||||
...sample("entry-1", "2026-10-20T11:00:00.000Z"),
|
||||
deleted: true,
|
||||
};
|
||||
store.merge([first, sample("entry-2")]);
|
||||
store.merge([newer]);
|
||||
store.merge([first, first]);
|
||||
assert.equal(store.all().length, 2);
|
||||
assert.equal(store.all()[0].deleted, true);
|
||||
store.close();
|
||||
});
|
||||
test("batch is atomic on a type collision", () => {
|
||||
const store = openDatabase(":memory:");
|
||||
store.merge([sample()]);
|
||||
assert.throws(() =>
|
||||
store.merge([sample("entry-2"), { ...sample(), type: "food" }]),
|
||||
);
|
||||
assert.equal(store.all().length, 1);
|
||||
store.close();
|
||||
});
|
||||
test("opening the same database after an app update preserves records and sessions", () => {
|
||||
const directory = mkdtempSync(join(tmpdir(), "calio-db-"));
|
||||
const filename = join(directory, "calio.sqlite");
|
||||
try {
|
||||
const store = openDatabase(filename);
|
||||
store.merge([sample()]);
|
||||
store.db
|
||||
.prepare("INSERT INTO sessions VALUES (?,?)")
|
||||
.run("session", Date.now() + 60000);
|
||||
store.close();
|
||||
const reopened = openDatabase(filename);
|
||||
assert.deepEqual(reopened.all(), [sample()]);
|
||||
assert.ok(reopened.db.prepare("SELECT * FROM sessions").get());
|
||||
assert.equal(
|
||||
reopened.db.prepare("PRAGMA user_version").get().user_version,
|
||||
1,
|
||||
);
|
||||
reopened.close();
|
||||
} finally {
|
||||
rmSync(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
test("a newer database refuses an unsafe application downgrade", () => {
|
||||
const directory = mkdtempSync(join(tmpdir(), "calio-version-"));
|
||||
const filename = join(directory, "calio.sqlite");
|
||||
try {
|
||||
const store = openDatabase(filename);
|
||||
store.db.exec("PRAGMA user_version=2");
|
||||
store.close();
|
||||
assert.throws(() => openDatabase(filename), /newer Calio/);
|
||||
} finally {
|
||||
rmSync(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue