|
| 1 | +import { DatabaseSync } from "node:sqlite"; |
| 2 | +import { expect, test } from "@playwright/test"; |
| 3 | +import { activeProjectDbFile, addGoal, createBlock, expectUndoTop } from "./helpers"; |
| 4 | + |
| 5 | +test("temporary campaign derives a finite target and can be completed and reactivated", async ({ |
| 6 | + page, |
| 7 | +}) => { |
| 8 | + const id = await createBlock(page); |
| 9 | + await addGoal(page, "iron plate", "Iron plate"); |
| 10 | + |
| 11 | + await page.locator('button[aria-label^="Add a recipe that makes "]').click(); |
| 12 | + const recipePicker = page.getByRole("dialog", { name: /Recipes that make/ }); |
| 13 | + await recipePicker.locator('[data-recipe-candidate="iron-plate"]').click(); |
| 14 | + await expect(recipePicker).toBeHidden(); |
| 15 | + |
| 16 | + await page.getByRole("button", { name: "Make this block temporary", exact: true }).click(); |
| 17 | + const quantity = page.getByRole("button", { name: "Make 3,600", exact: true }); |
| 18 | + await expect(quantity).toBeVisible(); |
| 19 | + await quantity.click(); |
| 20 | + const quantityInput = page.locator('input[inputmode="decimal"]:focus'); |
| 21 | + await quantityInput.fill("5"); |
| 22 | + await quantityInput.press("Enter"); |
| 23 | + |
| 24 | + const durationUnit = page.getByTitle( |
| 25 | + "Campaign duration unit — click to cycle seconds / minutes / hours", |
| 26 | + ); |
| 27 | + await expect(durationUnit).toHaveText("h"); |
| 28 | + await durationUnit.click(); |
| 29 | + await expect(durationUnit).toHaveText("s"); |
| 30 | + await durationUnit.click(); |
| 31 | + await expect(durationUnit).toHaveText("min"); |
| 32 | + const duration = page.getByRole("textbox", { name: "Campaign duration" }); |
| 33 | + await duration.fill("30"); |
| 34 | + await duration.press("Enter"); |
| 35 | + |
| 36 | + const ironImport = page.getByRole("button", { name: /^Iron ore 40 total/ }); |
| 37 | + await expect(ironImport).toBeVisible(); |
| 38 | + await expect(ironImport.locator("[data-campaign-rate]")).toHaveText("0.022/s"); |
| 39 | + const balance = page.locator('[data-slot="card"]').filter({ hasText: "Block balance" }); |
| 40 | + await expect(balance.getByText(/^avg /)).toHaveCount(0); |
| 41 | + await expect(balance.getByText("<0.01", { exact: true }).first()).toBeVisible(); |
| 42 | + |
| 43 | + await page.getByRole("combobox", { name: "Campaign confidence" }).click(); |
| 44 | + await page.getByRole("option", { name: "90% confidence" }).click(); |
| 45 | + const goal = page.locator('[data-goal="iron-plate"]'); |
| 46 | + await expect(goal).toContainText(/Make 5/); |
| 47 | + await expect(goal).not.toContainText(/plan |\/s/); |
| 48 | + await expectUndoTop(page, /Set campaign confidence|Edit block/); |
| 49 | + |
| 50 | + await page.getByRole("button", { name: "Complete temporary campaign", exact: true }).click(); |
| 51 | + await expect( |
| 52 | + page.getByRole("button", { name: "Reactivate temporary campaign", exact: true }), |
| 53 | + ).toBeVisible(); |
| 54 | + await expect(page.getByRole("status").filter({ hasText: /removed from factory planning/ })).toBeVisible(); |
| 55 | + |
| 56 | + const completedDb = new DatabaseSync(activeProjectDbFile(), { readOnly: true }); |
| 57 | + try { |
| 58 | + const row = completedDb.prepare("SELECT enabled, data FROM blocks WHERE id = ?").get(id) as { |
| 59 | + enabled: number; |
| 60 | + data: string; |
| 61 | + }; |
| 62 | + const data = JSON.parse(row.data) as { |
| 63 | + campaign?: { completedAt?: string; duration?: number }; |
| 64 | + }; |
| 65 | + expect(row.enabled).toBe(0); |
| 66 | + expect(data.campaign?.duration).toBe(1800); |
| 67 | + expect(data.campaign?.completedAt).toBeTruthy(); |
| 68 | + } finally { |
| 69 | + completedDb.close(); |
| 70 | + } |
| 71 | + |
| 72 | + await page |
| 73 | + .getByRole("button", { name: "Reactivate temporary campaign", exact: true }) |
| 74 | + .click(); |
| 75 | + await expect( |
| 76 | + page.getByRole("button", { name: "Complete temporary campaign", exact: true }), |
| 77 | + ).toBeVisible(); |
| 78 | + await expect(page.getByRole("status").filter({ hasText: /campaign reactivated/ })).toBeVisible(); |
| 79 | + |
| 80 | + const activeDb = new DatabaseSync(activeProjectDbFile(), { readOnly: true }); |
| 81 | + try { |
| 82 | + const row = activeDb.prepare("SELECT enabled, data FROM blocks WHERE id = ?").get(id) as { |
| 83 | + enabled: number; |
| 84 | + data: string; |
| 85 | + }; |
| 86 | + const data = JSON.parse(row.data) as { campaign?: { completedAt?: string } }; |
| 87 | + expect(row.enabled).toBe(1); |
| 88 | + expect(data.campaign?.completedAt).toBeUndefined(); |
| 89 | + } finally { |
| 90 | + activeDb.close(); |
| 91 | + } |
| 92 | +}); |
0 commit comments