ZDDC/zddc/internal/zddc/file_test.go
ZDDC 4eeb25c0ef feat(server): local-only tool-HTML override; remove apps URL/version fetching
Replaces the URL/channel/version-fetching tool-HTML system with a
local-only override model. No network fetch, no Ed25519 signatures, no
channels/versions, no `apps:` .zddc key.

Tool HTML resolves, in precedence:
1. a real file on disk at the path (operator drops browse.html / archive.html
   / a new mytool.html) — served by the existing static handler;
2. an `<app>.html` member of the site-root <ZDDC_ROOT>/.zddc.zip bundle, read
   server-side via internal/zipfs (local file, no fetch, no signature;
   re-stat'd each request for free hot-reload);
3. the embedded //go:embed default.

Remove (complete unwire):
- internal/apps/{fetch,verify,cache,singleflight}.go and their tests; the
  spec-parsing/cascade machinery in apps.go (ParseSpec/Resolve/PreviewLine/
  SpecComponents/appsState, DefaultUpstream*/DefaultChannel/CacheDirName).
- --apps-pubkey / ZDDC_APPS_PUBKEY flag+env+Config field; the setupApps
  cache/fetcher/pubkey wiring (now just apps.NewServer(root, version)).
- the `apps:` / `apps_pubkey:` .zddc keys: ZddcFile.Apps/AppsPubKey, the
  walker merges, cascade-summary adds, validate.go apps validation
  (ValidateAppSourceSpec/validateURLSpec/validateChannelOrVersion/
  AppsDefaultKey/IsValidAppsKey), and the isZero/is-empty refs. A stale
  apps:/apps_pubkey: in an existing .zddc is now silently ignored
  (back-compat), not a parse error. Client .zddc validator (preview-yaml.js)
  drops the apps/apps_pubkey keys + appsmap case.

Add:
- internal/apps/bundle.go — nil-safe Bundle over <root>/.zddc.zip with
  stat-based hot-reload, size caps, corrupt-zip tolerance.
- handler.go: Server{Bundle}, resolveBytes (bundle→embedded), simplified
  Serve; X-ZDDC-Source = bundle:<m> / embedded:<app>@<ver>.
- dispatch: GET /.zddc.zip is 404 for everyone (config, not content); the
  server reads members from the filesystem internally.

Tests: new bundle_test.go (member hit/absent/no-file/hot-reload/corrupt);
handler_test.go rewritten for bundle-overrides-embedded, absent-member→
embedded, unknown-tool 503, conditional-GET for both sources; dispatch test
covers bundle override + /.zddc.zip 404 + availability rules. go build/vet/
test ./... all green; gofmt clean. Docs (AGENTS.md, ARCHITECTURE.md) updated.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-04 08:59:28 -05:00

135 lines
3.5 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")
}
// A stale `apps:` key in the fixture is ignored (the key was removed),
// not a parse error — back-compat for existing .zddc files.
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)
}
}
// Inherit defaults to "inherit normally" when the field is absent;
// explicit true behaves the same; explicit false marks the level as
// a fence.
func TestParseFile_InheritDirective(t *testing.T) {
cases := []struct {
name string
body string
wantPtrNil bool
wantInherit bool
}{
{
name: "absent → nil pointer, inherits",
body: `acl:
permissions:
"*@example.com": r
`,
wantPtrNil: true,
wantInherit: true,
},
{
name: "explicit true → non-nil, inherits",
body: `acl:
inherit: true
permissions:
"*@example.com": r
`,
wantPtrNil: false,
wantInherit: true,
},
{
name: "explicit false → non-nil, fences",
body: `acl:
inherit: false
permissions:
"*@vendor.com": rwcd
`,
wantPtrNil: false,
wantInherit: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), ".zddc")
if err := os.WriteFile(path, []byte(tc.body), 0o644); err != nil {
t.Fatalf("write: %v", err)
}
zf, err := ParseFile(path)
if err != nil {
t.Fatalf("ParseFile: %v", err)
}
if (zf.ACL.Inherit == nil) != tc.wantPtrNil {
t.Errorf("Inherit pointer nil=%v want %v", zf.ACL.Inherit == nil, tc.wantPtrNil)
}
if got := zf.ACL.InheritsAncestors(); got != tc.wantInherit {
t.Errorf("InheritsAncestors() = %v want %v", got, tc.wantInherit)
}
})
}
}