sweedworks

← all sources

checkpermalink.py

Round-trips shareable links through a stubbed DOM

272 lines. This is the file the build actually runs, copied verbatim at build time.

"""Drive tokens/app.js under QuickJS with a stubbed DOM to test permalinks.

No browser is available here, so this exercises the round trip directly:
type text -> click "Copy link" -> feed the resulting URL back in as a fragment
-> the text must come back byte-identical. Covers the cases that quietly break
URL handling: unicode, emoji, "&", "=", "+", "#", newlines, and malformed
escapes that must not throw.
"""

import json
import os
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, "pylib"))
ROOT = os.path.abspath(os.path.join(HERE, ".."))

import quickjs  # noqa: E402

from tokenlib import POLYFILL  # noqa: E402

DOM_STUB = r"""
var globalThis = this;
var __handlers = {};   // "id:type" -> [fn]
var __els = {};
var __lastReplaceUrl = null;

function __mkEl(id) {
  return {
    id: id, value: "", textContent: "", className: "", hidden: false,
    placeholder: "", title: "", src: "",
    _attrs: {},
    setAttribute: function (k, v) { this._attrs[k] = String(v); },
    getAttribute: function (k) { return this._attrs[k] === undefined ? null : this._attrs[k]; },
    addEventListener: function (type, fn) {
      var key = this.id + ":" + type;
      (__handlers[key] = __handlers[key] || []).push(fn);
    },
    appendChild: function () {},
    focus: function () {}, select: function () {},
    scrollIntoView: function () {}
  };
}

["pg-in","pg-tokens","pg-status","pg-tok","pg-chr","pg-rat","pg-ids","pg-ws",
 "pg-link","pg-linkwrap","pg-linkurl"].forEach(function (id) {
  __els[id] = __mkEl(id);
});

// Encoding switch buttons, so setEncoding() and its click wiring are exercised.
var __encBtns = ["o200k_base", "cl100k_base"].map(function (name) {
  var b = __mkEl("enc-" + name);
  b._attrs["data-enc"] = name;
  return b;
});

var document = {
  getElementById: function (id) { return __els[id] || null; },
  querySelectorAll: function (sel) { return sel === "[data-enc]" ? __encBtns : []; },
  createElement: function () { return __mkEl("_new"); },
  createTextNode: function () { return {}; },
  createDocumentFragment: function () { return { appendChild: function () {} }; },
  head: { appendChild: function () {} }
};

var location = { origin: "https://sweedworks.com", pathname: "/tokens/", hash: "" };
var history = { replaceState: function (a, b, url) { __lastReplaceUrl = url; } };
var __clipboard = null;
var navigator = {
  clipboard: {
    writeText: function (s) {
      __clipboard = s;
      return { then: function (ok) { ok(); return this; } };
    }
  }
};

var window = {
  addEventListener: function (type, fn) {
    var key = "window:" + type;
    (__handlers[key] = __handlers[key] || []).push(fn);
  },
  GPTTokenizer_o200k_base: null
};
// app.js checks `"IntersectionObserver" in window` — leave it absent so it
// takes the eager-load path, which is what a permalink visit does anyway.

function requestAnimationFrame(fn) { fn(); return 1; }
function cancelAnimationFrame() {}

function __fire(key) {
  var fns = __handlers[key] || [];
  for (var i = 0; i < fns.length; i++) fns[i]({});
}
"""


def harness():
    ctx = quickjs.Context()
    ctx.set_memory_limit(1 << 29)
    ctx.set_max_stack_size(1 << 22)
    ctx.eval(POLYFILL)
    ctx.eval(DOM_STUB)
    src = open(os.path.join(ROOT, "tokens", "app.js"), encoding="utf-8").read()
    ctx.eval(src)
    return ctx


CASES = [
    "strawberry",
    " strawberry",
    "How many r's are in strawberry?",
    "a & b = c",              # & and = are the fragment's own delimiters
    "one+two",                # + must not silently become a space
    "look: #hash",            # a second # inside the text
    "line one\nline two",
    "こんにちは世界",
    "🍓👩‍👩‍👧‍👦",
    "café",
    "100% of the time",       # % begins an escape sequence
    "  leading and trailing  ",
]


