77 lines
2.3 KiB
JavaScript
77 lines
2.3 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import request from "supertest";
|
|
import { setup, signup } from "./helpers.js";
|
|
import { buildComparison } from "../shared/roast-comparison.js";
|
|
test("comparison uses per-roast observations without changing planned or uploaded readings", () => {
|
|
const detail = {
|
|
plan: {
|
|
fields: { 1.4: "8:00", 4.3: "1:30" },
|
|
planActual: { fc: { note: "Legacy note" } },
|
|
},
|
|
parsed: { milestones: [{ key: "fc", timeS: 450, tempC: 190 }] },
|
|
observations: { fc: { timeS: 460, tempC: 191, note: "Heard clearly" } },
|
|
};
|
|
const fc = buildComparison(detail).find((r) => r.key === "fc");
|
|
assert.equal(fc.target.timeS, 480);
|
|
assert.equal(fc.timeS, 460);
|
|
assert.equal(fc.deltaS, -20);
|
|
assert.equal(fc.note, "Heard clearly");
|
|
assert.equal(detail.parsed.milestones[0].timeS, 450);
|
|
assert.equal(
|
|
buildComparison({ ...detail, observations: {} }).find((r) => r.key === "fc")
|
|
.note,
|
|
"Legacy note",
|
|
);
|
|
});
|
|
test("roast observations persist per owner and reject invalid values", async () => {
|
|
const { app, agent, db } = await setup();
|
|
const { csrf } = await signup(agent, "[email protected]");
|
|
const other = request.agent(app);
|
|
const o = await signup(other, "[email protected]");
|
|
const owner = (
|
|
await db.query("SELECT id FROM users WHERE email=$1", [
|
|
"[email protected]",
|
|
])
|
|
).rows[0].id;
|
|
const row = (
|
|
await db.query(
|
|
"INSERT INTO actual_roasts(user_id,filename,original_content,parsed) VALUES($1,'sample.alog','{}',$2) RETURNING id",
|
|
[owner, { milestones: [{ key: "fc", timeS: 450, tempC: 190 }] }],
|
|
)
|
|
).rows[0];
|
|
const observations = {
|
|
fc: { timeS: 460, tempC: 191, note: "Audible first crack" },
|
|
};
|
|
assert.equal(
|
|
(
|
|
await agent
|
|
.put("/api/roasts/" + row.id)
|
|
.set("x-csrf-token", csrf)
|
|
.send({ observations })
|
|
).status,
|
|
200,
|
|
);
|
|
const saved = (await agent.get("/api/roasts/" + row.id)).body.roast;
|
|
assert.deepEqual(saved.observations, observations);
|
|
assert.equal(saved.parsed.milestones[0].timeS, 450);
|
|
assert.equal(
|
|
(
|
|
await other
|
|
.put("/api/roasts/" + row.id)
|
|
.set("x-csrf-token", o.csrf)
|
|
.send({ observations: {} })
|
|
).status,
|
|
404,
|
|
);
|
|
assert.equal(
|
|
(
|
|
await agent
|
|
.put("/api/roasts/" + row.id)
|
|
.set("x-csrf-token", csrf)
|
|
.send({ observations: { fc: { timeS: -1 } } })
|
|
).status,
|
|
400,
|
|
);
|
|
});
|