|
1 | 1 | package serviceoffercontroller |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "crypto/sha256" |
4 | 5 | "encoding/json" |
| 6 | + "fmt" |
5 | 7 | "strings" |
6 | 8 | "testing" |
7 | 9 |
|
@@ -46,9 +48,9 @@ func TestBuildHostHTTPRoute(t *testing.T) { |
46 | 48 | // Rule shapes: first three Exact → catalog httpd with full-path |
47 | 49 | // rewrite; last PathPrefix / → verifier with prefix rewrite + headers. |
48 | 50 | wantExact := map[string]string{ |
49 | | - "/": "/offers/sec/audit/index.html", |
50 | | - "/openapi.json": "/offers/sec/audit/openapi.json", |
51 | | - "/.well-known/x402": "/offers/sec/audit/x402.json", |
| 51 | + "/": "/offers/sec/audit/index.html", |
| 52 | + "/openapi.json": "/offers/sec/audit/openapi.json", |
| 53 | + "/.well-known/x402": "/offers/sec/audit/x402.json", |
52 | 54 | "/.well-known/agent-registration.json": "/offers/sec/audit/agent-registration.json", |
53 | 55 | } |
54 | 56 | for i := 0; i < 4; i++ { |
@@ -339,6 +341,164 @@ func TestCatalogAdvertisesDedicatedOrigin(t *testing.T) { |
339 | 341 | } |
340 | 342 | } |
341 | 343 |
|
| 344 | +// TestBuildHostHTTPRoute_AgentChatWidget pins the agent-only chat rules: |
| 345 | +// Exact /chat and /chat-vendor.js rewrite to the SHARED widget files at the |
| 346 | +// catalog httpd root (not the offer bundle dir), sit between the discovery |
| 347 | +// rules and the catch-all so they win over the payment gate, and do not |
| 348 | +// exist at all for non-agent offers (covered by TestBuildHostHTTPRoute's |
| 349 | +// rule-count pin). |
| 350 | +func TestBuildHostHTTPRoute_AgentChatWidget(t *testing.T) { |
| 351 | + offer := hostnameOffer() |
| 352 | + offer.Spec.Type = "agent" |
| 353 | + route := buildHostHTTPRoute(offer) |
| 354 | + |
| 355 | + rules, _, _ := unstructured.NestedSlice(route.Object, "spec", "rules") |
| 356 | + if len(rules) != 7 { |
| 357 | + t.Fatalf("rules = %d, want 7 (4 discovery + /chat + /chat-vendor.js + catch-all)", len(rules)) |
| 358 | + } |
| 359 | + |
| 360 | + wantShared := map[string]string{ |
| 361 | + "/chat": "/offers/sec/audit/chat.html", |
| 362 | + "/chat-vendor.js": "/chat-vendor.js", |
| 363 | + } |
| 364 | + for i := 4; i <= 5; i++ { |
| 365 | + rule := rules[i].(map[string]any) |
| 366 | + match := rule["matches"].([]any)[0].(map[string]any)["path"].(map[string]any) |
| 367 | + if match["type"] != "Exact" { |
| 368 | + t.Errorf("rule %d match type = %v, want Exact", i, match["type"]) |
| 369 | + } |
| 370 | + public := match["value"].(string) |
| 371 | + want, ok := wantShared[public] |
| 372 | + if !ok { |
| 373 | + t.Fatalf("rule %d matches unexpected path %q", i, public) |
| 374 | + } |
| 375 | + filter := rule["filters"].([]any)[0].(map[string]any) |
| 376 | + rewrite := filter["urlRewrite"].(map[string]any)["path"].(map[string]any) |
| 377 | + if got := rewrite["replaceFullPath"]; got != want { |
| 378 | + t.Errorf("rule %d (%s) rewrites to %v, want %s", i, public, got, want) |
| 379 | + } |
| 380 | + backend := rule["backendRefs"].([]any)[0].(map[string]any) |
| 381 | + if backend["name"] != staticSiteConfigMapName || backend["namespace"] != staticSiteNamespace { |
| 382 | + t.Errorf("rule %d backend = %v", i, backend) |
| 383 | + } |
| 384 | + } |
| 385 | + |
| 386 | + // /chat holds a hot session key and signs USDC transfers — it must carry |
| 387 | + // frame-ancestors 'self' so it can't be clickjacked into a cross-origin |
| 388 | + // iframe (the offer's own landing page still embeds it same-origin). |
| 389 | + chatRule := rules[4].(map[string]any) |
| 390 | + var sawCSP bool |
| 391 | + for _, rawFilter := range chatRule["filters"].([]any) { |
| 392 | + filter := rawFilter.(map[string]any) |
| 393 | + if filter["type"] != "ResponseHeaderModifier" { |
| 394 | + continue |
| 395 | + } |
| 396 | + for _, s := range filter["responseHeaderModifier"].(map[string]any)["set"].([]any) { |
| 397 | + h := s.(map[string]any) |
| 398 | + if h["name"] == "Content-Security-Policy" && h["value"] == "frame-ancestors 'self'" { |
| 399 | + sawCSP = true |
| 400 | + } |
| 401 | + } |
| 402 | + } |
| 403 | + if !sawCSP { |
| 404 | + t.Errorf("/chat rule missing Content-Security-Policy: frame-ancestors 'self'") |
| 405 | + } |
| 406 | + |
| 407 | + // Catch-all must still be last. |
| 408 | + last := rules[6].(map[string]any) |
| 409 | + match := last["matches"].([]any)[0].(map[string]any)["path"].(map[string]any) |
| 410 | + if match["type"] != "PathPrefix" { |
| 411 | + t.Fatalf("last rule = %v, want the PathPrefix catch-all", match) |
| 412 | + } |
| 413 | +} |
| 414 | + |
| 415 | +// TestOfferLandingChatEmbed pins the landing-page widget embed: agent offers |
| 416 | +// get the chat card iframing /chat; everything else keeps the plain landing. |
| 417 | +func TestOfferLandingChatEmbed(t *testing.T) { |
| 418 | + profile := schemas.StorefrontProfile{DisplayName: "Acme", ContactEmail: "ops@acme.example"} |
| 419 | + |
| 420 | + plain := buildOfferLandingHTML(hostnameOffer(), profile) |
| 421 | + if strings.Contains(plain, `data-obol="chat"`) { |
| 422 | + t.Fatalf("non-agent landing embeds the chat widget") |
| 423 | + } |
| 424 | + |
| 425 | + agent := hostnameOffer() |
| 426 | + agent.Spec.Type = "agent" |
| 427 | + withChat := buildOfferLandingHTML(agent, profile) |
| 428 | + if !strings.Contains(withChat, `data-obol="chat"`) || !strings.Contains(withChat, `src="/chat"`) { |
| 429 | + t.Fatalf("agent landing missing chat embed:\n%s", withChat) |
| 430 | + } |
| 431 | +} |
| 432 | + |
| 433 | +// TestStaticSiteServesChatWidget pins the widget delivery: the shared |
| 434 | +// vendor bundle sits in the catalog ConfigMap (served with a JavaScript |
| 435 | +// MIME type — module imports hard-fail otherwise), while the chat page is |
| 436 | +// rendered per agent offer into its bundle with the offer's title and the |
| 437 | +// same resolved theme tokens as its landing page. |
| 438 | +func TestStaticSiteServesChatWidget(t *testing.T) { |
| 439 | + cm := buildStaticSiteConfigMap("# cat", `{"services":[]}`, `{}`, "<html></html>", nil) |
| 440 | + data, _, _ := unstructured.NestedStringMap(cm.Object, "data") |
| 441 | + if data["chat-vendor.js"] == "" { |
| 442 | + t.Fatalf("catalog ConfigMap missing the shared chat vendor bundle") |
| 443 | + } |
| 444 | + if !strings.Contains(data["httpd.conf"], ".js:text/javascript") { |
| 445 | + t.Fatalf("httpd.conf missing .js MIME mapping: %q", data["httpd.conf"]) |
| 446 | + } |
| 447 | + var sawJS bool |
| 448 | + for _, raw := range staticSiteVolumeItems(nil) { |
| 449 | + item := raw.(map[string]any) |
| 450 | + if item["key"] == "chat-vendor.js" && item["path"] == "chat-vendor.js" { |
| 451 | + sawJS = true |
| 452 | + } |
| 453 | + } |
| 454 | + if !sawJS { |
| 455 | + t.Fatalf("volume items missing the chat vendor projection") |
| 456 | + } |
| 457 | + |
| 458 | + // Per-offer page: agent offers gain a chat.html bundle file carrying |
| 459 | + // the landing page's theme tokens and title; non-agent offers do not. |
| 460 | + profile := schemas.StorefrontProfile{DisplayName: "Acme"} |
| 461 | + plain := buildOfferBundles([]*monetizeapi.ServiceOffer{hostnameOffer()}, profile, noUpstreamOpenAPI) |
| 462 | + for _, f := range plain { |
| 463 | + if strings.HasSuffix(f.Path, "chat.html") { |
| 464 | + t.Fatalf("non-agent offer rendered a chat page: %s", f.Path) |
| 465 | + } |
| 466 | + } |
| 467 | + agent := hostnameOffer() |
| 468 | + agent.Spec.Type = "agent" |
| 469 | + bundles := buildOfferBundles([]*monetizeapi.ServiceOffer{agent}, profile, noUpstreamOpenAPI) |
| 470 | + var chat string |
| 471 | + for _, f := range bundles { |
| 472 | + if f.Path == "offers/sec/audit/chat.html" { |
| 473 | + chat = f.Content |
| 474 | + } |
| 475 | + } |
| 476 | + if chat == "" { |
| 477 | + t.Fatalf("agent offer bundle missing chat.html (got %d files)", len(bundles)) |
| 478 | + } |
| 479 | + theme := storefront.ResolveTheme(profile.Theme, profile.AccentColor) |
| 480 | + if !strings.Contains(chat, "--bg01:"+theme.Vars["bg01"]) { |
| 481 | + t.Errorf("chat page missing resolved theme tokens") |
| 482 | + } |
| 483 | + if !strings.Contains(chat, "chat-vendor.js?v=") { |
| 484 | + t.Errorf("chat page missing cache-busted vendor import") |
| 485 | + } |
| 486 | +} |
| 487 | + |
| 488 | +// TestChatVendorVersionMatchesBundle guards the ?v= cache-buster on |
| 489 | +// chat.html's chat-vendor.js import against a forgotten bump: the bundle is |
| 490 | +// served 1-year immutable (buildHostHTTPRoute's exactToShared), so a rebuild |
| 491 | +// that forgets to bump ?v= would silently serve returning visitors the OLD |
| 492 | +// payment-signing bundle for up to a year. This must fail CI whenever |
| 493 | +// assets/chat-vendor.js and assets/chat.html's ?v= go out of sync. |
| 494 | +func TestChatVendorVersionMatchesBundle(t *testing.T) { |
| 495 | + sum := sha256.Sum256([]byte(chatWidgetVendorJS)) |
| 496 | + want := "chat-vendor.js?v=" + fmt.Sprintf("%x", sum)[:8] |
| 497 | + if !strings.Contains(chatWidgetTmplSrc, want) { |
| 498 | + t.Fatalf("chat.html's chat-vendor.js ?v= does not match sha256(assets/chat-vendor.js); want %q (see assets/README.md rebuild steps)", want) |
| 499 | + } |
| 500 | +} |
| 501 | + |
342 | 502 | // TestHostRouteDiscoveryRulesAreGETOnly pins the method scoping: a |
343 | 503 | // root-priced offer advertises POST <origin>/ as its paid resource, so the |
344 | 504 | // Exact "/" discovery rule must only capture GETs — POSTs fall through to |
@@ -374,7 +534,7 @@ func TestBuildOfferBundles_UpstreamOpenAPI(t *testing.T) { |
374 | 534 | "get": map[string]any{ |
375 | 535 | "summary": "Leaderboard", "security": []any{map[string]any{"x402": []any{}}}, |
376 | 536 | "x-payment-info": map[string]any{"price": map[string]any{"amount": "0.001"}}, |
377 | | - "responses": map[string]any{"200": map[string]any{}, "402": map[string]any{}}, |
| 537 | + "responses": map[string]any{"200": map[string]any{}, "402": map[string]any{}}, |
378 | 538 | }, |
379 | 539 | }, |
380 | 540 | "/v1/markets/overview": map[string]any{ |
|
0 commit comments