75 lines
2.3 KiB
Go
75 lines
2.3 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"kra/pkg/module"
|
|
platformtask "kra/pkg/task"
|
|
)
|
|
|
|
func TestCatalogIncludesSystemDefinition(t *testing.T) {
|
|
catalog := Catalog()
|
|
if len(catalog.Definitions) != 2 {
|
|
t.Fatalf("definitions = %d, want 2", len(catalog.Definitions))
|
|
}
|
|
if catalog.Definitions[0].Name != "system" || catalog.Definitions[1].Name != "payment" {
|
|
t.Fatalf("definition order = [%q, %q], want [system, payment]", catalog.Definitions[0].Name, catalog.Definitions[1].Name)
|
|
}
|
|
if got := catalog.MigrationSteps(); len(got) != 5 {
|
|
t.Fatalf("module migrations = %d, want 5", len(got))
|
|
}
|
|
if surface := catalog.Surface(); len(surface.Menus) != 3 || len(surface.APIs) != 15 {
|
|
t.Fatalf("admin surface = %d menus/%d APIs, want 3/15", len(surface.Menus), len(surface.APIs))
|
|
}
|
|
if got := catalog.DefaultTimedTasks(); len(got) != 2 {
|
|
t.Fatalf("default timed tasks = %d, want 2", len(got))
|
|
}
|
|
}
|
|
|
|
func TestTaskRegistryRegistersStaticModuleMethods(t *testing.T) {
|
|
method := platformtask.Method{
|
|
Name: "test.static",
|
|
Run: func(context.Context, json.RawMessage) error { return nil },
|
|
}
|
|
catalog := module.Catalog{Definitions: []module.Definition{{Tasks: []platformtask.Method{method}}}}
|
|
registry := TaskRegistry(catalog)
|
|
if _, ok := registry.Lookup(method.Name); !ok {
|
|
t.Fatalf("method %q was not registered", method.Name)
|
|
}
|
|
}
|
|
|
|
type testRouteRegistrar struct{ called bool }
|
|
|
|
func (registrar *testRouteRegistrar) RegisterRoutes(*gin.RouterGroup, *gin.RouterGroup, *gin.Engine) {
|
|
registrar.called = true
|
|
}
|
|
|
|
type testTaskContributor struct{ name string }
|
|
|
|
func (contributor testTaskContributor) RegisterTasks(registry *platformtask.Registry) {
|
|
registry.Register(platformtask.Method{
|
|
Name: contributor.name,
|
|
Run: func(context.Context, json.RawMessage) error { return nil },
|
|
})
|
|
}
|
|
|
|
func TestRuntimeAppliesAllContributions(t *testing.T) {
|
|
registry := platformtask.NewRegistry()
|
|
route := &testRouteRegistrar{}
|
|
runtime := Runtime(RuntimeContributions{
|
|
Routes: []module.RouteRegistrar{route},
|
|
Tasks: []platformtask.Contributor{testTaskContributor{name: "test.runtime"}},
|
|
}, registry)
|
|
|
|
if _, ok := registry.Lookup("test.runtime"); !ok {
|
|
t.Fatal("runtime task contributor was not applied")
|
|
}
|
|
runtime.RegisterRoutes(nil, nil, nil)
|
|
if !route.called {
|
|
t.Fatal("runtime route contributor was not called")
|
|
}
|
|
}
|