def main():
    failures = []

    for text in CASES:
        ctx = harness()
        ctx.set("_t", text)
        ctx.eval("__els['pg-in'].value = _t;")
        ctx.eval("__fire('pg-link:click');")

        url = ctx.eval("String(__lastReplaceUrl)")
        copied = ctx.eval("String(__clipboard)")
        if url != copied:
            failures.append((text, "address bar and clipboard disagree",
                             url, copied))
            continue

        # Now simulate someone opening that link in a fresh page.
        frag = url.split("#", 1)[1] if "#" in url else ""
        ctx2 = harness()
        ctx2.set("_h", "#" + frag)
        ctx2.eval("location.hash = _h;")
        # Re-run the script against the new location.
        src = open(os.path.join(ROOT, "tokens", "app.js"), encoding="utf-8").read()
        ctx2.eval(src)
        got = ctx2.eval("String(__els['pg-in'].value)")

        if got != text:
            failures.append((text, "round trip changed the text", repr(got), repr(text)))
        elif "?" in url.split("#")[0]:
            failures.append((text, "text leaked into the query string", url, ""))

    # The encoding switch is part of the shared state: a link made while
    # comparing GPT-4 must reopen on GPT-4, and the default must stay out of
    # the URL so ordinary links keep their readable form.
    ctx = harness()
    ctx.set("_t", "strawberry")
    ctx.eval("__els['pg-in'].value = _t;")
    ctx.eval("__fire('enc-cl100k_base:click');")
    ctx.eval("__fire('pg-link:click');")
    url = ctx.eval("String(__lastReplaceUrl)")
    if "enc=cl100k_base" not in url:
        failures.append(("enc switch", "encoding missing from link", url, ""))
    else:
        ctx2 = harness()
        ctx2.set("_h", "#" + url.split("#", 1)[1])
        ctx2.eval("location.hash = _h;")
        ctx2.eval(open(os.path.join(ROOT, "tokens", "app.js"), encoding="utf-8").read())
        pressed = ctx2.eval(
            "String(__encBtns[1].getAttribute('aria-pressed'))")
        if pressed != "true":
            failures.append(("enc switch", "reopened on the wrong encoding",
                             pressed, "true"))

    ctx = harness()
    ctx.eval("__els['pg-in'].value = 'strawberry'; __fire('pg-link:click');")
    if "enc=" in ctx.eval("String(__lastReplaceUrl)"):
        failures.append(("enc default", "default encoding should not appear in URL",
                         ctx.eval("String(__lastReplaceUrl)"), ""))

    # The shared module, exercised directly — all three interactive pages use it.
    shared = quickjs.Context()
    shared.eval("var globalThis = this;")
    shared.eval("var location = {origin:'https://sweedworks.com', pathname:'/x/', hash:''};"
                "var history = {replaceState:function(){}};"
                "var navigator = {};")
    shared.eval(open(os.path.join(ROOT, "permalink.js"), encoding="utf-8").read())

    SHARED_CASES = [
        ({"corpus": "a & b", "n": "30"}, {"n": "30"}),                 # default dropped
        ({"corpus": "🍓 = 100%", "n": "5"}, {"n": "30"}),
        ({"corpus": "line\nbreak", "probe": " strawberry"}, {}),
        ({"prompt": "it was the", "temp": "18", "topk": "0"},
         {"prompt": "it was the", "topk": "0"}),                       # two defaults
        ({"corpus": "plus+and=equals", "n": "1"}, {}),
    ]
    for params, defaults in SHARED_CASES:
        shared.set("_p", json.dumps(params))
        shared.set("_d", json.dumps(defaults))
        url = shared.eval("Permalink.build(JSON.parse(_p), JSON.parse(_d))")
        if "?" in url:
            failures.append(("shared", "query string used", url, ""))
            continue
        frag = url.split("#", 1)[1] if "#" in url else ""
        shared.set("_h", "#" + frag)
        shared.eval("location.hash = _h;")
        got = json.loads(shared.eval("JSON.stringify(Permalink.read())"))
        want = {k: v for k, v in params.items()
                if str(defaults.get(k)) != str(v)}
        if got != want:
            failures.append(("shared", "round trip differs", str(got), str(want)))
        for k in defaults:
            if str(defaults[k]) == str(params.get(k)) and k in got:
                failures.append(("shared", f"default {k} leaked into URL", url, ""))

    # num() must clamp and fall back rather than propagate nonsense.
    shared.eval("location.hash = '#a=5&b=abc&c=-9&d=999';")
    checks = [("a", 1, 0, 10, 5), ("b", 7, 0, 10, 7), ("c", 1, 0, 10, 0),
              ("d", 1, 0, 10, 10), ("missing", 3, 0, 10, 3)]
    for key, fallback, lo, hi, want in checks:
        shared.set("_k", key)
        shared.set("_f", fallback)
        shared.set("_lo", lo)
        shared.set("_hi", hi)
        got = float(shared.eval(
            "Permalink.num(Permalink.read(), _k, _f, _lo, _hi)"))
        if got != want:
            failures.append(("shared", f"num({key}) = {got}, expected {want}", "", ""))

    # Malformed fragments must not throw or wipe the page.
    for bad in ["#t=%E0%A4", "#t=%", "#t", "#ids=1", "#", "#t=%zz"]:
        ctx = harness()
        ctx.set("_h", bad)
        try:
            ctx.eval("location.hash = _h;")
            src = open(os.path.join(ROOT, "tokens", "app.js"), encoding="utf-8").read()
            ctx.eval(src)
        except Exception as e:
            failures.append((bad, "malformed fragment threw", str(e)[:120], ""))

    for text, why, a, b in failures:
        print(f"FAIL  {text!r}: {why}\n        {a}\n        {b}")

    if failures:
        print(f"\n{len(failures)} permalink failure(s)")
        return 1

    print(f"PASS  permalink round trip — {len(CASES)} strings, encoding switch "
          f"carried and restored, default kept out of the URL, "
          f"6 malformed fragments handled")
    print(f"PASS  shared module — {len(SHARED_CASES)} param sets round trip, "
          f"defaults omitted, num() clamps and falls back")

    # Show a couple of real links for the record.
    ctx = harness()
    for sample in ["strawberry", "こんにちは世界"]:
        ctx.set("_t", sample)
        ctx.eval("__els['pg-in'].value = _t; __fire('pg-link:click');")
        print(f"        {sample!r} -> {ctx.eval('String(__lastReplaceUrl)')}")
    ctx = harness()
    ctx.eval("__els['pg-in'].value = 'strawberry';"
             "__fire('enc-cl100k_base:click'); __fire('pg-link:click');")
    print(f"        on cl100k -> {ctx.eval('String(__lastReplaceUrl)')}")
    return 0


if __name__ == "__main__":
    sys.exit(main())