mirror of
https://github.com/GabrielVidal1/ask-form.git
synced 2026-12-01 18:07:27 +01:00
`ask-form create form.yaml` takes the whole form in one file: title, description, lang and sections (each a page, keyed by its title), with fields keyed by their key, options by their value and a table's columns by their key, in the order written. A section takes an intro (markdown under its title) and a showIf for the whole page. Plain lists of option texts and the old lists (`key:` fields, `value:` options) are read too, and the flat JSON field array still works byte for byte (--fields-file spec.json). - internal/form/yaml.go: a strict YAML 1.2 subset reader, stdlib only. Ordered mappings (*form.Map), block and flow collections, | and > scalars, quotes, comments; refuses anchors, tags, ? keys, several documents, tab indentation and duplicate keys, with the line. Only true/false are booleans; a number keeps its text (json.Number). - internal/form/doc.go: ParseDoc compiles a form file (YAML, or JSON of the same shape) into the flat field array: section title -> group, intro -> a markdown block, section showIf AND-ed into its fields'. The CLI does it before POST /v1/forms, so server, relay, store and MCP are unchanged and a new CLI works with an older serve. - examples: spec.yaml + spec.fr.yaml replace the JSON specs (converted, each checked to normalise to the same fields); /spec.json still answers with the form compiled to JSON. - SKILL.md, the claude.ai sandbox skill, agent-install, README and CLAUDE.md teach the YAML form file; a test keeps SKILL.md's YAML valid. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
147 lines
4.7 KiB
Go
147 lines
4.7 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/GabrielVidal1/ask-form/examples"
|
|
"github.com/GabrielVidal1/ask-form/internal/form"
|
|
"github.com/GabrielVidal1/ask-form/internal/i18n"
|
|
)
|
|
|
|
func TestExamples(t *testing.T) {
|
|
list, err := LoadExamples(examples.FS)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(list) < 9 {
|
|
t.Fatalf("want at least 9 examples, got %d", len(list))
|
|
}
|
|
srv := httptest.NewServer(ExamplesHandler(map[string][]Example{i18n.EN: list}, ""))
|
|
defer srv.Close()
|
|
get := func(path string) (int, string) {
|
|
res, err := http.Get(srv.URL + path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, _ := io.ReadAll(res.Body)
|
|
return res.StatusCode, string(b)
|
|
}
|
|
if code, body := get("/examples"); code != 200 || strings.Count(body, `class="example-card"`) != len(list) {
|
|
t.Fatalf("index: %d", code)
|
|
}
|
|
for _, ex := range list {
|
|
if code, body := get("/examples/" + ex.Name); code != 200 || !strings.Contains(body, `action="/examples/`+ex.Name+`"`) {
|
|
t.Fatalf("%s page: %d", ex.Name, code)
|
|
}
|
|
if ex.Name == "new-project" {
|
|
if _, body := get("/examples/new-project"); !strings.Contains(body, "1 · skeleton") || !strings.Contains(body, "polished · 5") {
|
|
t.Fatal("new-project: slider end labels missing")
|
|
}
|
|
}
|
|
// The form file as written, and the same form as JSON (links to
|
|
// spec.json from before the examples were YAML still answer).
|
|
if code, body := get("/examples/" + ex.Name + "/spec.yaml"); code != 200 || !strings.HasPrefix(body, "title: ") {
|
|
t.Fatalf("%s spec.yaml: %d", ex.Name, code)
|
|
}
|
|
code, body := get("/examples/" + ex.Name + "/spec.json")
|
|
var sp struct {
|
|
Title string `json:"title"`
|
|
Fields []json.RawMessage `json:"fields"`
|
|
}
|
|
if code != 200 || json.Unmarshal([]byte(body), &sp) != nil || sp.Title != ex.Title {
|
|
t.Fatalf("%s spec.json: %d", ex.Name, code)
|
|
}
|
|
if fields, err := form.Normalize(mustJSON(t, sp.Fields)); err != nil || len(fields) != len(ex.State.Fields) {
|
|
t.Fatalf("%s spec.json: %v, %d fields, want %d", ex.Name, err, len(fields), len(ex.State.Fields))
|
|
}
|
|
// Every html block and mockup has its document; none may carry a
|
|
// script the frame CSP would have to allow. (A widget's may: it is
|
|
// the one frame that runs the agent's script.)
|
|
for _, f := range ex.State.Fields {
|
|
if f.Type == form.HTML || f.Type == form.Widget {
|
|
if code, _ := get("/examples/" + ex.Name + "/frame/" + f.Key); code != 200 {
|
|
t.Fatalf("%s frame %s: %d", ex.Name, f.Key, code)
|
|
}
|
|
}
|
|
for i, o := range f.Options {
|
|
if o.HTML != "" {
|
|
if code, _ := get("/examples/" + ex.Name + "/frame/" + f.Key + "/" + string(rune('0'+i))); code != 200 {
|
|
t.Fatalf("%s option frame %s/%d: %d", ex.Name, f.Key, i, code)
|
|
}
|
|
}
|
|
if strings.Contains(strings.ToLower(o.HTML), "<script") {
|
|
t.Errorf("%s: option %s carries a script", ex.Name, o.Value)
|
|
}
|
|
}
|
|
if f.Type != form.Widget && strings.Contains(strings.ToLower(f.HTML), "<script") {
|
|
t.Errorf("%s: block %s carries a script", ex.Name, f.Key)
|
|
}
|
|
}
|
|
// Send the defaults (plus every required question's first option):
|
|
// the result page shows the JSON, nothing is stored.
|
|
v := url.Values{}
|
|
for _, f := range ex.State.Fields {
|
|
if f.Display() {
|
|
continue
|
|
}
|
|
if f.Type == form.Widget {
|
|
// A widget's value is JSON: its default, else a value of its kind.
|
|
val := form.DefaultJSON(f)
|
|
if val == "" {
|
|
val = map[string]string{"": `"x"`, "string": `"x"`, "number": `1`, "boolean": `true`, "object": `{"a":1}`, "array": `[1]`}[f.Schema]
|
|
}
|
|
v.Set(form.InputName(f.Key), val)
|
|
continue
|
|
}
|
|
switch d := f.Default.(type) {
|
|
case string:
|
|
v.Set(form.InputName(f.Key), d)
|
|
case float64:
|
|
v.Set(form.InputName(f.Key), form.FormatNumber(d))
|
|
case bool:
|
|
if d {
|
|
v.Set(form.InputName(f.Key), "on")
|
|
}
|
|
}
|
|
if f.Required && v.Get(form.InputName(f.Key)) == "" {
|
|
if len(f.Options) > 0 {
|
|
v.Set(form.InputName(f.Key), f.Options[0].Value)
|
|
} else {
|
|
v.Set(form.InputName(f.Key), "x")
|
|
}
|
|
}
|
|
}
|
|
res, err := http.PostForm(srv.URL+"/examples/"+ex.Name, v)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, _ := io.ReadAll(res.Body)
|
|
// A multi form stays open, its answer in `responses`.
|
|
want := `"status": "submitted"`
|
|
if ex.State.Multi {
|
|
want = `"answerCount": 1`
|
|
}
|
|
if res.StatusCode != 200 || !strings.Contains(string(b), want) {
|
|
t.Fatalf("%s submit: %d %s", ex.Name, res.StatusCode, string(b)[:min(400, len(b))])
|
|
}
|
|
}
|
|
if code, _ := get("/examples/nope"); code != 404 {
|
|
t.Fatalf("unknown example: %d", code)
|
|
}
|
|
}
|
|
|
|
func mustJSON(t *testing.T, v any) json.RawMessage {
|
|
t.Helper()
|
|
b, err := json.Marshal(v)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return b
|
|
}
|