1package headless23import (4 "context"5 "encoding/base64"6 "testing"7 "time"89 "src.rest/cat/012/internal/notebook"10 "src.rest/cat/012/internal/sheet"11)1213// --notebooks runs a cell of several statements as the screen does: its14// own variable isn't a cell it reads, and a later cell reads it from its15// run.16func TestRunNotebookStatements(t *testing.T) {17 w := sheet.NewBook()18 nb, err := w.AddNotebook("Notebook", w.Sheet(0))19 if err != nil {20 t.Fatal(err)21 }22 src := "files = ls | where type == file # the files\n$files | where size > 1kb"23 nb.SetNotebookCells("add cells", []notebook.Cell{24 {Kind: notebook.Code, Source: "$files | length"},25 {Kind: notebook.Code, Source: src},26 })27 cmd, _, _ := notebook.Parse(src).Command()28 enc := func(s string) string { return base64.StdEncoding.EncodeToString([]byte(s)) }29 nu := fakeNu{30 cmd: "__out " + enc("[[name]; [b]]") + "\nfiles " + enc("[[name]; [a], [b]]") + "\n",31 "$files | length": "2",32 }33 if failed := RunNotebooks(context.Background(), w, NotebookOptions{Runner: nu, Timeout: time.Second}); len(failed) != 0 {34 t.Fatalf("failed: %q", failed)35 }36 cells := nb.NotebookCells()37 if o := w.Output(cells[1].ID); o == nil || string(o.NUON) != "[[name]; [b]]" || string(o.Vars["files"]) != "[[name]; [a], [b]]" {38 t.Errorf("the cell of two statements: %+v", o)39 }40 if o := w.Output(cells[0].ID); o == nil || string(o.NUON) != "2" || o.Reads["files"] == 0 {41 t.Errorf("the cell reading $files: %+v", o)42 }43}