|
| 1 | +/* |
| 2 | + * discardable-text.test.ts |
| 3 | + * |
| 4 | + * Copyright (C) 2026 Posit Software, PBC |
| 5 | + */ |
| 6 | + |
| 7 | +import { unitTest } from "../../test.ts"; |
| 8 | +import { assertEquals } from "testing/asserts"; |
| 9 | +import { isDiscardableTextExecuteResult } from "../../../src/core/jupyter/jupyter.ts"; |
| 10 | +import { kTextPlain } from "../../../src/core/mime.ts"; |
| 11 | +import { JupyterOutput } from "../../../src/core/jupyter/types.ts"; |
| 12 | + |
| 13 | +// An execute_result whose only mime bundle is text/plain, stored as the |
| 14 | +// per-line array Jupyter emits. |
| 15 | +const execResult = (textPlainLines: string[]): JupyterOutput => ({ |
| 16 | + output_type: "execute_result", |
| 17 | + data: { [kTextPlain]: textPlainLines }, |
| 18 | +}); |
| 19 | + |
| 20 | +// text/plain reprs that top-level plotting expressions echo under |
| 21 | +// ipynb-shell-interactivity: all. The matplotlib reprs below are captured |
| 22 | +// verbatim from the executed 11150.quarto_ipynb (keep-ipynb: true) — Jupyter |
| 23 | +// emits text/plain as a per-line array, so multi-line reprs have length > 1. |
| 24 | +const boxplotDict = [ |
| 25 | + "{'whiskers': [<matplotlib.lines.Line2D at 0x17685b54980>,\n", |
| 26 | + " <matplotlib.lines.Line2D at 0x17685b54ad0>],\n", |
| 27 | + " 'caps': [<matplotlib.lines.Line2D at 0x17685b54c20>,\n", |
| 28 | + " <matplotlib.lines.Line2D at 0x17685b54d70>],\n", |
| 29 | + " 'boxes': [<matplotlib.lines.Line2D at 0x17685b54830>],\n", |
| 30 | + " 'medians': [<matplotlib.lines.Line2D at 0x17685b54ec0>],\n", |
| 31 | + " 'fliers': [<matplotlib.lines.Line2D at 0x17685b55010>],\n", |
| 32 | + " 'means': []}", |
| 33 | +]; |
| 34 | +// A single-line dict of matplotlib artists (small enough that IPython does not |
| 35 | +// pretty-print it across lines). Unambiguous plotting noise. |
| 36 | +const singleLineDict = ["{'line': <matplotlib.lines.Line2D at 0x1407>}"]; |
| 37 | +const titleText = ["Text(0.5, 1.0, 'Test')"]; |
| 38 | +const axesRepr = ["<Axes: title={'center': 'Test'}>"]; |
| 39 | +const line2DRepr = ["<matplotlib.lines.Line2D at 0x14071bc7e00>"]; |
| 40 | +// A DataFrame echoed alongside the figure: multi-line text/plain plus text/html |
| 41 | +// (also captured from 11150.quarto_ipynb). Two mime keys -> must be kept. |
| 42 | +const dataFrameText = [ |
| 43 | + " day total_bill\n", |
| 44 | + "0 Fri 17.151579\n", |
| 45 | + "1 Sat 20.441379\n", |
| 46 | + "2 Sun 21.410000\n", |
| 47 | + "3 Thur 17.682742", |
| 48 | +]; |
| 49 | + |
| 50 | +unitTest( |
| 51 | + "jupyter - isDiscardableTextExecuteResult drops plotting-call reprs only alongside an image", |
| 52 | + // deno-lint-ignore require-await |
| 53 | + async () => { |
| 54 | + // Discard plotting-object noise when the cell also emitted an image. |
| 55 | + assertEquals( |
| 56 | + isDiscardableTextExecuteResult(execResult(titleText), true), |
| 57 | + true, |
| 58 | + "plt.title()/set_title() Text repr should be discarded", |
| 59 | + ); |
| 60 | + assertEquals( |
| 61 | + isDiscardableTextExecuteResult(execResult(boxplotDict), true), |
| 62 | + true, |
| 63 | + "plt.boxplot() multi-line dict of Line2D should be discarded", |
| 64 | + ); |
| 65 | + assertEquals( |
| 66 | + isDiscardableTextExecuteResult(execResult(axesRepr), true), |
| 67 | + true, |
| 68 | + "seaborn/pandas Axes repr should be discarded", |
| 69 | + ); |
| 70 | + assertEquals( |
| 71 | + isDiscardableTextExecuteResult(execResult(line2DRepr), true), |
| 72 | + true, |
| 73 | + "bare Line2D repr should be discarded", |
| 74 | + ); |
| 75 | + assertEquals( |
| 76 | + isDiscardableTextExecuteResult(execResult(singleLineDict), true), |
| 77 | + true, |
| 78 | + "single-line dict of matplotlib artists should be discarded", |
| 79 | + ); |
| 80 | + |
| 81 | + // Gate: with no image in the cell, nothing here is discarded — a cell |
| 82 | + // that only echoed these values (no figure) must keep them. |
| 83 | + assertEquals( |
| 84 | + isDiscardableTextExecuteResult(execResult(titleText), false), |
| 85 | + false, |
| 86 | + "Text repr must be kept when the cell has no image", |
| 87 | + ); |
| 88 | + assertEquals( |
| 89 | + isDiscardableTextExecuteResult(execResult(boxplotDict), false), |
| 90 | + false, |
| 91 | + "boxplot dict must be kept when the cell has no image", |
| 92 | + ); |
| 93 | + |
| 94 | + // No over-suppression: legitimate output survives even alongside an image. |
| 95 | + // A Name(...) repr that is not a matplotlib Text must not be swept up. |
| 96 | + assertEquals( |
| 97 | + isDiscardableTextExecuteResult( |
| 98 | + execResult(["datetime.datetime(2020, 1, 1, 0, 0)"]), |
| 99 | + true, |
| 100 | + ), |
| 101 | + false, |
| 102 | + "non-matplotlib single-line Name(...) repr must be kept", |
| 103 | + ); |
| 104 | + // A non-matplotlib Text( repr (e.g. rich.text.Text -> Text('...')) has no |
| 105 | + // leading coordinate, so it must be kept. |
| 106 | + assertEquals( |
| 107 | + isDiscardableTextExecuteResult(execResult(["Text('hello world')"]), true), |
| 108 | + false, |
| 109 | + "non-matplotlib Text('...') repr must be kept", |
| 110 | + ); |
| 111 | + // A multi-line bracketed value (list/tuple) with no matplotlib reference |
| 112 | + // must survive — the discard heuristic must not widen to arbitrary brackets. |
| 113 | + assertEquals( |
| 114 | + isDiscardableTextExecuteResult( |
| 115 | + execResult(["[1, 2, 3,\n", " 4, 5, 6]"]), |
| 116 | + true, |
| 117 | + ), |
| 118 | + false, |
| 119 | + "multi-line list with no matplotlib reference must be kept", |
| 120 | + ); |
| 121 | + // An arbitrary multi-line repr with no matplotlib reference must survive. |
| 122 | + assertEquals( |
| 123 | + isDiscardableTextExecuteResult( |
| 124 | + execResult(["MyResult(\n", " value=42,\n", ")"]), |
| 125 | + true, |
| 126 | + ), |
| 127 | + false, |
| 128 | + "multi-line custom repr with no matplotlib reference must be kept", |
| 129 | + ); |
| 130 | + // Multiple mime types (e.g. a DataFrame's text/plain + text/html) -> keep. |
| 131 | + assertEquals( |
| 132 | + isDiscardableTextExecuteResult( |
| 133 | + { |
| 134 | + output_type: "execute_result", |
| 135 | + data: { [kTextPlain]: dataFrameText, "text/html": ["<table>"] }, |
| 136 | + }, |
| 137 | + true, |
| 138 | + ), |
| 139 | + false, |
| 140 | + "multi-mime output (DataFrame text/plain + text/html) must be kept", |
| 141 | + ); |
| 142 | + |
| 143 | + // Only execute_result is ever discardable. |
| 144 | + assertEquals( |
| 145 | + isDiscardableTextExecuteResult( |
| 146 | + { output_type: "display_data", data: { [kTextPlain]: titleText } }, |
| 147 | + true, |
| 148 | + ), |
| 149 | + false, |
| 150 | + "display_data must never be discarded", |
| 151 | + ); |
| 152 | + assertEquals( |
| 153 | + isDiscardableTextExecuteResult( |
| 154 | + { output_type: "stream", name: "stdout", text: titleText }, |
| 155 | + true, |
| 156 | + ), |
| 157 | + false, |
| 158 | + "stream output must never be discarded", |
| 159 | + ); |
| 160 | + }, |
| 161 | +); |
0 commit comments