|
| 1 | +import { DatabaseSync } from "node:sqlite"; |
| 2 | +import { expect, test } from "@playwright/test"; |
| 3 | +import { activeProjectDbFile, expectUndoTop, goto, uniqueName } from "./helpers"; |
| 4 | + |
| 5 | +test("a secondary goal accepts and persists a negative consume rate", async ({ page }) => { |
| 6 | + // Block 38's shape: Tar remains the primary sink while Shale oil is added as |
| 7 | + // another goal and changed from the default production rate to consumption. |
| 8 | + const data = { |
| 9 | + recipes: [], |
| 10 | + goals: [ |
| 11 | + { name: "tar", rate: -9.575 }, |
| 12 | + { name: "scrude", rate: 1 }, |
| 13 | + ], |
| 14 | + }; |
| 15 | + const db = new DatabaseSync(activeProjectDbFile()); |
| 16 | + let id: number; |
| 17 | + try { |
| 18 | + db.exec("PRAGMA busy_timeout = 5000"); |
| 19 | + const inserted = db |
| 20 | + .prepare("INSERT INTO blocks (name, data) VALUES (?, ?)") |
| 21 | + .run(uniqueName("Secondary sink"), JSON.stringify(data)); |
| 22 | + id = Number(inserted.lastInsertRowid); |
| 23 | + } finally { |
| 24 | + db.close(); |
| 25 | + } |
| 26 | + |
| 27 | + await goto(page, `/block/${id}`); |
| 28 | + await page.getByRole("button", { name: "1", exact: true }).click(); |
| 29 | + const input = page.locator("input:focus"); |
| 30 | + await input.fill("-2.5"); |
| 31 | + await input.press("Enter"); |
| 32 | + |
| 33 | + await expect(page.getByRole("button", { name: "-2.5", exact: true })).toBeVisible(); |
| 34 | + await expectUndoTop(page, /Set "Shale oil" rate/); |
| 35 | + |
| 36 | + const addConsumer = page.getByRole("button", { |
| 37 | + name: "add a recipe that consumes Shale oil", |
| 38 | + }); |
| 39 | + await expect(addConsumer).toBeVisible(); |
| 40 | + await addConsumer.click(); |
| 41 | + await expect(page.getByRole("dialog", { name: "Recipes that consume Shale oil" })).toBeVisible(); |
| 42 | + |
| 43 | + const saved = new DatabaseSync(activeProjectDbFile(), { readOnly: true }); |
| 44 | + try { |
| 45 | + const row = saved.prepare("SELECT data FROM blocks WHERE id = ?").get(id) as { data: string }; |
| 46 | + const parsed = JSON.parse(row.data) as { goals: { name: string; rate: number }[] }; |
| 47 | + expect(parsed.goals.find((goal) => goal.name === "scrude")?.rate).toBe(-2.5); |
| 48 | + } finally { |
| 49 | + saved.close(); |
| 50 | + } |
| 51 | +}); |
0 commit comments