mirror of
https://github.com/hanzoai/zip.git
synced 2026-10-30 20:11:40 +00:00
The import path a reader types is the one the org owns, rather than the forge that happens to host the bytes. hanzo.ai serves the go-import meta and points it at the same repository, so where the code lives does not change. The README's `go get` line moves with it — an install instruction naming the old path is the one leftover a reader would actually follow.
97 lines
2.8 KiB
Go
97 lines
2.8 KiB
Go
package zip_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"hanzo.ai/zip"
|
|
"hanzo.ai/zip/runtime"
|
|
)
|
|
|
|
// stubLoader is a minimal runtime.Loader fixture for tests.
|
|
type stubLoader struct{ rt string }
|
|
|
|
func (s stubLoader) Runtimes() []string { return []string{s.rt} }
|
|
func (s stubLoader) LoadDir(_ context.Context, _ string) (map[string]runtime.Module, error) {
|
|
return map[string]runtime.Module{}, nil
|
|
}
|
|
func (s stubLoader) LoadOne(_ context.Context, dir string) (runtime.Module, error) {
|
|
return &stubModule{dir: dir, rt: s.rt}, nil
|
|
}
|
|
|
|
type stubModule struct {
|
|
dir string
|
|
rt string
|
|
}
|
|
|
|
func (m *stubModule) Name() string { return m.dir }
|
|
func (m *stubModule) Runtime() string { return m.rt }
|
|
func (m *stubModule) Exports() []string { return []string{"handler"} }
|
|
func (m *stubModule) Close() error { return nil }
|
|
func (m *stubModule) Invoke(_ context.Context, fn string, payload []byte) ([]byte, error) {
|
|
return json.Marshal(map[string]any{
|
|
"status": 201,
|
|
"headers": map[string]string{"X-Test": "1"},
|
|
"body": map[string]any{"fn": fn, "echo": json.RawMessage(payload)},
|
|
})
|
|
}
|
|
|
|
func TestModuleRoute(t *testing.T) {
|
|
app := zip.New(zip.Config{
|
|
DisableStartupMessage: true,
|
|
Loader: stubLoader{rt: "goja"},
|
|
AllowedRuntimes: []string{"goja"},
|
|
})
|
|
if err := app.Module("POST /v1/eval", "goja", "./fixtures/eval"); err != nil {
|
|
t.Fatalf("Module: %v", err)
|
|
}
|
|
req, _ := http.NewRequest("POST", "/v1/eval", strings.NewReader(`{"x":1}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp, err := app.Fiber().Test(req)
|
|
if err != nil {
|
|
t.Fatalf("Test: %v", err)
|
|
}
|
|
if resp.StatusCode != 201 {
|
|
t.Fatalf("status %d, want 201", resp.StatusCode)
|
|
}
|
|
if resp.Header.Get("X-Test") != "1" {
|
|
t.Fatalf("X-Test header missing")
|
|
}
|
|
body, _ := io.ReadAll(resp.Body)
|
|
if !strings.Contains(string(body), `"fn":"eval"`) {
|
|
t.Fatalf("body=%s", body)
|
|
}
|
|
}
|
|
|
|
func TestModuleRouteRuntimeMismatch(t *testing.T) {
|
|
app := zip.New(zip.Config{
|
|
DisableStartupMessage: true,
|
|
Loader: stubLoader{rt: "goja"},
|
|
})
|
|
if err := app.Module("POST /v1/eval", "wazero", "./fixtures/eval"); err == nil {
|
|
t.Fatal("expected error for runtime mismatch")
|
|
}
|
|
}
|
|
|
|
func TestModuleRouteAllowedRuntimes(t *testing.T) {
|
|
app := zip.New(zip.Config{
|
|
DisableStartupMessage: true,
|
|
Loader: stubLoader{rt: "pyvm"},
|
|
AllowedRuntimes: []string{"goja", "wazero"}, // pyvm not allowed
|
|
})
|
|
if err := app.Module("POST /v1/eval", "pyvm", "./fixtures/eval"); err == nil {
|
|
t.Fatal("expected error for disallowed runtime")
|
|
}
|
|
}
|
|
|
|
func TestModuleRouteNoLoader(t *testing.T) {
|
|
app := zip.New(zip.Config{DisableStartupMessage: true})
|
|
if err := app.Module("POST /v1/eval", "goja", "./fixtures/eval"); err == nil {
|
|
t.Fatal("expected error when Loader is nil")
|
|
}
|
|
}
|