131 lines
3.5 KiB
Go
131 lines
3.5 KiB
Go
package contract
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.ipao.vip/rogee/go-sip/contracts"
|
|
)
|
|
|
|
func TestDecodeExecuteValidatesImportedSchema(t *testing.T) {
|
|
raw, err := contracts.Read("examples/call.execute.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
envelope, payload, err := DecodeExecute(raw)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if envelope.TenantKey != "tenant-demo-key" || payload.ExecutionID == "" {
|
|
t.Fatalf("unexpected decoded command: %+v %+v", envelope, payload)
|
|
}
|
|
}
|
|
|
|
func TestValidateTenantKeyPreservesRawValueAndBudget(t *testing.T) {
|
|
for _, key := range []string{"客户-A", " tenant /raw "} {
|
|
if err := ValidateTenantKey(key); err != nil {
|
|
t.Fatalf("tenant key %q: %v", key, err)
|
|
}
|
|
}
|
|
if err := ValidateTenantKey(strings.Repeat("x", 225)); err == nil {
|
|
t.Fatal("expected routing budget rejection")
|
|
}
|
|
}
|
|
|
|
func TestEventFixtures(t *testing.T) {
|
|
positive := []string{
|
|
"examples/event-command-result.json",
|
|
"examples/event-call-status.json",
|
|
"examples/event-transcript-updated.json",
|
|
"examples/event-call-finished.json",
|
|
"examples/event-recording-ready.json",
|
|
"examples/event-recording-failed.json",
|
|
"examples/event-transcript-failed.json",
|
|
"examples/event-contact-opt-out.json",
|
|
}
|
|
for _, name := range positive {
|
|
raw, err := contracts.Read(name)
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", name, err)
|
|
}
|
|
if err := ValidateEvent(raw); err != nil {
|
|
t.Fatalf("%s: %v", name, err)
|
|
}
|
|
}
|
|
invalid, err := contracts.Read("examples/invalid-event-unknown-type.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := ValidateEvent(invalid); err == nil {
|
|
t.Fatal("expected invalid transcript alias to be rejected")
|
|
}
|
|
}
|
|
|
|
func TestProjectOwnedW01Fixtures(t *testing.T) {
|
|
valid := []string{
|
|
"examples/ai-authorization.json",
|
|
"examples/oss-upload-grant.json",
|
|
"examples/static-cell-artifact.json",
|
|
"p1-development-profile.json",
|
|
}
|
|
for _, name := range valid {
|
|
raw, err := contracts.Read(name)
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", name, err)
|
|
}
|
|
if err := ValidateSourceSchema(schemaNameForFixture(name), raw); err != nil {
|
|
t.Fatalf("%s: %v", name, err)
|
|
}
|
|
}
|
|
invalid := []struct {
|
|
name string
|
|
schema string
|
|
}{
|
|
{"examples/invalid-ai-authorization-revoked.json", "ai-authorization.schema.json"},
|
|
{"examples/invalid-oss-upload-http.json", "oss-upload.schema.json"},
|
|
}
|
|
for _, tc := range invalid {
|
|
raw, err := contracts.Read(tc.name)
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", tc.name, err)
|
|
}
|
|
if err := ValidateSourceSchema(tc.schema, raw); err == nil {
|
|
t.Fatalf("%s: expected rejection", tc.name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func schemaNameForFixture(name string) string {
|
|
switch name {
|
|
case "examples/ai-authorization.json":
|
|
return "ai-authorization.schema.json"
|
|
case "examples/oss-upload-grant.json":
|
|
return "oss-upload.schema.json"
|
|
case "examples/static-cell-artifact.json":
|
|
return "static-cell-artifact.schema.json"
|
|
case "p1-development-profile.json":
|
|
return "p1-development-profile.schema.json"
|
|
default:
|
|
return ""
|
|
}
|
|
}
|
|
|
|
func TestEventBuilder(t *testing.T) {
|
|
raw, err := (EventBuilder{
|
|
TenantID: "tenant-1", TenantKey: "tenant-1", TraceID: "trace-1",
|
|
EventType: "command.result", Aggregate: "command", AggregateID: "cmd-1", Version: 1,
|
|
Payload: map[string]any{
|
|
"command_id": "cmd-1", "command_type": "call.execute",
|
|
"status": "accepted", "reason_code": "accepted",
|
|
"execution_id": "execution-1",
|
|
},
|
|
}).Marshal(time.Unix(0, 0), "event-1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := ValidateEvent(raw); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|