Files
ask-form/internal/server/fields_test.go
gabrielvidalandClaude Opus 5.5 5a39a743c7 feat: forms in English or French (--lang en|fr)
create --lang sets a form's language (stored as its lang); without it the
machine's default applies (serve --lang / ASK_FORM_LANG, which setup --lang
writes), else English. The answer page, its errors (server and client
side), the confirm step, the thanks/closed pages, upload messages and
the parts a form gets by itself (Other…, Anything else?, Option N) follow
it; the questions stay as the agent wrote them and the answers JSON is
unchanged. The page scripts read the machine's catalogue from
<html data-l10n>. The examples are served per language (/fr/examples from
spec.fr.json, linked to each other) and brand preview takes --lang. An
old serve ignoring --lang makes create warn.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 14:30:49 +01:00

85 lines
3.4 KiB
Go

package server
import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/GabrielVidal1/ask-form/examples"
"github.com/GabrielVidal1/ask-form/internal/i18n"
)
// The review-change example uses every field added with conditions: its page
// carries the conditions for the script, and the server alone (no JavaScript)
// holds the same line on what is hidden and what is required.
func TestConditionalExamplePage(t *testing.T) {
list, err := LoadExamples(examples.FS)
if err != nil {
t.Fatal(err)
}
srv := httptest.NewServer(ExamplesHandler(map[string][]Example{i18n.EN: list}, ""))
defer srv.Close()
get := func(path string) string {
res, err := http.Get(srv.URL + path)
if err != nil || res.StatusCode != 200 {
t.Fatalf("GET %s: %v %v", path, err, res)
}
b, _ := io.ReadAll(res.Body)
return string(b)
}
page := get("/examples/review-change")
for _, want := range []string{
`data-show-if="`, // a condition for the script
`data-required-if="`, // a requiredIf
`class="rating stars"`, // rating drawn as stars
`type="radio" name="f.clarity"`, // …made of radios, so it works without a script
`<span class="d-add">`, // diff lines
`<details class="block-details">`, // details block
`name="f.requested.where"`, // a table's cells
`name="f.freeze.start"`, // a date range's inputs
`type="time" name="f.window"`, // time
`class="req if-req" aria-hidden="true" hidden>`, // requiredIf marker, off until it applies
} {
if !strings.Contains(page, want) {
t.Errorf("page lacks %s", want)
}
}
if strings.Contains(page, `<script>`) {
t.Error("the answer page carries an inline script")
}
post := func(v url.Values) (int, string) {
res, err := http.PostForm(srv.URL+"/examples/review-change", v)
if err != nil {
t.Fatal(err)
}
b, _ := io.ReadAll(res.Body)
return res.StatusCode, string(b)
}
// Reject without a reason: the reason (requiredIf) is what's missing; the
// required table (showIf: approve with changes) is not asked for.
code, body := post(url.Values{"f.verdict": {"reject"}})
if code != http.StatusUnprocessableEntity || !strings.Contains(body, "What would make you change your mind?") || strings.Contains(body, "Requested changes</a>") {
t.Fatalf("reject without reason: %d", code)
}
// Reject with a reason: the rollout answers sent anyway are dropped.
code, body = post(url.Values{"f.verdict": {"reject"}, "f.reason": {"too risky"}, "f.window": {"23:00"}, "f.tell": {"on"}})
if code != 200 || !strings.Contains(body, "too risky") || strings.Contains(body, "23:00") || strings.Contains(body, `&#34;tell&#34;`) {
t.Fatalf("reject: %d %s", code, body)
}
// Approve with changes needs its table.
code, _ = post(url.Values{"f.verdict": {"changes"}})
if code != http.StatusUnprocessableEntity {
t.Fatalf("changes without rows: %d", code)
}
code, body = post(url.Values{"f.verdict": {"changes"},
"f.requested.where": {"a.go", ""}, "f.requested.what": {"rename", ""}, "f.requested.severity": {"nit", ""},
"f.freeze.start": {"2026-12-24"}, "f.freeze.end": {"2026-12-26"}})
if code != 200 || !strings.Contains(body, "rename") || !strings.Contains(body, "2026-12-24 → 2026-12-26") {
t.Fatalf("changes: %d %s", code, body)
}
}