aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/tests/test_csv_cell.py
blob: ca38805e6a386c433b7329ffead6be5e3a2ed566 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""
The audit export never hands a spreadsheet a formula.

It carries text a member chose — a refused blob's kind, a file name. A cell that
starts with `=`, `+`, `-` or `@` runs as a formula when the operator opens the
file, so it is given a leading apostrophe, which spreadsheets read as text.
"""

import json
import shutil
import subprocess
from pathlib import Path

import pytest

CSV = Path(__file__).resolve().parents[1] / "src" / "meshbay_hub" / "static" / "csv.js"

pytestmark = pytest.mark.skipif(shutil.which("node") is None, reason="node unavailable")

CASES = ['=HYPERLINK("http://x","y")', "+1+1", "-2+3", "@SUM(A1)", "\t=1", "plain",
         'with "quotes"', "a,b", "", None, 12]


def test_a_cell_is_text_and_quoted_where_it_must_be(tmp_path):
    script = tmp_path / "case.mjs"
    script.write_text(
        f"import {{ csvCell }} from '{CSV.as_uri()}';\n"
        f"console.log(JSON.stringify({json.dumps(CASES)}.map(csvCell)));\n")
    out = json.loads(subprocess.run(["node", str(script)], capture_output=True,
                                    text=True, check=True).stdout)
    assert out == ['"\'=HYPERLINK(""http://x"",""y"")"', "'+1+1", "'-2+3", "'@SUM(A1)",
                   "'\t=1", "plain", '"with ""quotes"""', '"a,b"', "", "", "12"]