package mapatest import ( "encoding/json" "net/http" "net/url" "strings" "testing" ) // COL7's server half, COL8 and COL13 (columns and icons spec §1.9, // §1.10, §9): a layout drawn as columns with script off; History's // words for a layout; the block routes refusing fences, a cut layout, // and a post counted by another grammar. const twoColumns = ":::: columns\n\n::: column\nLeft.\n\n:::\n\n" + "::: column\nRight.\n\n:::\n\n::::" func TestCOL7ALayoutIsDrawnAsColumns(t *testing.T) { rig := newRig(t) acme := rig.createPage(t, Ana, "Acme", "Top.\n\n"+twoColumns) page := rig.page(t, Bo, acme) if !strings.Contains(page, `
`+ `

Left.

Right.

`) { t.Fatal("the layout is not drawn as columns") } if !strings.Contains(rig.page(t, Bo, acme+"/edit"), "::: column") { t.Fatal("Edit as Markdown does not show the fences") } } func TestCOL8HistorySaysWhatHappenedToTheColumns(t *testing.T) { rig := newRig(t) acme := rig.createPage(t, Ana, "Acme", "Left.\n\nRight.") steps := []struct{ body, said string }{ {twoColumns, "Added columns"}, {strings.TrimSuffix(twoColumns, "::::") + "::: column\nMore.\n\n" + ":::\n\n::::", "Added a column"}, {"Left.\n\nRight.", "Removed columns"}, } for _, step := range steps { rig.post(t, Ana, acme, url.Values{"body": {step.body}, "revision": {revisionAs(t, rig, Ana, acme)}}) if history := rig.page(t, Ana, acme+"/history"); !strings.Contains( history, step.said) { t.Errorf("History does not say %q", step.said) } } } func TestCOL13FencesAreNoBlockToMarkOrLink(t *testing.T) { rig := newRig(t) acme := rig.createPage(t, Ana, "Acme", twoColumns) before := rig.count(t, "revisions") for _, block := range []string{"0", "1", "3"} { rig.post(t, Bo, acme+"/blocks", url.Values{"block": {block}}) } if rig.count(t, "revisions") != before { t.Fatal("a fence line took a marker") } column := rig.createPage(t, Bo, "Column", "A column.") rig.post(t, Bo, column+"/mentions", url.Values{ "source": {strings.TrimPrefix(acme, "/p/")}, "block": {"1"}}) if rig.count(t, "revisions") != before+1 { t.Fatal("Link it linked a fence line") } } func TestCOL13MakeTeamPageNeverCutsALayout(t *testing.T) { rig := newRig(t) created := rig.post(t, Ana, "/scratch", url.Values{"body": {twoColumns}}) note := created.Header().Get("Location") revision := revisionAs(t, rig, Ana, note) status, reply := rig.postJSON(Ana, note+"/share", url.Values{ "title": {"Half"}, "revision": {revision}, "block": {"1", "2", "5"}}) if status == http.StatusOK || !strings.Contains(reply["error"].(string), "inside one column") { t.Fatalf("a cut layout was shared: %d %v", status, reply) } status, _ = rig.postJSON(Ana, note+"/share", url.Values{ "title": {"Inside"}, "revision": {revision}, "block": {"2"}}) if status != http.StatusOK { t.Fatalf("a block inside one column: %d", status) } } func TestCOL13AnotherGrammarIsRefusedAndTheDraftKept(t *testing.T) { rig := newRig(t) acme := rig.createPage(t, Ana, "Acme", "One.\n\nTwo.") for _, grammar := range []url.Values{{"grammar": {""}}, {"grammar": {"1"}}} { for _, route := range []string{"/blocks", "/mentions", "/resolve"} { form := url.Values{"block": {"0"}, "source": {"x"}} for key, value := range grammar { form[key] = value } status, reply := rig.postJSON(Bo, acme+route, form) said, _ := reply["error"].(string) if status != http.StatusConflict || !strings.Contains(said, "Reload the page") { body, _ := json.Marshal(reply) t.Errorf("%s with grammar %q: %d %s", route, grammar.Get("grammar"), status, body) } } } } // COL13's forms: every server-drawn form that posts a block or hunk // index carries the grammar, and a post without it is refused with the // sentence, the draft kept. func TestCOL13TheFormsCarryTheGrammar(t *testing.T) { rig := newRig(t) created := rig.post(t, Ana, "/scratch", url.Values{"body": {twoColumns}}) note := created.Header().Get("Location") acme := rig.createPage(t, Ana, "Acme", "Acme.") rig.createPage(t, Bo, "Notes", "We met Acme.") hidden := `` for _, path := range []string{note + "/share", acme + "/blocks", acme} { if !strings.Contains(rig.page(t, Ana, path), hidden) { t.Errorf("%s has no grammar field", path) } } share := rig.page(t, Ana, note+"/share") if strings.Count(share, `name="block"`) != 3 { t.Errorf("Make team page lists %d rows, not the layout's one "+ "and its two blocks", strings.Count(share, `name="block"`)) } rig.autosave(t, Ana, note, revisionAs(t, rig, Ana, note), "0", "Typed meanwhile.") drafts := rig.count(t, "drafts") status, reply := rig.postJSON(Ana, note+"/share", url.Values{ "grammar": {""}, "title": {"T"}, "block": {"0"}, "revision": {revisionAs(t, rig, Ana, note)}}) said, _ := reply["error"].(string) if status != http.StatusConflict || !strings.Contains(said, "Reload") || rig.count(t, "drafts") != drafts { t.Fatalf("share without the grammar: %d %q", status, said) } }