This repository has been archived on 2026-04-03. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
core/server/handlers/package_validate_test.go
Jeffrey Smith c2473efee2
All checks were successful
CI/CD / detect-changes (pull_request) Successful in 4s
CI/CD / test-runners (pull_request) Has been skipped
CI/CD / e2e-smoke (pull_request) Has been skipped
CI/CD / test-frontend (pull_request) Successful in 5s
CI/CD / test-go-pg (pull_request) Successful in 2m48s
CI/CD / test-sqlite (pull_request) Successful in 2m57s
CI/CD / build-and-deploy (pull_request) Successful in 1m23s
Feat v0.9.0 multi-surface packages (#73)
Packages can declare a `surfaces` array with per-path access controls,
titles, and layouts. A single package can serve a public form, an
authenticated dashboard, and an admin page — each with independent
access enforcement.

Kernel:
- Manifest validation for surfaces array (path, access, duplicates)
- Auto-synthesis from legacy auth/layout for existing packages
- Unified /s/:slug route tree (RegisterExtensionRoutes) dispatches
  between surface rendering and ext API calls
- matchSurface() with Gin-style :param support and specificity ordering
- evaluateAccess() for per-surface access checks
- Nav filters out pending_review packages (pre-existing bug fix)

Frontend:
- __SURFACE_PATH__ and __SURFACE_PARAMS__ template injection
- sw.navigate(path, params) for SPA-style intra-package routing
- surface.navigate event + popstate handling
- SDK version bumped to 0.9.0

Docs:
- MULTI-SURFACE-GUIDE.md — full developer guide
- PACKAGE-FORMAT.md — surfaces field reference
- CHANGELOG.md, ROADMAP.md updated

22 new tests (11 manifest validation, 11 route matching/nav).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-03 12:34:38 +00:00

352 lines
8.3 KiB
Go

package handlers
import (
"testing"
)
func TestValidateManifest_ValidSurface(t *testing.T) {
m := map[string]any{
"id": "my-surface",
"title": "My Surface",
"type": "surface",
}
info, err := ValidateManifest(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if info.ID != "my-surface" {
t.Errorf("expected id 'my-surface', got %q", info.ID)
}
if info.Type != "surface" {
t.Errorf("expected type 'surface', got %q", info.Type)
}
if info.Version != "0.0.0" {
t.Errorf("expected default version '0.0.0', got %q", info.Version)
}
}
func TestValidateManifest_MissingID(t *testing.T) {
m := map[string]any{
"title": "No ID",
}
_, err := ValidateManifest(m)
if err == nil {
t.Fatal("expected error for missing id")
}
}
func TestValidateManifest_MissingTitle(t *testing.T) {
m := map[string]any{
"id": "no-title",
}
_, err := ValidateManifest(m)
if err == nil {
t.Fatal("expected error for missing title")
}
}
func TestValidateManifest_InvalidID(t *testing.T) {
m := map[string]any{
"id": "UPPERCASE-BAD",
"title": "Bad ID",
}
_, err := ValidateManifest(m)
if err == nil {
t.Fatal("expected error for invalid package id")
}
}
func TestValidateManifest_InvalidType(t *testing.T) {
m := map[string]any{
"id": "my-pkg",
"title": "My Package",
"type": "invalid",
}
_, err := ValidateManifest(m)
if err == nil {
t.Fatal("expected error for invalid type")
}
}
func TestValidateManifest_ExtensionNoTools(t *testing.T) {
m := map[string]any{
"id": "my-ext",
"title": "My Extension",
"type": "extension",
}
_, err := ValidateManifest(m)
if err == nil {
t.Fatal("expected error for extension without tools/pipes/hooks")
}
}
func TestValidateManifest_LibraryNoExports(t *testing.T) {
m := map[string]any{
"id": "my-lib",
"title": "My Library",
"type": "library",
}
_, err := ValidateManifest(m)
if err == nil {
t.Fatal("expected error for library without exports")
}
}
func TestValidateManifest_ValidLibrary(t *testing.T) {
m := map[string]any{
"id": "my-lib",
"title": "My Library",
"type": "library",
"version": "1.2.0",
"exports": []any{"module_a", "module_b"},
}
info, err := ValidateManifest(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if info.Version != "1.2.0" {
t.Errorf("expected version '1.2.0', got %q", info.Version)
}
if !info.HasExports {
t.Error("expected HasExports to be true")
}
}
func TestValidateManifest_NilManifest(t *testing.T) {
_, err := ValidateManifest(nil)
if err == nil {
t.Fatal("expected error for nil manifest")
}
}
func TestValidateManifest_SignatureField(t *testing.T) {
m := map[string]any{
"id": "my-pkg",
"title": "Signed Package",
"type": "surface",
"signature": "sha256:abc123",
}
info, err := ValidateManifest(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if info.Signature != "sha256:abc123" {
t.Errorf("expected signature 'sha256:abc123', got %q", info.Signature)
}
}
func TestValidateManifest_Dependencies(t *testing.T) {
m := map[string]any{
"id": "consumer",
"title": "Consumer Package",
"type": "surface",
"dependencies": map[string]any{
"chat-core": ">=1.0.0",
},
}
info, err := ValidateManifest(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(info.Dependencies) != 1 {
t.Errorf("expected 1 dependency, got %d", len(info.Dependencies))
}
}
func TestValidateManifest_ValidTestRunner(t *testing.T) {
m := map[string]any{
"id": "icd-test-runner",
"title": "ICD Test Runner",
"type": "test-runner",
}
info, err := ValidateManifest(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if info.Type != "test-runner" {
t.Errorf("expected type 'test-runner', got %q", info.Type)
}
}
func TestValidateManifest_TestRunnerWithRequires(t *testing.T) {
m := map[string]any{
"id": "chat-runner",
"title": "Chat Runner",
"type": "test-runner",
"requires": []any{"chat", "chat-core"},
}
info, err := ValidateManifest(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(info.Requires) != 2 {
t.Errorf("expected 2 requires, got %d", len(info.Requires))
}
if info.Requires[0] != "chat" || info.Requires[1] != "chat-core" {
t.Errorf("unexpected requires: %v", info.Requires)
}
}
func TestValidateManifest_WorkflowNoDef(t *testing.T) {
m := map[string]any{
"id": "my-wf",
"title": "My Workflow",
"type": "workflow",
}
_, err := ValidateManifest(m)
if err == nil {
t.Fatal("expected error for workflow without workflow_definition")
}
}
// ── Surfaces validation ─────────────────────────────────────
func TestValidateManifest_ValidSurfaces(t *testing.T) {
m := map[string]any{
"id": "bug-tracker",
"title": "Bug Tracker",
"type": "full",
"hooks": []any{"surface"},
"surfaces": []any{
map[string]any{"path": "/", "title": "Dashboard"},
map[string]any{"path": "/submit", "access": "public", "title": "Report a Bug"},
map[string]any{"path": "/:id", "title": "Bug Detail", "nav": false},
map[string]any{"path": "/admin", "access": "admin", "title": "Settings"},
},
}
info, err := ValidateManifest(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !info.HasSurfaces {
t.Error("expected HasSurfaces to be true")
}
if !info.HasRoute {
t.Error("expected HasRoute to be true")
}
}
func TestValidateManifest_EmptySurfaces(t *testing.T) {
m := map[string]any{
"id": "my-pkg",
"title": "My Package",
"type": "surface",
"surfaces": []any{},
}
_, err := ValidateManifest(m)
if err == nil {
t.Fatal("expected error for empty surfaces array")
}
}
func TestValidateManifest_SurfaceMissingPath(t *testing.T) {
m := map[string]any{
"id": "my-pkg",
"title": "My Package",
"type": "surface",
"surfaces": []any{
map[string]any{"title": "No Path"},
},
}
_, err := ValidateManifest(m)
if err == nil {
t.Fatal("expected error for surface without path")
}
}
func TestValidateManifest_SurfacePathNoSlash(t *testing.T) {
m := map[string]any{
"id": "my-pkg",
"title": "My Package",
"type": "surface",
"surfaces": []any{
map[string]any{"path": "submit"},
},
}
_, err := ValidateManifest(m)
if err == nil {
t.Fatal("expected error for path not starting with /")
}
}
func TestValidateManifest_SurfaceDuplicatePath(t *testing.T) {
m := map[string]any{
"id": "my-pkg",
"title": "My Package",
"type": "surface",
"surfaces": []any{
map[string]any{"path": "/"},
map[string]any{"path": "/"},
},
}
_, err := ValidateManifest(m)
if err == nil {
t.Fatal("expected error for duplicate surface path")
}
}
func TestValidateManifest_SurfaceInvalidAccess(t *testing.T) {
m := map[string]any{
"id": "my-pkg",
"title": "My Package",
"type": "surface",
"surfaces": []any{
map[string]any{"path": "/", "access": "superuser"},
},
}
_, err := ValidateManifest(m)
if err == nil {
t.Fatal("expected error for invalid access level")
}
}
func TestValidateManifest_SurfaceGroupAccess(t *testing.T) {
m := map[string]any{
"id": "my-pkg",
"title": "My Package",
"type": "surface",
"surfaces": []any{
map[string]any{"path": "/", "access": "group:engineering"},
},
}
info, err := ValidateManifest(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !info.HasSurfaces {
t.Error("expected HasSurfaces to be true")
}
}
func TestValidateManifest_AutoSynthesizeSurfaces(t *testing.T) {
m := map[string]any{
"id": "legacy-pkg",
"title": "Legacy Package",
"type": "surface",
"auth": "public",
"layout": "editor",
}
info, err := ValidateManifest(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !info.HasSurfaces {
t.Error("expected HasSurfaces after auto-synthesis")
}
// Verify the synthesized surfaces array was injected into the manifest
surfaces, ok := m["surfaces"].([]any)
if !ok || len(surfaces) != 1 {
t.Fatalf("expected 1 synthesized surface, got %v", m["surfaces"])
}
s := surfaces[0].(map[string]any)
if s["path"] != "/" {
t.Errorf("expected path '/', got %q", s["path"])
}
if s["access"] != "public" {
t.Errorf("expected access 'public', got %q", s["access"])
}
if s["layout"] != "editor" {
t.Errorf("expected layout 'editor', got %q", s["layout"])
}
}