Public: anyone can read this; the crew can change it.
A granted app's two-page print comes out as one sheet, printed on both sides #18194
Merged 14h 13m ago, live in production
mason/forge:mason/18190-a-granted-app-s-two-page-print-comes-out → main 0 commits fork 2 threads · 2 unresolved
13 files
changed +249
−40
- docs/features/devices.md
- src/actions/devices.ts
- src/contract/openapi/routes/postApiV1DevicesIdPrint.ts
- src/core/migration-pins/20261204110000-print-job-sheets.pin
- src/core/migrations/20261204110000-print-job-sheets.ts
- src/core/print_log.ts
- src/devices/ipp.ts
- src/devices/print.ts
- src/http/api/print.ts
- src/tui/screens/devices.ts
- src/web/settings/devices.ts
- test/print.test.ts
- test/print_raster.test.ts
docs/features/devices.md +2 −1
| @@ -20,6 +20,7 @@ row: Devices | ||
| 20 | 20 | - Checked before anything leaves for the LAN: at most 10 MB and 20 pages, `%PDF-` header and `%%EOF` trailer, otherwise 422. Pages are counted from the plain page tree; a PDF whose tree is in a compressed object stream answers 422 `pages_unknown`. Not granted, or not a declared device: 403. Printer silent or refusing: 502. |
| 21 | 21 | - The route takes a device number only; the job goes to the declared address, so an app reaches no other LAN host through it. Its cell's own egress is unchanged (`src/apps/egress.ts` still opens only granted `host:port`s). |
| 22 | 22 | - An app reads how a job it sent ended with `GET $FORGE_API_URL/devices/<id>/print/<job>?within=<seconds>` (the same token and grant; `<job>` is the `job.id` the print answered with, which the forge keeps on the `print_job` row). The forge asks the printer (IPP Get-Job-Attributes) about every half second for up to `within` seconds (default 10, at most 30) and answers `{"job":{"id","state","printer","reasons"}}` with `state` one of `completed`, `failed` (the printer reports canceled, aborted or stopped) or `timed_out` (still pending, held or processing when the time ran out; `printer` says which). Only `completed` means the page came out: an app should say "sent, not confirmed" for anything else. A job this app did not send is 404 `job_unknown`; a printer that never answers is 502; an app may read 60 times a minute and hold 2 reads open at once; past either, 429 `rate_limited`. The terminal door is the Devices screen, which lists each print (`src/tui/screens/devices.ts`). |
| 23 | - `?sides=` is `one-sided` (the default; no IPP `sides` attribute is sent) or `two-sided-long-edge`; anything else is 400 `invalid_sides`, answered before the body is read. Two-sided is sent only when the printer's `sides-supported` (Get-Printer-Attributes) lists it; otherwise 422 `sides_unsupported` and nothing is printed. A printer that ignores `sides` is not detectable: the cap counts what was asked. | |
| 23 | 24 | - Each print appends `device.printed` (device, app, pages). |
| 24 | 25 | - The app and grant are checked before the body is read, so a caller without a grant costs a lookup, not 10 MB. An app may start 10 prints a minute and have 2 in flight; past that, 429 `rate_limited`. |
| 25 | 26 | - A PDF containing an object stream (`/ObjStm`) answers 422 `pages_unknown`: the page tree could hide there behind a plain decoy. |
| @@ -27,6 +28,6 @@ row: Devices | ||
| 27 | 28 | |
| 28 | 29 | ## Daily limit and the print log |
| 29 | 30 | |
| 30 | - Each grant has a daily cap on jobs, default 10, set by the owner from 0 (no prints) to 1000. Jobs that reached the printer (printed or failed there) count, per UTC day; a refusal never uses up the day. Past the cap the app gets 429 `daily_cap` and can print again after midnight UTC. In-flight jobs count, and a place is reserved before the body is read, so parallel requests cannot slip past the last one. | |
| 31 | - Each grant has a daily cap on sheets of paper, default 10, set by the owner from 0 (no prints) to 1000. Jobs that reached the printer (printed or failed there) count their sheets, per UTC day: a page each one-sided, a page for two when `?sides=two-sided-long-edge`, so a two-page two-sided job is one sheet and a two-page one-sided job is two. A job whose sheets do not fit what is left is 429 `daily_cap` before anything is sent; a refusal never uses up the day. Past the cap the app gets 429 `daily_cap` and can print again after midnight UTC. In-flight jobs count their reserved sheets, and a place is reserved before the body is read, so parallel requests cannot slip past the last one. | |
| 31 | 32 | - Every job is a row in `print_job` (app, device, pages, outcome, time). A cap refusal is written once per app, device and day; refusals are pruned to the last 100 per device and counted rows are dropped after 30 days, so a caller cannot grow the log. |
| 32 | 33 | - The device page shows "n of cap printed today" per app, a "Daily limit" row to set it and a "Recent prints" row (last 10). In the terminal's Devices screen `l` sets the limit (slug, then number) and each row lists the last 5 prints; both doors use `setCap`, admin only, and append `device.cap_set` (device, app, cap). |
src/actions/devices.ts +1 −1
| @@ -84,7 +84,7 @@ export function revoke(app: App, actor: User | undefined, id: number, slug: stri | ||
| 84 | 84 | return ok(gone.value.slug); |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | /** The owner sets how many jobs one app may print on one device each day. */ | |
| 87 | /** The owner sets how many sheets one app may print on one device each day. */ | |
| 88 | 88 | export function setCap(app: App, actor: User | undefined, id: number, slug: string, cap: number): Result<string, DeviceActionError> { |
| 89 | 89 | if (!mayManageDevices(actor)) return err("forbidden"); |
| 90 | 90 | const device = Number.isSafeInteger(id) ? getDevice(app.db, id) : undefined; |
src/contract/openapi/routes/postApiV1DevicesIdPrint.ts +4 −1
| @@ -4,10 +4,13 @@ export const route: Route = { | ||
| 4 | 4 | method: "post", |
| 5 | 5 | path: "/api/v1/devices/{id}/print", |
| 6 | 6 | summary: "Print the PDF or PWG raster in the request body on a device the app was granted", |
| 7 | detail: "Print the PDF (application/pdf) or PWG raster (image/pwg-raster, up to 80 MB, 20 pages) in the request body on a device the app was granted (the app's own token); answers 200 with the job", | |
| 7 | detail: "Print the PDF (application/pdf) or PWG raster (image/pwg-raster, up to 80 MB, 20 pages) in the request body on a device the app was granted (the app's own token); answers 200 with the job. Optional ?sides=: one-sided (the default) or two-sided-long-edge, which puts a second page on the back of the sheet and counts one sheet for two pages against the daily limit", | |
| 8 | 8 | auth: true, |
| 9 | 9 | role: "member", |
| 10 | 10 | refusals: { |
| 11 | invalid_sides: "400: sides must be one-sided (the default) or two-sided-long-edge.", | |
| 12 | sides_unsupported: "422: This printer does not say it prints on both sides; ask for one-sided.", | |
| 13 | daily_cap: "429: This app has used its sheets for today on this printer; it can print again tomorrow.", | |
| 11 | 14 | forbidden: "403: the app holds no grant on this device", |
| 12 | 15 | rate_limited: "429: the app started more than 10 prints in a minute, or has 2 in flight", |
| 13 | 16 | too_large: "422: the PDF is over the size bound", |
src/core/migration-pins/20261204110000-print-job-sheets.pin added +1
| @@ -0,0 +1,1 @@ | ||
| 1 | 22288d79fe541941a587335fe1e82812e783f525fd8c065e21c21ea495c79b66 |
src/core/migrations/20261204110000-print-job-sheets.ts added +6
| @@ -0,0 +1,6 @@ | ||
| 1 | // Sheets of paper on each print_job row (#18190): the daily cap counts sheets, so a | |
| 2 | // two-page two-sided job is one and a two-page one-sided job is two. Rows older than | |
| 3 | // this were counted as one job each and stay one. Additive. | |
| 4 | export default ` | |
| 5 | ALTER TABLE print_job ADD COLUMN sheets INTEGER NOT NULL DEFAULT 1; | |
| 6 | `; |
src/core/print_log.ts +8 −7
| @@ -1,8 +1,8 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * The print log and the daily cap. One row per job an app starts on a device; |
| 3 | * the cap counts the jobs of the UTC day that reached the printer (printed or | |
| 4 | * failed there), so a refusal never uses up the day. The cap is a number on | |
| 5 | * the owner's grant, default 10. | |
| 3 | * the cap counts the sheets of the UTC day that reached the printer (printed or | |
| 4 | * failed there; a job with no `sheets` is one), so a refusal never uses up the day. The cap is a | |
| 5 | * number on the owner's grant, default 10 sheets. | |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | import type { Database } from "bun:sqlite"; |
| @@ -33,7 +33,7 @@ export function dailyCap(db: Database, deviceId: number, appId: string): number | ||
| 33 | 33 | export function printedToday(db: Database, deviceId: number, appId: string, now = new Date()): number { |
| 34 | 34 | return db |
| 35 | 35 | .query<{ n: number }, [number, string, string]>( |
| 36 | "SELECT COUNT(*) AS n FROM print_job WHERE device_id = ?1 AND app_id = ?2 AND day = ?3 AND outcome IN ('printed','failed')", | |
| 36 | "SELECT COALESCE(SUM(sheets), 0) AS n FROM print_job WHERE device_id = ?1 AND app_id = ?2 AND day = ?3 AND outcome IN ('printed','failed')", | |
| 37 | 37 | ) |
| 38 | 38 | .get(deviceId, appId, dayOf(now))!.n; |
| 39 | 39 | } |
| @@ -60,7 +60,7 @@ export const OUTCOME_WORDS: Record<Outcome, string> = { | ||
| 60 | 60 | */ |
| 61 | 61 | export function logJob( |
| 62 | 62 | db: Database, |
| 63 | job: { deviceId: number; appId: string; pages: number; outcome: Outcome; jobId?: number | null }, | |
| 63 | job: { deviceId: number; appId: string; pages: number; outcome: Outcome; jobId?: number | null; sheets?: number }, | |
| 64 | 64 | now = new Date(), |
| 65 | 65 | ): void { |
| 66 | 66 | if ( |
| @@ -70,7 +70,7 @@ export function logJob( | ||
| 70 | 70 | .get(job.deviceId, job.appId, dayOf(now)) |
| 71 | 71 | ) |
| 72 | 72 | return; |
| 73 | db.query("INSERT INTO print_job (device_id, app_id, pages, outcome, day, at, job_id) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)").run( | |
| 73 | db.query("INSERT INTO print_job (device_id, app_id, pages, outcome, day, at, job_id, sheets) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)").run( | |
| 74 | 74 | job.deviceId, |
| 75 | 75 | job.appId, |
| 76 | 76 | job.pages, |
| @@ -78,6 +78,7 @@ export function logJob( | ||
| 78 | 78 | dayOf(now), |
| 79 | 79 | now.toISOString(), |
| 80 | 80 | job.jobId ?? null, |
| 81 | job.sheets ?? 1, | |
| 81 | 82 | ); |
| 82 | 83 | if (job.outcome === "printed" || job.outcome === "failed") { |
| 83 | 84 | db.query("DELETE FROM print_job WHERE day < ?1").run(dayOf(new Date(now.getTime() - KEEP_DAYS * 86_400_000))); |
| @@ -100,7 +101,7 @@ export function grantLimits(db: Database, deviceId: number, now = new Date()): G | ||
| 100 | 101 | return db |
| 101 | 102 | .query<GrantLimit, [number, string]>( |
| 102 | 103 | `SELECT a.slug AS slug, g.daily_cap AS cap, |
| 103 | (SELECT COUNT(*) FROM print_job j WHERE j.device_id = g.device_id AND j.app_id = g.app_id AND j.day = ?2 | |
| 104 | (SELECT COALESCE(SUM(j.sheets), 0) FROM print_job j WHERE j.device_id = g.device_id AND j.app_id = g.app_id AND j.day = ?2 | |
| 104 | 105 | AND j.outcome IN ('printed','failed')) AS today |
| 105 | 106 | FROM device_grant g JOIN app a ON a.id = g.app_id WHERE g.device_id = ?1 ORDER BY a.slug`, |
| 106 | 107 | ) |
src/devices/ipp.ts +12 −5
| @@ -10,6 +10,7 @@ const OP_GET_JOB_ATTRIBUTES = 0x0009; // RFC 8011 §4.3.4 | ||
| 10 | 10 | const OP_GET_PRINTER_ATTRIBUTES = 0x000b; // RFC 8011 §5.2 operation-id registry |
| 11 | 11 | const TAG = { |
| 12 | 12 | operation: 0x01, |
| 13 | job: 0x02, | |
| 13 | 14 | end: 0x03, |
| 14 | 15 | integer: 0x21, |
| 15 | 16 | boolean: 0x22, |
| @@ -31,6 +32,7 @@ const WANTED = [ | ||
| 31 | 32 | "marker-levels", |
| 32 | 33 | "marker-types", |
| 33 | 34 | "marker-colors", |
| 35 | "sides-supported", | |
| 34 | 36 | ]; |
| 35 | 37 | |
| 36 | 38 | const RASTER_WANTED = ["pwg-raster-document-resolution-supported", "pwg-raster-document-type-supported"]; |
| @@ -122,6 +124,8 @@ export interface PrinterReport { | ||
| 122 | 124 | readonly reasons: readonly string[]; |
| 123 | 125 | readonly message: string; |
| 124 | 126 | readonly supplies: readonly Supply[]; |
| 127 | /** `sides-supported` keywords (RFC 8011 §5.2.8); empty when the printer lists none. */ | |
| 128 | readonly sides: readonly string[]; | |
| 125 | 129 | } |
| 126 | 130 | |
| 127 | 131 | const str = (a: IppAttributes, k: string): string[] => |
| @@ -142,6 +146,7 @@ export function reportOf(attributes: IppAttributes): PrinterReport { | ||
| 142 | 146 | state: stateOf(attributes.get("printer-state")?.[0]), |
| 143 | 147 | reasons: str(attributes, "printer-state-reasons").filter((r) => r !== "none"), |
| 144 | 148 | message: str(attributes, "printer-state-message")[0] ?? "", |
| 149 | sides: str(attributes, "sides-supported"), | |
| 145 | 150 | supplies: names.map((name, i) => { |
| 146 | 151 | const level = levels[i]; |
| 147 | 152 | return { |
| @@ -229,7 +234,7 @@ async function exchange(url: string, body: Uint8Array, timeoutMs: number): Promi | ||
| 229 | 234 | } |
| 230 | 235 | |
| 231 | 236 | /** The request body for Print-Job: the document follows the attributes. */ |
| 232 | export function encodePrintJob(printerUri: string, pdf: Uint8Array, jobName: string, requestId = 1, format = "application/pdf"): Uint8Array { | |
| 237 | export function encodePrintJob(printerUri: string, pdf: Uint8Array, jobName: string, requestId = 1, format = "application/pdf", sides?: string): Uint8Array { | |
| 233 | 238 | const head: number[] = [ |
| 234 | 239 | 1, 1, |
| 235 | 240 | OP_PRINT_JOB >> 8, OP_PRINT_JOB & 255, |
| @@ -241,6 +246,8 @@ export function encodePrintJob(printerUri: string, pdf: Uint8Array, jobName: str | ||
| 241 | 246 | ...attr(TAG.nameWithoutLanguage, "requesting-user-name", "forge"), |
| 242 | 247 | ...attr(TAG.nameWithoutLanguage, "job-name", jobName), |
| 243 | 248 | ...attr(TAG.mimeType, "document-format", format), |
| 249 | // The job attribute group is sent only when the caller asked for sides: no `sides`, no group. | |
| 250 | ...(sides === undefined ? [] : [TAG.job, ...attr(TAG.keyword, "sides", sides)]), | |
| 244 | 251 | TAG.end, |
| 245 | 252 | ]; |
| 246 | 253 | const out = new Uint8Array(head.length + pdf.length); |
| @@ -271,13 +278,13 @@ export type PrintResult = | ||
| 271 | 278 | * IPP Print-Job and return the job state the printer reports. A printer that |
| 272 | 279 | * takes a long document needs longer than a probe does. |
| 273 | 280 | */ |
| 274 | export async function printPdf(url: string, pdf: Uint8Array, jobName: string, timeoutMs = 30_000): Promise<PrintResult> { | |
| 275 | return printDocument(url, pdf, jobName, "application/pdf", timeoutMs); | |
| 281 | export async function printPdf(url: string, pdf: Uint8Array, jobName: string, timeoutMs = 30_000, sides?: string): Promise<PrintResult> { | |
| 282 | return printDocument(url, pdf, jobName, "application/pdf", timeoutMs, sides); | |
| 276 | 283 | } |
| 277 | 284 | |
| 278 | 285 | /** The same Print-Job with another document-format (PWG raster is `image/pwg-raster`). */ |
| 279 | export async function printDocument(url: string, doc: Uint8Array, jobName: string, format: string, timeoutMs = 30_000): Promise<PrintResult> { | |
| 280 | const answer = await exchange(url, encodePrintJob(url, doc, jobName, 1, format), timeoutMs); | |
| 286 | export async function printDocument(url: string, doc: Uint8Array, jobName: string, format: string, timeoutMs = 30_000, sides?: string): Promise<PrintResult> { | |
| 287 | const answer = await exchange(url, encodePrintJob(url, doc, jobName, 1, format, sides), timeoutMs); | |
| 281 | 288 | if (!answer.ok) return answer; |
| 282 | 289 | const a = answer.response.attributes; |
| 283 | 290 | const id = a.get("job-id")?.[0]; |
src/devices/print.ts +64 −16
| @@ -11,7 +11,7 @@ import { getDevice, type Device } from "../core/devices.ts"; | ||
| 11 | 11 | import { dailyCap, logJob, printedToday, sentJob, type Outcome } from "../core/print_log.ts"; |
| 12 | 12 | import { RateLimiter } from "../core/ratelimit.ts"; |
| 13 | 13 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 14 | import { printDocument, printPdf, probeRasterSupport, readJob, type PrintJob } from "./ipp.ts"; | |
| 14 | import { printDocument, printPdf, probePrinter, probeRasterSupport, readJob, type PrintJob } from "./ipp.ts"; | |
| 15 | 15 | import { PWG_MAX_BYTES, readPwg, unsupportedBy, type PwgError } from "./pwg.ts"; |
| 16 | 16 | import { BODY_CAPS } from "../kernel/body_caps.ts"; |
| 17 | 17 | |
| @@ -22,12 +22,29 @@ export const PDF_MAX_PAGES = 20; | ||
| 22 | 22 | export const PRINTS_PER_MINUTE = 10; |
| 23 | 23 | export const PRINTS_AT_ONCE = 2; |
| 24 | 24 | |
| 25 | export type PrintError = "forbidden" | "rate_limited" | "daily_cap" | "not_pdf" | "too_large" | "too_many_pages" | "pages_unknown" | "printer_unreachable" | "printer_refused" | PwgError | "raster_resolution_refused" | "raster_type_refused" | "job_unknown"; | |
| 25 | /** The `sides` an app may ask for: one-sided is today's behaviour and the default. */ | |
| 26 | export const PRINT_SIDES = ["one-sided", "two-sided-long-edge"] as const; | |
| 27 | export type PrintSides = (typeof PRINT_SIDES)[number]; | |
| 28 | ||
| 29 | /** | |
| 30 | * `?sides=` as asked: absent is one-sided, one of PRINT_SIDES is itself, anything else is | |
| 31 | * undefined. | |
| 32 | */ | |
| 33 | export function sidesOf(raw: string | null): PrintSides | undefined { | |
| 34 | return raw === null ? "one-sided" : PRINT_SIDES.find((s) => s === raw); | |
| 35 | } | |
| 36 | ||
| 37 | /** Sheets of paper a job of `pages` uses: both sides of a sheet hold two pages. */ | |
| 38 | export const sheetsOf = (pages: number, sides: PrintSides): number => (sides === "one-sided" ? pages : Math.ceil(pages / 2)); | |
| 39 | ||
| 40 | export type PrintError = "invalid_sides" | "sides_unsupported" | "forbidden" | "rate_limited" | "daily_cap" | "not_pdf" | "too_large" | "too_many_pages" | "pages_unknown" | "printer_unreachable" | "printer_refused" | PwgError | "raster_resolution_refused" | "raster_type_refused" | "job_unknown"; | |
| 26 | 41 | |
| 27 | 42 | export const PRINT_WORDS: Record<PrintError, string> = { |
| 43 | invalid_sides: "sides must be one-sided (the default) or two-sided-long-edge.", | |
| 44 | sides_unsupported: "This printer does not say it prints on both sides; ask for one-sided.", | |
| 28 | 45 | forbidden: "This app is not granted that printer.", |
| 29 | 46 | rate_limited: "This app is printing too fast; wait a little.", |
| 30 | daily_cap: "This app has used its prints for today on this printer; it can print again tomorrow.", | |
| 47 | daily_cap: "This app has used its sheets for today on this printer; it can print again tomorrow.", | |
| 31 | 48 | not_pdf: "The body must be a PDF (application/pdf).", |
| 32 | 49 | too_large: "The document is over its size limit (10 MB for a PDF, 80 MB for PWG raster).", |
| 33 | 50 | too_many_pages: "The document is over 20 pages.", |
| @@ -84,12 +101,15 @@ export function checkRaster(raster: Uint8Array): Result<number, PrintError> { | ||
| 84 | 101 | } |
| 85 | 102 | |
| 86 | 103 | const perMinute = new RateLimiter({ perMinute: PRINTS_PER_MINUTE, adminPerMinute: PRINTS_PER_MINUTE, enabled: true }); |
| 87 | const inFlight = new Map<string, number>(); | |
| 104 | /** Per app: jobs in flight, and the sheets they reserved against the daily cap. */ | |
| 105 | const inFlight = new Map<string, { jobs: number; sheets: number }>(); | |
| 88 | 106 | |
| 89 | 107 | /** Who is printing and where, decided from the token and the grant alone. */ |
| 90 | 108 | export interface Permit { |
| 91 | 109 | readonly caller: AppRef; |
| 92 | 110 | readonly device: Device; |
| 111 | /** Sheets reserved against the daily cap: one until the document is read, then its own. */ | |
| 112 | sheets: number; | |
| 93 | 113 | /** Set once the reserved place is given back. */ |
| 94 | 114 | released: boolean; |
| 95 | 115 | } |
| @@ -98,8 +118,9 @@ export interface Permit { | ||
| 98 | 118 | export function releasePermit(permit: Permit): void { |
| 99 | 119 | if (permit.released) return; |
| 100 | 120 | permit.released = true; |
| 101 | const left = (inFlight.get(permit.caller.id) ?? 1) - 1; | |
| 102 | if (left <= 0) inFlight.delete(permit.caller.id); | |
| 121 | const held = inFlight.get(permit.caller.id) ?? { jobs: 1, sheets: permit.sheets }; | |
| 122 | const left = { jobs: held.jobs - 1, sheets: held.sheets - permit.sheets }; | |
| 123 | if (left.jobs <= 0) inFlight.delete(permit.caller.id); | |
| 103 | 124 | else inFlight.set(permit.caller.id, left); |
| 104 | 125 | } |
| 105 | 126 | |
| @@ -117,28 +138,53 @@ export function permitPrint(app: App, secret: string, deviceId: number): Result< | ||
| 117 | 138 | if (!caller || !Number.isSafeInteger(deviceId)) return err("forbidden"); |
| 118 | 139 | const device = getDevice(app.db, deviceId); |
| 119 | 140 | if (!device || device.kind !== "printer" || !isGranted(app.db, device.id, caller.id)) return err("forbidden"); |
| 120 | const held = inFlight.get(caller.id) ?? 0; | |
| 141 | const { jobs: held, sheets: reserved } = inFlight.get(caller.id) ?? { jobs: 0, sheets: 0 }; | |
| 121 | 142 | if (held >= PRINTS_AT_ONCE || !perMinute.take(caller.id, "ro").allowed) return err("rate_limited"); |
| 122 | // Jobs still in flight count, so parallel requests cannot slip past the last place. | |
| 123 | if (printedToday(app.db, device.id, caller.id) + held >= dailyCap(app.db, device.id, caller.id)) { | |
| 143 | // Sheets reserved by jobs still in flight count, so parallel requests cannot slip past the last | |
| 144 | // place. | |
| 145 | if (printedToday(app.db, device.id, caller.id) + reserved >= dailyCap(app.db, device.id, caller.id)) { | |
| 124 | 146 | logJob(app.db, { deviceId: device.id, appId: caller.id, pages: 0, outcome: "refused_cap" }); |
| 125 | 147 | return err("daily_cap"); |
| 126 | 148 | } |
| 127 | inFlight.set(caller.id, held + 1); | |
| 128 | return ok({ caller, device, released: false }); | |
| 149 | inFlight.set(caller.id, { jobs: held + 1, sheets: reserved + 1 }); | |
| 150 | return ok({ caller, device, sheets: 1, released: false }); | |
| 129 | 151 | } |
| 130 | 152 | |
| 131 | 153 | /** Print `pdf` for a permitted app; the document is checked before any byte leaves for the LAN. */ |
| 132 | export async function printFor(app: App, permit: Permit, pdf: Uint8Array, kind: "pdf" | "raster" = "pdf"): Promise<Result<PrintJob, PrintError>> { | |
| 154 | export async function printFor(app: App, permit: Permit, pdf: Uint8Array, kind: "pdf" | "raster" = "pdf", sides: PrintSides = "one-sided"): Promise<Result<PrintJob, PrintError>> { | |
| 133 | 155 | try { |
| 134 | 156 | const { caller, device } = permit; |
| 135 | 157 | const checked = kind === "raster" ? checkRaster(pdf) : checkPdf(pdf); |
| 136 | const log = (pages: number, outcome: Outcome, jobId: number | null = null): void => | |
| 137 | logJob(app.db, { deviceId: device.id, appId: caller.id, pages, outcome, jobId }); | |
| 158 | const log = (pages: number, outcome: Outcome, jobId: number | null = null, sheets = 1): void => | |
| 159 | logJob(app.db, { deviceId: device.id, appId: caller.id, pages, outcome, jobId, sheets }); | |
| 138 | 160 | if (!checked.ok) { |
| 139 | 161 | log(0, "refused_document"); |
| 140 | 162 | return checked; |
| 141 | 163 | } |
| 164 | // The sheets this document needs must fit the day beside what was printed and what other jobs | |
| 165 | // reserved: checked and reserved with no await between, so parallel jobs cannot pass together. | |
| 166 | const sheets = sheetsOf(checked.value, sides); | |
| 167 | const held = inFlight.get(caller.id) ?? { jobs: 0, sheets: 0 }; | |
| 168 | const others = held.sheets - permit.sheets; | |
| 169 | if (printedToday(app.db, device.id, caller.id) + others + sheets > dailyCap(app.db, device.id, caller.id)) { | |
| 170 | logJob(app.db, { deviceId: device.id, appId: caller.id, pages: 0, outcome: "refused_cap" }); | |
| 171 | return err("daily_cap"); | |
| 172 | } | |
| 173 | inFlight.set(caller.id, { jobs: held.jobs, sheets: others + sheets }); | |
| 174 | permit.sheets = sheets; | |
| 175 | if (sides !== "one-sided") { | |
| 176 | // Two-sided only when the printer says it can: otherwise nothing reaches it. | |
| 177 | const asked = await probePrinter(device.url); | |
| 178 | if (!asked.ok) { | |
| 179 | // Nothing was sent to print, so no sheet of the day is used. | |
| 180 | log(0, "failed", null, 0); | |
| 181 | return err(asked.error === "unreachable" ? "printer_unreachable" : "printer_refused"); | |
| 182 | } | |
| 183 | if (!asked.report.sides.includes(sides)) { | |
| 184 | log(0, "refused_document"); | |
| 185 | return err("sides_unsupported"); | |
| 186 | } | |
| 187 | } | |
| 142 | 188 | if (kind === "raster") { |
| 143 | 189 | // Only now does anything touch the LAN: ask what the printer takes of what the pages need. |
| 144 | 190 | const asked = await probeRasterSupport(device.url); |
| @@ -156,8 +202,10 @@ export async function printFor(app: App, permit: Permit, pdf: Uint8Array, kind: | ||
| 156 | 202 | } |
| 157 | 203 | } |
| 158 | 204 | const jobName = `${caller.slug} (forge)`; |
| 159 | const sent = kind === "raster" ? await printDocument(device.url, pdf, jobName, "image/pwg-raster") : await printPdf(device.url, pdf, jobName); | |
| 160 | log(checked.value, sent.ok ? "printed" : "failed", sent.ok ? sent.job.id : null); | |
| 205 | // The IPP attribute is sent only when asked: one-sided is the printer's own default. | |
| 206 | const askedSides = sides === "one-sided" ? undefined : sides; | |
| 207 | const sent = kind === "raster" ? await printDocument(device.url, pdf, jobName, "image/pwg-raster", undefined, askedSides) : await printPdf(device.url, pdf, jobName, undefined, askedSides); | |
| 208 | log(checked.value, sent.ok ? "printed" : "failed", sent.ok ? sent.job.id : null, sheets); | |
| 161 | 209 | releasePermit(permit); |
| 162 | 210 | if (sent.ok) { |
| 163 | 211 | app.bus.append({ kind: "device.printed", payload: { device: device.name, app: caller.slug, pages: checked.value } }); |
src/http/api/print.ts +9 −2
| @@ -4,7 +4,7 @@ | ||
| 4 | 4 | * PDF, or PWG raster when the content-type says `image/pwg-raster`. The route takes a device number and nothing that names an address. |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | import { PRINT_WORDS, PDF_MAX_BYTES, jobOutcome, permitPrint, printFor, releasePermit, withinOf, type PrintError } from "../../devices/print.ts"; | |
| 7 | import { PRINT_WORDS, PDF_MAX_BYTES, jobOutcome, permitPrint, printFor, releasePermit, sidesOf, withinOf, type PrintError } from "../../devices/print.ts"; | |
| 8 | 8 | import { PWG_MAX_BYTES } from "../../devices/pwg.ts"; |
| 9 | 9 | import type { Handler } from "../context.ts"; |
| 10 | 10 | import { readBounded } from "../limits.ts"; |
| @@ -13,6 +13,8 @@ import type { Router } from "../router.ts"; | ||
| 13 | 13 | import { tokenSecret } from "../token_header.ts"; |
| 14 | 14 | |
| 15 | 15 | const STATUS: Record<PrintError, number> = { |
| 16 | invalid_sides: 400, | |
| 17 | sides_unsupported: 422, | |
| 16 | 18 | forbidden: 403, |
| 17 | 19 | rate_limited: 429, |
| 18 | 20 | daily_cap: 429, |
| @@ -46,6 +48,11 @@ export function printRoutes(router: Router<Handler>): void { | ||
| 46 | 48 | const permit = permitPrint(ctx.app, secret, id); |
| 47 | 49 | if (!permit.ok) return printRefusal(permit.error); |
| 48 | 50 | // The permit holds a place until printFor, or here when the body never gets that far. |
| 51 | const sides = sidesOf(new URL(ctx.request.url).searchParams.get("sides")); | |
| 52 | if (sides === undefined) { | |
| 53 | releasePermit(permit.value); | |
| 54 | return printRefusal("invalid_sides"); | |
| 55 | } | |
| 49 | 56 | const raster = (ctx.request.headers.get("content-type") ?? "").split(";")[0]!.trim().toLowerCase() === "image/pwg-raster"; |
| 50 | 57 | const cap = raster ? PWG_MAX_BYTES : PDF_MAX_BYTES; |
| 51 | 58 | let pdf: Awaited<ReturnType<typeof readBounded>>; |
| @@ -59,7 +66,7 @@ export function printRoutes(router: Router<Handler>): void { | ||
| 59 | 66 | releasePermit(permit.value); |
| 60 | 67 | return apiError("too_large", 422, PRINT_WORDS.too_large); |
| 61 | 68 | } |
| 62 | const done = await printFor(ctx.app, permit.value, pdf, raster ? "raster" : "pdf"); | |
| 69 | const done = await printFor(ctx.app, permit.value, pdf, raster ? "raster" : "pdf", sides); | |
| 63 | 70 | if (!done.ok) return printRefusal(done.error); |
| 64 | 71 | return json({ job: { id: done.value.id, state: done.value.state, reasons: done.value.reasons } }); |
| 65 | 72 | }); |
src/tui/screens/devices.ts +2 −2
| @@ -106,14 +106,14 @@ export function devicesScreen(f: Session, ctx: Ctx): Screen { | ||
| 106 | 106 | const setLimit = (d: Device) => (line: string): string | undefined => { |
| 107 | 107 | const [slug = "", raw = "", extra] = line.trim().split(/\s+/); |
| 108 | 108 | const made = setCap(f.app, f.user, d.id, slug, /^\d{1,4}$/.test(raw) && extra === undefined ? Number(raw) : Number.NaN); |
| 109 | note = made.ok ? `${made.value} may print ${raw} a day on ${d.name}.` : DEVICE_ERRORS[made.error]; | |
| 109 | note = made.ok ? `${made.value} may print ${raw} sheets a day on ${d.name}.` : DEVICE_ERRORS[made.error]; | |
| 110 | 110 | if (made.ok) reload(); |
| 111 | 111 | return made.ok ? undefined : note; |
| 112 | 112 | }; |
| 113 | 113 | const askLimit = (d: Device): Screen => |
| 114 | 114 | promptScreen(ctx, screen, { |
| 115 | 115 | title: "Daily print limit", |
| 116 | label: `App slug, then prints a day (0 to ${CAP_MAX}) on ${d.name}`, | |
| 116 | label: `App slug, then sheets a day (0 to ${CAP_MAX}) on ${d.name}`, | |
| 117 | 117 | placeholder: grantLimits(f.db, d.id).map((l) => l.slug).join(" ") || "grant an app first", |
| 118 | 118 | onSubmit: setLimit(d), |
| 119 | 119 | }); |
src/web/settings/devices.ts +2 −2
| @@ -56,7 +56,7 @@ function access(ctx: Ctx, d: Device): Html { | ||
| 56 | 56 | <ul class="list">${limits.map( |
| 57 | 57 | (l) => html`<li>${row({ |
| 58 | 58 | title: html`<span class="mono">${l.slug}</span>`, |
| 59 | meta: html`<span class="small muted">${String(l.today)} of ${String(l.cap)} printed today</span>`, | |
| 59 | meta: html`<span class="small muted">${String(l.today)} of ${String(l.cap)} sheets printed today</span>`, | |
| 60 | 60 | trail: html`<form class="row-actions" method="post" action="/settings/devices/revoke"> |
| 61 | 61 | <input type="hidden" name="id" value="${String(d.id)}" /> |
| 62 | 62 | <input type="hidden" name="app" value="${l.slug}" /> |
| @@ -78,7 +78,7 @@ function access(ctx: Ctx, d: Device): Html { | ||
| 78 | 78 | <select class="input" name="app" aria-label="App to limit on ${d.name}"> |
| 79 | 79 | ${limits.map((l) => html`<option value="${l.slug}">${l.slug} (${String(l.cap)})</option>`)} |
| 80 | 80 | </select> |
| 81 | <input class="input mono" type="number" name="cap" min="0" max="${String(CAP_MAX)}" value="${String(limits[0]!.cap)}" aria-label="Prints a day" /> | |
| 81 | <input class="input mono" type="number" name="cap" min="0" max="${String(CAP_MAX)}" value="${String(limits[0]!.cap)}" aria-label="Sheets a day" /> | |
| 82 | 82 | ${btn("Set limit", { size: "sm", label: `Set the daily limit on ${d.name}` })} |
| 83 | 83 | </form> |
| 84 | 84 | </dd></div>`)} |
test/print.test.ts +135 −3
| @@ -7,7 +7,7 @@ import { createApp, type App } from "../src/app.ts"; | ||
| 7 | 7 | import { mintAppToken, revokeAppToken } from "../src/apps/token.ts"; |
| 8 | 8 | import { grantDevice, isGranted } from "../src/core/device_grants.ts"; |
| 9 | 9 | import { declareDevice } from "../src/core/devices.ts"; |
| 10 | import { logJob } from "../src/core/print_log.ts"; | |
| 10 | import { logJob, setDailyCap } from "../src/core/print_log.ts"; | |
| 11 | 11 | import { createToken } from "../src/core/tokens.ts"; |
| 12 | 12 | import { createSession } from "../src/core/sessions.ts"; |
| 13 | 13 | import { getUserByName } from "../src/core/users.ts"; |
| @@ -33,11 +33,23 @@ const pdf = (pages: number): Uint8Array => | ||
| 33 | 33 | let received: Uint8Array[] = []; |
| 34 | 34 | let reads: Uint8Array[] = []; |
| 35 | 35 | let jobState = 9; |
| 36 | let sidesListed: string[] = []; | |
| 36 | 37 | const printer = Bun.serve({ |
| 37 | 38 | port: 0, |
| 38 | 39 | hostname: "127.0.0.1", |
| 39 | 40 | async fetch(req) { |
| 40 | 41 | const body = new Uint8Array(await req.arrayBuffer()); |
| 42 | if (body[3] === 0x0b) { | |
| 43 | return new Response( | |
| 44 | Uint8Array.from([ | |
| 45 | 1, 1, 0, 0, 0, 0, 0, 1, | |
| 46 | 0x01, ...attr(0x47, "attributes-charset", "utf-8"), | |
| 47 | 0x04, ...sidesListed.flatMap((v, i) => attr(0x44, i === 0 ? "sides-supported" : "", v)), | |
| 48 | 0x03, | |
| 49 | ]), | |
| 50 | { headers: { "content-type": "application/ipp" } }, | |
| 51 | ); | |
| 52 | } | |
| 41 | 53 | if (body[3] === 9) { |
| 42 | 54 | reads.push(body); |
| 43 | 55 | return new Response( |
| @@ -146,6 +158,8 @@ test("a stopped app's token no longer prints", async () => { | ||
| 146 | 158 | test("a non-PDF, an oversize or a long document is 422 and is not sent", async () => { |
| 147 | 159 | received = []; |
| 148 | 160 | const path = `/api/v1/devices/${deviceId}/print`; |
| 161 | // The cap counts sheets now: a 20-page one-sided job needs room for 20. | |
| 162 | setDailyCap(app.db, deviceId, "labels", 100); | |
| 149 | 163 | expect((await print(path, granted, "just text")).status).toBe(422); |
| 150 | 164 | expect((await print(path, granted, enc.encode("%PDF-1.4 truncated, no end"))).status).toBe(422); |
| 151 | 165 | expect((await print(path, granted, pdf(21))).status).toBe(422); |
| @@ -256,7 +270,7 @@ test("the daily cap refuses past the limit, says why, and resets the next day", | ||
| 256 | 270 | const t = mintAppToken(app.db, owner.id, "capped", "production").token; |
| 257 | 271 | const path = `/api/v1/devices/${otherId}/print`; |
| 258 | 272 | expect((await print(path, t, pdf(1))).status).toBe(200); |
| 259 | expect((await print(path, t, pdf(3))).status).toBe(200); | |
| 273 | expect((await print(path, t, pdf(1))).status).toBe(200); | |
| 260 | 274 | const res = await print(path, t, pdf(1)); |
| 261 | 275 | expect(res.status).toBe(429); |
| 262 | 276 | expect(((await res.json()) as { error: string }).error).toBe("daily_cap"); |
| @@ -270,7 +284,7 @@ test("the daily cap refuses past the limit, says why, and resets the next day", | ||
| 270 | 284 | } |
| 271 | 285 | // One row per job: two printed, one refused by the cap. |
| 272 | 286 | const rows = app.db.query<{ pages: number; outcome: string }, []>("SELECT pages, outcome FROM print_job WHERE app_id = 'a4' ORDER BY id").all(); |
| 273 | expect(rows).toEqual([{ pages: 1, outcome: "printed" }, { pages: 3, outcome: "printed" }, { pages: 0, outcome: "refused_cap" }, { pages: 1, outcome: "printed" }]); | |
| 287 | expect(rows).toEqual([{ pages: 1, outcome: "printed" }, { pages: 1, outcome: "printed" }, { pages: 0, outcome: "refused_cap" }, { pages: 1, outcome: "printed" }]); | |
| 274 | 288 | }); |
| 275 | 289 | |
| 276 | 290 | test("the owner sets the limit on the device page, which lists the last jobs", async () => { |
| @@ -479,3 +493,121 @@ test("a printer that never answers is 502 printer_unreachable; one that refuses | ||
| 479 | 493 | refusing.stop(true); |
| 480 | 494 | } |
| 481 | 495 | }); |
| 496 | ||
| 497 | // --- sides (#18190) --- | |
| 498 | ||
| 499 | test("encodePrintJob sends the sides attribute only when asked", async () => { | |
| 500 | const { encodePrintJob } = await import("../src/devices/ipp.ts"); | |
| 501 | const doc = pdf(1); | |
| 502 | const text = (b: Uint8Array) => Buffer.from(b).toString("latin1"); | |
| 503 | const two = encodePrintJob("ipp://x/ipp/print", doc, "job", 1, "application/pdf", "two-sided-long-edge"); | |
| 504 | expect(text(two)).toContain("sides"); | |
| 505 | expect(text(two)).toContain("two-sided-long-edge"); | |
| 506 | // job attribute group (0x02) holds a keyword (0x44) named sides, before the end tag | |
| 507 | const head = Buffer.from(two.subarray(0, two.length - doc.length)); | |
| 508 | const at = head.indexOf(Buffer.from("sides")); | |
| 509 | expect(head[at - 3]).toBe(0x44); | |
| 510 | expect(head.lastIndexOf(0x02, at)).toBeGreaterThan(head.indexOf(Buffer.from("document-format"))); | |
| 511 | expect(head[head.length - 1]).toBe(0x03); | |
| 512 | const none = encodePrintJob("ipp://x/ipp/print", doc, "job"); | |
| 513 | expect(text(none)).not.toContain("two-sided"); | |
| 514 | expect(Buffer.from(none.subarray(0, none.length - doc.length)).includes(Buffer.from("sides"))).toBe(false); | |
| 515 | }); | |
| 516 | ||
| 517 | test("reportOf reads sides-supported", async () => { | |
| 518 | const { reportOf } = await import("../src/devices/ipp.ts"); | |
| 519 | expect(reportOf(new Map([["sides-supported", ["one-sided", "two-sided-long-edge"]]])).sides).toEqual(["one-sided", "two-sided-long-edge"]); | |
| 520 | expect(reportOf(new Map()).sides).toEqual([]); | |
| 521 | }); | |
| 522 | ||
| 523 | test("sheetsOf: two pages are one sheet two-sided and two sheets one-sided", async () => { | |
| 524 | const { sheetsOf, sidesOf } = await import("../src/devices/print.ts"); | |
| 525 | expect(sheetsOf(2, "two-sided-long-edge")).toBe(1); | |
| 526 | expect(sheetsOf(3, "two-sided-long-edge")).toBe(2); | |
| 527 | expect(sheetsOf(2, "one-sided")).toBe(2); | |
| 528 | expect(sidesOf(null)).toBe("one-sided"); | |
| 529 | expect(sidesOf("two-sided-short-edge")).toBeUndefined(); | |
| 530 | }); | |
| 531 | ||
| 532 | test("a bad sides is 400 naming the allowed values, before the body is read", async () => { | |
| 533 | received = []; | |
| 534 | const res = await print(`/api/v1/devices/${deviceId}/print?sides=duplex`, granted, pdf(2)); | |
| 535 | expect(res.status).toBe(400); | |
| 536 | const body = (await res.json()) as { error: string; message?: string }; | |
| 537 | expect(body.error).toBe("invalid_sides"); | |
| 538 | expect(JSON.stringify(body)).toContain("one-sided"); | |
| 539 | expect(JSON.stringify(body)).toContain("two-sided-long-edge"); | |
| 540 | expect(received.length).toBe(0); | |
| 541 | }); | |
| 542 | ||
| 543 | test("two-sided is refused when the printer does not list it, and nothing is printed", async () => { | |
| 544 | received = []; | |
| 545 | sidesListed = []; | |
| 546 | const res = await print(`/api/v1/devices/${deviceId}/print?sides=two-sided-long-edge`, granted, pdf(2)); | |
| 547 | expect(res.status).toBe(422); | |
| 548 | expect(((await res.json()) as { error: string }).error).toBe("sides_unsupported"); | |
| 549 | expect(received.filter((b) => b[3] === 2).length).toBe(0); | |
| 550 | }); | |
| 551 | ||
| 552 | test("two-sided is sent when the printer lists it, and one-sided sends no sides", async () => { | |
| 553 | received = []; | |
| 554 | sidesListed = ["one-sided", "two-sided-long-edge"]; | |
| 555 | const two = await print(`/api/v1/devices/${deviceId}/print?sides=two-sided-long-edge`, granted, pdf(2)); | |
| 556 | expect(two.status).toBe(200); | |
| 557 | const jobs = received.filter((b) => b[3] === 2); | |
| 558 | expect(jobs.length).toBe(1); | |
| 559 | expect(Buffer.from(jobs[0]!).toString("latin1")).toContain("two-sided-long-edge"); | |
| 560 | received = []; | |
| 561 | const one = await print(`/api/v1/devices/${deviceId}/print`, granted, pdf(2)); | |
| 562 | expect(one.status).toBe(200); | |
| 563 | expect(received.length).toBe(1); | |
| 564 | expect(Buffer.from(received[0]!).toString("latin1")).not.toContain("sides"); | |
| 565 | }); | |
| 566 | ||
| 567 | test("the daily cap counts sheets: two pages two-sided are one, one-sided are two, and no job passes it", async () => { | |
| 568 | const { printedToday } = await import("../src/core/print_log.ts"); | |
| 569 | const owner = getUserByName(app.db, "alice")!; | |
| 570 | addApp("a7", "sheets"); | |
| 571 | grantDevice(app.db, otherId, "sheets", owner.id); | |
| 572 | setDailyCap(app.db, otherId, "sheets", 3); | |
| 573 | sidesListed = ["one-sided", "two-sided-long-edge"]; | |
| 574 | const t = mintAppToken(app.db, owner.id, "sheets", "production").token; | |
| 575 | const path = `/api/v1/devices/${otherId}/print`; | |
| 576 | expect((await print(`${path}?sides=two-sided-long-edge`, t, pdf(2))).status).toBe(200); | |
| 577 | expect(printedToday(app.db, otherId, "a7")).toBe(1); | |
| 578 | expect((await print(path, t, pdf(2))).status).toBe(200); | |
| 579 | expect(printedToday(app.db, otherId, "a7")).toBe(3); | |
| 580 | received = []; | |
| 581 | const over = await print(`${path}?sides=two-sided-long-edge`, t, pdf(1)); | |
| 582 | expect(over.status).toBe(429); | |
| 583 | setDailyCap(app.db, otherId, "sheets", 4); | |
| 584 | // One sheet left: a three-page one-sided job needs three and is refused unsent. | |
| 585 | const big = await print(path, t, pdf(3)); | |
| 586 | expect(big.status).toBe(429); | |
| 587 | expect(((await big.json()) as { error: string }).error).toBe("daily_cap"); | |
| 588 | expect(received.length).toBe(0); | |
| 589 | expect(printedToday(app.db, otherId, "a7")).toBe(3); | |
| 590 | }); | |
| 591 | ||
| 592 | test("the OpenAPI entry and the route say the same words", async () => { | |
| 593 | const { route } = await import("../src/contract/openapi/routes/postApiV1DevicesIdPrint.ts"); | |
| 594 | const { PRINT_WORDS } = await import("../src/devices/print.ts"); | |
| 595 | expect(route.refusals!["invalid_sides"]).toContain(PRINT_WORDS.invalid_sides); | |
| 596 | expect(route.refusals!["sides_unsupported"]).toContain(PRINT_WORDS.sides_unsupported); | |
| 597 | expect(route.refusals!["daily_cap"]).toContain(PRINT_WORDS.daily_cap); | |
| 598 | }); | |
| 599 | ||
| 600 | test("a two-sided job whose sides probe fails uses no sheet of the day", async () => { | |
| 601 | const { printedToday } = await import("../src/core/print_log.ts"); | |
| 602 | const refusing = Bun.serve({ port: 0, hostname: "127.0.0.1", fetch: () => new Response("no", { status: 500 }) }); | |
| 603 | try { | |
| 604 | const r = readerOn("probefail", `ipp://127.0.0.1:${refusing.port}/ipp/print`); | |
| 605 | const id = r.path.split("/")[4]!; | |
| 606 | const before = printedToday(app.db, Number(id), "id-probefail"); | |
| 607 | const res = await print(`/api/v1/devices/${id}/print?sides=two-sided-long-edge`, r.token, pdf(2)); | |
| 608 | expect(res.status).toBe(502); | |
| 609 | expect(printedToday(app.db, Number(id), "id-probefail")).toBe(before); | |
| 610 | } finally { | |
| 611 | refusing.stop(true); | |
| 612 | } | |
| 613 | }); |
test/print_raster.test.ts +3
| @@ -9,6 +9,7 @@ import { join } from "node:path"; | ||
| 9 | 9 | import { createApp, type App } from "../src/app.ts"; |
| 10 | 10 | import { mintAppToken } from "../src/apps/token.ts"; |
| 11 | 11 | import { grantDevice } from "../src/core/device_grants.ts"; |
| 12 | import { setDailyCap } from "../src/core/print_log.ts"; | |
| 12 | 13 | import { declareDevice } from "../src/core/devices.ts"; |
| 13 | 14 | import { getUserByName } from "../src/core/users.ts"; |
| 14 | 15 | import { readPwg } from "../src/devices/pwg.ts"; |
| @@ -89,6 +90,8 @@ beforeAll(async () => { | ||
| 89 | 90 | if (!d.ok) throw new Error("declare"); |
| 90 | 91 | deviceId = d.value.id; |
| 91 | 92 | expect(grantDevice(app.db, deviceId, "paper", owner.id).ok).toBe(true); |
| 93 | // The cap counts sheets: leave room for a 20-page one-sided job. | |
| 94 | setDailyCap(app.db, deviceId, "paper", 100); | |
| 92 | 95 | granted = mintAppToken(app.db, owner.id, "paper", "production").token; |
| 93 | 96 | }); |
| 94 | 97 | afterAll(async () => { |
new above
offline
This
daily_capsentence differs fromPRINT_WORDS.daily_cap("This app has used its sheets for today on this printer; it can print again tomorrow."). A person and an agent should get the same words. Use the route's sentence here, and extend the words test to coverdaily_cap.