|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { applyFontDelivery, applyGraphicDelivery, fontDelivery, graphicDelivery, graphicJobShips } from "./resourceDelivery"; |
| 3 | +import type { ImageProps } from "../registry/image"; |
| 4 | + |
| 5 | +const GFA = "^GFA,4,4,1,00FFFF00"; |
| 6 | +const image = (extra: Partial<ImageProps["storedAs"]> | null = {}): ImageProps => ({ |
| 7 | + imageId: "", |
| 8 | + widthDots: 8, |
| 9 | + heightDots: 4, |
| 10 | + threshold: 128, |
| 11 | + _gfaCache: GFA, |
| 12 | + ...(extra === null ? {} : { storedAs: { device: "R", name: "LOGO", ...extra } }), |
| 13 | +}); |
| 14 | +const entry = { path: "R:LOGO.GRF", gfa: GFA }; |
| 15 | +const recallOnly = { device: "R", name: "LOGO", embedInZpl: false }; |
| 16 | + |
| 17 | +describe("fontDelivery", () => { |
| 18 | + it("reads job while the design ships the font, even with a profile entry beside it", () => { |
| 19 | + expect(fontDelivery({ embedInZpl: true }, "E:A.TTF", [{ path: "E:A.TTF" }])).toBe("job"); |
| 20 | + expect(fontDelivery({ embedInZpl: true }, "E:A.TTF", undefined)).toBe("job"); |
| 21 | + }); |
| 22 | + |
| 23 | + it("reads setup from the profile entry under any spelling, else printer", () => { |
| 24 | + expect(fontDelivery(undefined, "E:A.TTF", [{ path: "e:a.ttf" }])).toBe("setup"); |
| 25 | + expect(fontDelivery({ embedInZpl: undefined }, "E:A.TTF", undefined)).toBe("printer"); |
| 26 | + }); |
| 27 | + |
| 28 | + it("keeps the profile entry when the job takes over, and drops it only for printer", () => { |
| 29 | + const setup = [{ path: "E:A.TTF" }]; |
| 30 | + expect(applyFontDelivery("job", undefined, "E:A.TTF", setup)).toEqual({ patch: { embedInZpl: true }, setupFonts: setup }); |
| 31 | + expect(applyFontDelivery("setup", { embedInZpl: true }, "E:A.TTF", undefined)).toEqual({ patch: { embedInZpl: undefined }, setupFonts: [{ path: "E:A.TTF" }] }); |
| 32 | + expect(applyFontDelivery("printer", undefined, "E:A.TTF", setup)).toEqual({ patch: null, setupFonts: undefined }); |
| 33 | + }); |
| 34 | + |
| 35 | + it("writes nothing the design or profile already holds", () => { |
| 36 | + const setup = [{ path: "E:A.TTF" }]; |
| 37 | + expect(applyFontDelivery("job", { embedInZpl: true }, "E:A.TTF", setup)).toEqual({ patch: null, setupFonts: setup }); |
| 38 | + expect(applyFontDelivery("setup", undefined, "E:A.TTF", setup).setupFonts).toBe(setup); |
| 39 | + const other = [{ path: "E:B.TTF" }]; |
| 40 | + expect(applyFontDelivery("printer", undefined, "E:A.TTF", other)).toEqual({ patch: null, setupFonts: other }); |
| 41 | + }); |
| 42 | +}); |
| 43 | + |
| 44 | +describe("graphicDelivery", () => { |
| 45 | + it("has no way to choose for an inline ^GF, and reads job while the design ships the bytes", () => { |
| 46 | + expect(graphicDelivery(image(null), [entry])).toBeUndefined(); |
| 47 | + expect(graphicDelivery(image(), [entry])).toBe("job"); |
| 48 | + expect(graphicDelivery(image({ embedInZpl: false }), [entry])).toBe("setup"); |
| 49 | + expect(graphicDelivery(image({ embedInZpl: false }), undefined)).toBe("printer"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("keeps a recall spelled with another extension in the choice, the entry named as the upload is", () => { |
| 53 | + expect(graphicDelivery(image({ embedInZpl: false, ext: "PNG" }), undefined)).toBe("printer"); |
| 54 | + expect(graphicDelivery(image({ embedInZpl: false, ext: "PNG" }), [entry])).toBe("setup"); |
| 55 | + const provided = applyGraphicDelivery("setup", image({ embedInZpl: false, ext: "PNG" }), [entry]); |
| 56 | + expect(provided).toEqual({ patch: { storedAs: recallOnly }, setupGraphics: [entry] }); |
| 57 | + const kept = applyGraphicDelivery("printer", image({ embedInZpl: false, ext: "PNG" }), undefined); |
| 58 | + expect(kept).toEqual({ patch: null, setupGraphics: undefined }); |
| 59 | + }); |
| 60 | + |
| 61 | + it("encodes the profile entry once for setup, keeps an existing one, and drops it only for printer", () => { |
| 62 | + const fresh = applyGraphicDelivery("setup", image(), undefined); |
| 63 | + expect(fresh).toEqual({ patch: { storedAs: recallOnly, _gfaCache: GFA }, setupGraphics: [entry] }); |
| 64 | + const stale = [{ path: "R:LOGO.GRF", gfa: "^GFA,4,4,1,FFFFFFFF" }]; |
| 65 | + expect(applyGraphicDelivery("setup", image({ embedInZpl: true }), stale)).toEqual({ patch: { storedAs: recallOnly }, setupGraphics: stale }); |
| 66 | + expect(applyGraphicDelivery("job", image({ embedInZpl: false }), [entry])).toEqual({ patch: { storedAs: { device: "R", name: "LOGO", embedInZpl: true } }, setupGraphics: [entry] }); |
| 67 | + expect(applyGraphicDelivery("printer", image({ embedInZpl: false }), [entry])).toEqual({ patch: null, setupGraphics: undefined }); |
| 68 | + }); |
| 69 | + |
| 70 | + it("writes nothing the design or profile already holds", () => { |
| 71 | + expect(applyGraphicDelivery("job", image(), [entry])).toEqual({ patch: null, setupGraphics: [entry] }); |
| 72 | + const kept = applyGraphicDelivery("setup", image({ embedInZpl: false }), [entry]); |
| 73 | + expect(kept).toEqual({ patch: null, setupGraphics: [entry] }); |
| 74 | + const others = [{ path: "R:OTHER.GRF", gfa: GFA }]; |
| 75 | + expect(applyGraphicDelivery("printer", image({ embedInZpl: false }), others)).toEqual({ patch: null, setupGraphics: others }); |
| 76 | + }); |
| 77 | + |
| 78 | + it("knows when a job would ship nothing, whatever the flag says", () => { |
| 79 | + expect(graphicJobShips(image())).toBe(true); |
| 80 | + expect(graphicJobShips(image({ embedInZpl: false }))).toBe(true); |
| 81 | + expect(graphicJobShips({ ...image(), _gfaCache: undefined })).toBe(false); |
| 82 | + expect(graphicJobShips({ ...image(), _gfaCache: "^GFA,4,4,1," })).toBe(false); |
| 83 | + expect(graphicJobShips(image(null))).toBe(false); |
| 84 | + }); |
| 85 | + |
| 86 | + it("refuses setup for bytes the script cannot carry, and does nothing without a printer name", () => { |
| 87 | + expect(applyGraphicDelivery("setup", { ...image(), _gfaCache: "^GFA,4,4,1," }, undefined)).toBeUndefined(); |
| 88 | + expect(applyGraphicDelivery("setup", image(null), undefined)).toBeUndefined(); |
| 89 | + const huge = `^GFA,600000,600000,1,${"F".repeat(1_200_000)}`; |
| 90 | + expect(applyGraphicDelivery("setup", { ...image(), _gfaCache: huge }, undefined)).toEqual({ refused: "tooLarge" }); |
| 91 | + }); |
| 92 | +}); |
0 commit comments