From 597a1cb7cd73b75a657d640f85d621c27b92a429 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Tue, 27 Jan 2026 11:57:56 +0100 Subject: [PATCH 1/6] tests, ojs_define - Add test to reproduce #13958 - Add a new verifyOjsDefine() helper with callback - Add a smoke test to check that ojs_define is working properly with regards to numbers - Add this test for all engines that support ojs_define --- .../Manifest.toml | 86 +++++++++++++++++++ .../ojs-define-numeric-precision/Project.toml | 2 + .../with-julia.qmd | 16 ++++ .../with-jupyter.qmd | 15 ++++ .../with-knitr.qmd | 13 +++ tests/smoke/ojs/ojs-define-precision.test.ts | 39 +++++++++ tests/verify.ts | 31 ++++++- 7 files changed, 199 insertions(+), 3 deletions(-) create mode 100644 tests/docs/ojs/ojs-define-numeric-precision/Manifest.toml create mode 100644 tests/docs/ojs/ojs-define-numeric-precision/Project.toml create mode 100644 tests/docs/ojs/ojs-define-numeric-precision/with-julia.qmd create mode 100644 tests/docs/ojs/ojs-define-numeric-precision/with-jupyter.qmd create mode 100644 tests/docs/ojs/ojs-define-numeric-precision/with-knitr.qmd create mode 100644 tests/smoke/ojs/ojs-define-precision.test.ts diff --git a/tests/docs/ojs/ojs-define-numeric-precision/Manifest.toml b/tests/docs/ojs/ojs-define-numeric-precision/Manifest.toml new file mode 100644 index 00000000000..c27231abbe6 --- /dev/null +++ b/tests/docs/ojs/ojs-define-numeric-precision/Manifest.toml @@ -0,0 +1,86 @@ +# This file is machine-generated - editing it directly is not advised + +julia_version = "1.11.7" +manifest_format = "2.0" +project_hash = "394674f2e1dbc0990c888c3ca4df111f6d9b2796" + +[[deps.Dates]] +deps = ["Printf"] +uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" +version = "1.11.0" + +[[deps.JSON]] +deps = ["Dates", "Logging", "Parsers", "PrecompileTools", "StructUtils", "UUIDs", "Unicode"] +git-tree-sha1 = "b3ad4a0255688dcb895a52fafbaae3023b588a90" +uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" +version = "1.4.0" + + [deps.JSON.extensions] + JSONArrowExt = ["ArrowTypes"] + + [deps.JSON.weakdeps] + ArrowTypes = "31f734f8-188a-4ce0-8406-c8a06bd891cd" + +[[deps.Logging]] +uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" +version = "1.11.0" + +[[deps.Parsers]] +deps = ["Dates", "PrecompileTools", "UUIDs"] +git-tree-sha1 = "7d2f8f21da5db6a806faf7b9b292296da42b2810" +uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0" +version = "2.8.3" + +[[deps.PrecompileTools]] +deps = ["Preferences"] +git-tree-sha1 = "5aa36f7049a63a1528fe8f7c3f2113413ffd4e1f" +uuid = "aea7be01-6a6a-4083-8856-8a6e6704d82a" +version = "1.2.1" + +[[deps.Preferences]] +deps = ["TOML"] +git-tree-sha1 = "522f093a29b31a93e34eaea17ba055d850edea28" +uuid = "21216c6a-2e73-6563-6e65-726566657250" +version = "1.5.1" + +[[deps.Printf]] +deps = ["Unicode"] +uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" +version = "1.11.0" + +[[deps.Random]] +deps = ["SHA"] +uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +version = "1.11.0" + +[[deps.SHA]] +uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" +version = "0.7.0" + +[[deps.StructUtils]] +deps = ["Dates", "UUIDs"] +git-tree-sha1 = "9297459be9e338e546f5c4bedb59b3b5674da7f1" +uuid = "ec057cc2-7a8d-4b58-b3b3-92acb9f63b42" +version = "2.6.2" + + [deps.StructUtils.extensions] + StructUtilsMeasurementsExt = ["Measurements"] + StructUtilsTablesExt = ["Tables"] + + [deps.StructUtils.weakdeps] + Measurements = "eff96d63-e80a-5855-80a2-b1b0885c5ab7" + Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" + +[[deps.TOML]] +deps = ["Dates"] +uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" +version = "1.0.3" + +[[deps.UUIDs]] +deps = ["Random", "SHA"] +uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" +version = "1.11.0" + +[[deps.Unicode]] +uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" +version = "1.11.0" diff --git a/tests/docs/ojs/ojs-define-numeric-precision/Project.toml b/tests/docs/ojs/ojs-define-numeric-precision/Project.toml new file mode 100644 index 00000000000..723a4c62b57 --- /dev/null +++ b/tests/docs/ojs/ojs-define-numeric-precision/Project.toml @@ -0,0 +1,2 @@ +[deps] +JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" diff --git a/tests/docs/ojs/ojs-define-numeric-precision/with-julia.qmd b/tests/docs/ojs/ojs-define-numeric-precision/with-julia.qmd new file mode 100644 index 00000000000..371295006cb --- /dev/null +++ b/tests/docs/ojs/ojs-define-numeric-precision/with-julia.qmd @@ -0,0 +1,16 @@ +--- +title: "ojs_define numeric precision test" +format: html +engine: julia +execute: + daemon: false +--- + +```{julia} +using JSON +ojs_define(num=0.00008604168504168504) +``` + +```{ojs} +num +``` diff --git a/tests/docs/ojs/ojs-define-numeric-precision/with-jupyter.qmd b/tests/docs/ojs/ojs-define-numeric-precision/with-jupyter.qmd new file mode 100644 index 00000000000..a521dd36cc0 --- /dev/null +++ b/tests/docs/ojs/ojs-define-numeric-precision/with-jupyter.qmd @@ -0,0 +1,15 @@ +--- +title: "ojs_define numeric precision test" +format: html +engine: jupyter +execute: + daemon: false +--- + +```{python} +ojs_define(num = 0.00008604168504168504) +``` + +```{ojs} +num +``` diff --git a/tests/docs/ojs/ojs-define-numeric-precision/with-knitr.qmd b/tests/docs/ojs/ojs-define-numeric-precision/with-knitr.qmd new file mode 100644 index 00000000000..c3d0b23132f --- /dev/null +++ b/tests/docs/ojs/ojs-define-numeric-precision/with-knitr.qmd @@ -0,0 +1,13 @@ +--- +title: "ojs_define numeric precision test" +format: html +engine: knitr +--- + +```{r} +ojs_define(num = 0.00008604168504168504) +``` + +```{ojs} +num +``` diff --git a/tests/smoke/ojs/ojs-define-precision.test.ts b/tests/smoke/ojs/ojs-define-precision.test.ts new file mode 100644 index 00000000000..2a66132db6b --- /dev/null +++ b/tests/smoke/ojs/ojs-define-precision.test.ts @@ -0,0 +1,39 @@ +import { docs, outputForInput } from "../../utils.ts"; +import { assert } from "testing/asserts"; +import { testRender } from "../render/render.ts"; +import { verifyOjsDefine } from "../../verify.ts"; + +// Test for #13958: ojs_define() rounds numbers incorrectly +// Test across all three execution engines: knitr, jupyter, julia +const engines = ["knitr", "jupyter", "julia"]; + +for (const engine of engines) { + const input = docs(`ojs/ojs-define-numeric-precision/with-${engine}.qmd`); + const output = outputForInput(input, "html"); + + testRender( + input, + "html", + false, + [ + verifyOjsDefine(async (contents) => { + const numEntry = contents.find((item) => item.name === "num"); + assert(numEntry, "Should find 'num' variable in ojs-define data"); + assert( + typeof numEntry.value === "number", + "ojs-define value should be a number" + ); + + // Validate numeric precision (the actual bug test) + const expected = 0.00008604168504168504; + const tolerance = 1e-15; // Machine epsilon tolerance + + const diff = Math.abs(numEntry.value - expected); + assert( + diff < tolerance, + `ojs-define value should preserve full precision. Expected ${expected}, got ${numEntry.value}, diff ${diff}` + ); + }, `ojs_define preserves full numeric precision (${engine})`)(output.outputPath), + ], + ); +} diff --git a/tests/verify.ts b/tests/verify.ts index 67164e3ac82..d4ae29ada7b 100644 --- a/tests/verify.ts +++ b/tests/verify.ts @@ -420,15 +420,15 @@ export const ensureHtmlElementCount = ( verify: async (_output: ExecuteOutput[]) => { const htmlInput = await Deno.readTextFile(file); const doc = new DOMParser().parseFromString(htmlInput, "text/html")!; - + // Convert single values to arrays for unified processing const selectorsArray = Array.isArray(options.selectors) ? options.selectors : [options.selectors]; const countsArray = Array.isArray(options.counts) ? options.counts : [options.counts]; - + if (selectorsArray.length !== countsArray.length) { throw new Error("Selectors and counts arrays must have the same length"); } - + selectorsArray.forEach((selector, index) => { const expectedCount = countsArray[index]; const elements = doc.querySelectorAll(selector); @@ -441,6 +441,31 @@ export const ensureHtmlElementCount = ( }; }; +export const verifyOjsDefine = ( + callback: (contents: Array<{name: string, value: any}>) => Promise, + name?: string, +): (file: string) => Verify => { + return (file: string) => ({ + name: name ?? "Inspecting OJS Define", + verify: async (_output: ExecuteOutput[]) => { + const htmlContent = await Deno.readTextFile(file); + const doc = new DOMParser().parseFromString(htmlContent, "text/html")!; + const scriptElement = doc.querySelector('script[type="ojs-define"]'); + assert( + scriptElement, + "Should find ojs-define script element in rendered HTML" + ); + const jsonContent = scriptElement.textContent.trim(); + const ojsData = JSON.parse(jsonContent); + assert( + ojsData.contents && Array.isArray(ojsData.contents), + "ojs-define should have contents array" + ); + await callback(ojsData.contents); + }, + }); +}; + const printColoredDiff = (diff: string) => { for (const line of diff.split("\n")) { if (line.startsWith("+") && !line.startsWith("+++")) { From 73ae243b25baae4131dfb33b568dd0fd018b8afd Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Tue, 27 Jan 2026 12:04:06 +0100 Subject: [PATCH 2/6] llm-docs - add more info to prioritize using testRender() --- llm-docs/testing-patterns.md | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/llm-docs/testing-patterns.md b/llm-docs/testing-patterns.md index ecaa4abdea7..d99234dc652 100644 --- a/llm-docs/testing-patterns.md +++ b/llm-docs/testing-patterns.md @@ -11,6 +11,45 @@ Quarto uses Deno for testing with custom verification helpers located in: ## Common Test Patterns +### Simple Render Tests + +For testing single document rendering with automatic cleanup: + +```typescript +import { docs } from "../../utils.ts"; +import { testRender } from "./render.ts"; + +// Simplest form - just render and verify output created +testRender(docs("test-plain.md"), "html", false); +``` + +**With additional verifiers:** + +```typescript +import { docs, outputForInput } from "../../utils.ts"; +import { testRender } from "./render.ts"; +import { ensureHtmlElements } from "../../verify.ts"; + +const input = docs("minimal.qmd"); +const output = outputForInput(input, "html"); + +testRender(input, "html", true, [ + ensureHtmlElements(output.outputPath, [], [ + "script#quarto-html-after-body", + ]), +]); +``` + +**Key points:** +- `testRender()` automatically handles output verification and cleanup +- Respects `QUARTO_TEST_KEEP_OUTPUTS` env var for debugging +- Set `noSupporting` parameter based on expected output: + - `true` - For truly self-contained HTML (no `_files/` directory, inline everything) + - `false` - For HTML with supporting files directory (OJS runtime, widget dependencies, plots, etc.) + - Most HTML outputs should use `false` (only use `true` for formats like `html` with `self-contained: true`) +- Pass additional verifiers in the array parameter (optional) +- Cleanup happens automatically via `cleanoutput()` in teardown + ### Project Rendering Tests For testing project rendering (especially website projects): @@ -226,6 +265,11 @@ testQuartoCmd(...); ## Examples from Codebase +### Simple Render Test +See `tests/smoke/render/render-plain.test.ts` for the simplest render tests (no additional verifiers). + +See `tests/smoke/render/render-minimal.test.ts` for render test with custom HTML element verification. + ### Project Ignore Test See `tests/smoke/project/project-ignore-dirs.test.ts` for testing directory exclusion patterns. From d969abf0a38452e305b925af7c504cd6dfd0153a Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Tue, 27 Jan 2026 12:07:58 +0100 Subject: [PATCH 3/6] knitr, ojs_define - use max precision when parsing JSON like it is the case for jupyter and julia engine --- src/resources/rmd/ojs_static.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/resources/rmd/ojs_static.R b/src/resources/rmd/ojs_static.R index 14f1d65137a..716f5efe398 100644 --- a/src/resources/rmd/ojs_static.R +++ b/src/resources/rmd/ojs_static.R @@ -45,7 +45,8 @@ ojs_define <- function(...) { dataframe = "columns", null = "null", na = "null", - auto_unbox = TRUE + auto_unbox = TRUE, + digits = NA ) script_string <- c( "