Artifact 28338cb47102a2c516c44167f1228aa48e09ff78d4cf56755009fe90bdd11908:
- File
internal/pikchr/pikchr_test.go
— part of check-in
[58fe4cf214]
at
2026-08-16 12:53:54
on branch trunk
— markup: Pikchr diagrams via WebAssembly (fossil's third markup)
pikchr.c built to a standalone WASI module (-DPIKCHR_SHELL -sSTANDALONE_WASM) and run under wazero, so the CGO-free constraint holds. Its own CLI reads - and writes SVG, so the host ABI is a pipe: no memory marshalling to get wrong. Renders text/x-pikchr artifacts and pikchr fences inside markdown; a diagram that will not compile degrades to its source plus the compiler's message.
SVG does NOT ride the document policy. It gets a narrow allow-list derived by enumerating what the module actually emits (six elements, a closed geometry/paint attribute set, no script/href/foreignObject), so even a hole in Pikchr's own label escaping cannot reach a page. Fence placeholders are alphanumeric with a per-render nonce: an HTML parser rewrites control bytes, so a NUL-based marker does not survive sanitizing (caught by test). Provenance and the rebuild recipe are in the ledger. (user: dmestas, size: 1048) [annotate] [blame] [check-ins using]
package pikchr import ( "strings" "testing" ) func TestRenderDiagram(t *testing.T) { out, err := Render([]byte(` box "hello" fit arrow right circle "world" fit `)) if err != nil { t.Fatal(err) } svg := string(out) if !strings.Contains(svg, "<svg") || !strings.Contains(svg, "</svg>") { t.Fatalf("not an svg:\n%s", svg[:min(200, len(svg))]) } if !strings.Contains(svg, "hello") || !strings.Contains(svg, "world") { t.Fatalf("labels missing:\n%s", svg[:min(400, len(svg))]) } } func TestSyntaxErrorSurfaces(t *testing.T) { if _, err := Render([]byte("this is not ( valid pikchr")); err == nil { t.Fatal("want an error for invalid source") } } // Instances must not leak state between renders. func TestRepeatedRenders(t *testing.T) { for i := 0; i < 3; i++ { out, err := Render([]byte(`box "n" fit`)) if err != nil { t.Fatalf("render %d: %v", i, err) } if !strings.Contains(string(out), "<svg") { t.Fatalf("render %d produced no svg", i) } } } func min(a, b int) int { if a < b { return a } return b }