ZDDC/zddc/internal/zddc/file_test.go
ZDDC 9ca36f25d8 feat(tables): new sortable/filterable grid tool for directories of YAML files
Tables is the eighth HTML tool: a read-only tabular view over a
directory of YAML files declared via `tables:` in `.zddc`. Anchor use
case is the Master Deliverables List, where each row is one
`<tracking>.yaml` under `Archive/<Party>/MDL/`. Rows click through to
the existing form renderer for editing.

Schema (zddc/internal/zddc/file.go)
  - New `Tables map[string]string` on ZddcFile. Map key becomes the URL
    stem (`tables[MDL]` → `<dir>/MDL.table.html`); the value is a path
    relative to the .zddc pointing at a `*.table.yaml` spec describing
    columns + the rows directory. No upward cascade in v1 — each
    directory hosting a table declares it directly.

Server handler (zddc/internal/handler/tablehandler.go)
  - `RecognizeTableRequest` matches GET `/<dir>/<name>.table.html`
    against the cascade's `tables:` declarations. Dispatch routes
    table requests before the form-system intercept.
  - `ServeTable` ACL-gates with `policy.ActionRead` and serves the
    embedded `tables.html` template; client walks the directory itself
    via the listing JSON or FS Access API.
  - tables.html embedded via //go:embed — same pattern as form.html.

Frontend (tables/)
  - Vanilla JS: app/context/util/filters/sort/render/main modules.
  - Reads spec + row YAML files via window.zddc.source (HTTP polyfill
    or local FS handle); js-yaml 4.1.0 vendored in shared/vendor for
    client-side parsing.
  - Sample fixtures under tables/sample/ for local testing.

Build + CI
  - Lockstep build registers tables alongside the other 7 tools (HTML
    output, embed mirror, versions.txt, release-output, tags).
  - Playwright project added; `npx playwright test --project=tables`
    is part of `npm test`.

Drive-by: rename mdedit Playwright selectors `#select-directory` →
`#addDirectoryBtn` to fix three pre-existing failing tests.

Drive-by: ignore locally-built `zddc/zddc-server` binary so it doesn't
get accidentally staged.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 20:32:01 -05:00

76 lines
2.1 KiB
Go

package zddc
import (
"os"
"path/filepath"
"testing"
)
// TestParseFile_TablesRoundTrip exercises the Tables field added to
// support the table tool. A .zddc with a tables: map should round-trip
// through ParseFile cleanly without disturbing existing fields.
func TestParseFile_TablesRoundTrip(t *testing.T) {
root := t.TempDir()
body := `acl:
permissions:
"*@example.com": rwcd
title: Demo
apps:
archive: stable
tables:
MDL: ./MDL.table.yaml
Subcontracts: ./contracts/subs.table.yaml
roles:
reviewers:
members: ["bob@example.com"]
`
path := filepath.Join(root, ".zddc")
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatalf("write .zddc: %v", err)
}
zf, err := ParseFile(path)
if err != nil {
t.Fatalf("ParseFile: %v", err)
}
if got := zf.Tables["MDL"]; got != "./MDL.table.yaml" {
t.Errorf("Tables[MDL] = %q want %q", got, "./MDL.table.yaml")
}
if got := zf.Tables["Subcontracts"]; got != "./contracts/subs.table.yaml" {
t.Errorf("Tables[Subcontracts] = %q want %q", got, "./contracts/subs.table.yaml")
}
// Sibling fields should still parse.
if zf.Title != "Demo" {
t.Errorf("Title = %q want %q", zf.Title, "Demo")
}
if got := zf.Apps["archive"]; got != "stable" {
t.Errorf("Apps[archive] = %q want %q", got, "stable")
}
if r, ok := zf.Roles["reviewers"]; !ok || len(r.Members) != 1 {
t.Errorf("Roles[reviewers] = %+v want one member", r)
}
if got := zf.ACL.Permissions["*@example.com"]; got != "rwcd" {
t.Errorf("ACL.Permissions[*@example.com] = %q want rwcd", got)
}
}
// TestParseFile_TablesEmptyOmitted confirms that a .zddc without a
// tables: key parses with a nil Tables map (omitempty round-trip).
func TestParseFile_TablesEmptyOmitted(t *testing.T) {
root := t.TempDir()
body := `title: NoTables
acl:
permissions:
"*@example.com": r
`
path := filepath.Join(root, ".zddc")
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatalf("write .zddc: %v", err)
}
zf, err := ParseFile(path)
if err != nil {
t.Fatalf("ParseFile: %v", err)
}
if zf.Tables != nil {
t.Errorf("Tables = %+v want nil for absent tables: key", zf.Tables)
}
}