package awsmodel_test import ( "os" "path/filepath" "slices" "strings" "testing" . "github.com/mulgadc/spinifex/internal/awsmodel" ) func testPageSet(t *testing.T) PageSet { t.Helper() contents, err := os.ReadFile(filepath.Join("..", "..", "docs", "coverage", "pages.json")) if err != nil { t.Fatal(err) } pages, err := ParsePageSet(contents) if err != nil { t.Fatal(err) } return pages } func TestParsePageSetAcceptsTheCheckedInFile(t *testing.T) { pages := testPageSet(t) if len(pages.Services) == 0 { t.Fatal("no service pages are configured") } // Predastore serves S3, so the page says so rather than crediting Spinifex. if implementer := pages.Services[S3].ImplementedBy; implementer != "Predastore" { t.Errorf("S3 is implemented by %q, want Predastore", implementer) } } func TestParsePageSetRejectsUnpublishableMetadata(t *testing.T) { // A well-formed index, so each case below fails only for its own reason. index := `"index":{"name":"All","title":"All","seoTitle":"` + strings.Repeat("x", 39) + ` — Spinifex Docs","description":"` + strings.Repeat("x", 155) + `","tags":["aws"]}` head := `{"category":"Coverage","basePath":"/coverage",` for name, contents := range map[string]string{ "unknown service": head + index + `,"services":{"notaservice":{}}}`, "missing category": `{"basePath":"/coverage",` + index + `,"services":{}}`, "missing basePath": `{"category":"Coverage",` + index + `,"services":{}}`, "unknown field": head + `"colour":"blue",` + index + `,"services":{}}`, "index with a slug": head + `"index":{"slug":"aws","name":"All","title":"All","seoTitle":"` + strings.Repeat("x", 39) + ` — Spinifex Docs","description":"` + strings.Repeat("x", 155) + `","tags":["aws"]}}`, "short seoTitle": head + `"index":{"name":"All","title":"All",` + `"seoTitle":"Too short — Spinifex Docs","description":"` + strings.Repeat("x", 155) + `","tags":["aws"]}}`, "missing suffix": head + `"index":{"name":"All","title":"All",` + `"seoTitle":"` + strings.Repeat("x", 55) + `","description":"` + strings.Repeat("x", 155) + `","tags":["aws"]}}`, "short description": head + `"index":{"name":"All","title":"All",` + `"seoTitle":"` + strings.Repeat("x", 39) + ` — Spinifex Docs","description":"too short","tags":["aws"]}}`, "bad service slug": head + index + `,"services":{"sts":{"slug":"STS Coverage","name":"STS","title":"STS",` + `"seoTitle":"` + strings.Repeat("x", 39) + ` — Spinifex Docs","description":"` + strings.Repeat("x", 155) + `","tags":["aws"]}}}`, } { t.Run(name, func(t *testing.T) { if _, err := ParsePageSet([]byte(contents)); err == nil { t.Fatal("ParsePageSet accepted unpublishable metadata") } }) } } func TestRenderServicePagePublishesOnlyImplementedOperations(t *testing.T) { pages := testPageSet(t) coverage, err := CompareOperations(STS, DispatchInventory{ Registered: []string{"AssumeRole", "GetCallerIdentity", "GetSessionToken", "PublishInternal"}, Stubbed: []string{"GetSessionToken"}, Unsupported: []string{"GetCallerIdentity"}, }) if err != nil { t.Fatal(err) } page, err := RenderServicePage(coverage, pages, "### Scope\n\nHand-written prose.\n") if err != nil { t.Fatal(err) } for _, want := range []string{ `title: "STS API Coverage"`, `category: "Coverage"`, "sections:\n - overview\n", "# STS API Coverage\n\n## Overview\n", "Spinifex implements **1 operation** in the STS `2011-06-15` API model.", "### Scope\n\nHand-written prose.", "### Operations\n\n| Operation |\n|---|\n| `AssumeRole` |\n", } { if !strings.Contains(page, want) { t.Errorf("page does not contain %q:\n%s", want, page) } } // Everything other than an implemented operation from the pinned model is // the internal report's: a stub answers with an error, a refusal serves // nothing, and a route the model does not describe has no page to sit on. for _, absent := range []string{ "GetSessionToken", "GetCallerIdentity", "PublishInternal", StatusStub, StatusOutsideModel, StatusNotImplemented, "Status", } { if strings.Contains(page, absent) { t.Errorf("page publishes %q:\n%s", absent, page) } } if strings.Contains(page, "