package main // P5a task 07: `sift fetch --pages` and `--grep`. import ( "encoding/json" "strings" "testing" ) func TestParseFetchOptsPDF(t *testing.T) { o, err := parseFetchOpts([]string{"https://example.org/t.pdf", "--pages", "41-60", "--grep", "attention head"}) if err != nil { t.Fatal(err) } if o.URL != "https://example.org/t.pdf" || o.Pages != "41-60" || o.Grep != "attention head" { t.Errorf("got %+v", o) } o, err = parseFetchOpts([]string{"--grep", "x", "--question", "why", "https://example.org/t.pdf"}) if err != nil || o.URL != "https://example.org/t.pdf" || o.Grep != "x" || o.Question != "why" { t.Errorf("flags before the URL: %+v %v", o, err) } if !strings.Contains(fetchUsage, "--pages") || !strings.Contains(fetchUsage, "--grep") { t.Errorf("usage lacks the new flags: %s", fetchUsage) } } func TestFetchBodyPDFFields(t *testing.T) { b, _ := json.Marshal(fetchBody(fetchOpts{URL: "https://example.org/a.pdf"})) if strings.Contains(string(b), `"pages"`) || strings.Contains(string(b), `"grep"`) { t.Errorf("unset pages/grep must not be sent: %s", b) } b, _ = json.Marshal(fetchBody(fetchOpts{URL: "https://example.org/a.pdf", Pages: "2-3", Grep: "fox"})) var m map[string]any _ = json.Unmarshal(b, &m) if m["pages"] != "2-3" || m["grep"] != "fox" || m["url"] != "https://example.org/a.pdf" || m["mode"] != "clean" { t.Errorf("body = %s", b) } b, _ = json.Marshal(fetchBody(fetchOpts{URL: "u", Question: "q"})) _ = json.Unmarshal(b, &m) if m["mode"] != "digest" || m["question"] != "q" || m["context"] != true { t.Errorf("question body = %s (mode must default to digest, context on)", b) } }