| 1 | package sheets |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/base64" |
| 6 | "encoding/json" |
| 7 | "strings" |
| 8 | "sync" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/home/connect" |
| 12 | "amadan.net/oficina/sheets/internal/sheets/store" |
| 13 | ) |
| 14 | |
| 15 | func (f *sheetAgentFixture) credentials() *connect.Credentials { |
| 16 | keys, _ := json.Marshal(map[string]string{"key": base64.StdEncoding.EncodeToString(f.home.key.SignPub())}) |
| 17 | return &connect.Credentials{Issuer: homeTestIssuer, ConsumerID: "sheets-preview", KeysBase64: base64.StdEncoding.EncodeToString(keys), DEKBase64: base64.StdEncoding.EncodeToString(bytes.Repeat([]byte{81}, 32))} |
| 18 | } |
| 19 | func TestAgentSheetProductionConfiguration(t *testing.T) { |
| 20 | f := newSheetAgentFixture(t) |
| 21 | cfg := f.credentials() |
| 22 | if e := f.a.app.ConfigureAgentAccess(f.a.issuer, "blue", cfg); e != nil { |
| 23 | t.Fatal(e) |
| 24 | } |
| 25 | for _, pair := range [][2]string{{f.a.issuer, ""}, {"", "blue"}, {"http://insecure.example", "blue"}, {homeTestIssuer, "blue"}, {f.a.issuer, " blue"}} { |
| 26 | if e := f.a.app.ConfigureAgentAccess(pair[0], pair[1], cfg); e == nil { |
| 27 | t.Fatal("invalid enabled configuration", pair) |
| 28 | } |
| 29 | } |
| 30 | if e := f.a.app.ConfigureAgentAccess(f.a.issuer, "blue", nil); e == nil { |
| 31 | t.Fatal("enabled without Home") |
| 32 | } |
| 33 | for _, missing := range []string{"", "SHEETS_AGENT_ISSUER", "SHEETS_AGENT_TEAM", "SHEETS_HOME_ISSUER"} { |
| 34 | t.Run(missing, func(t *testing.T) { |
| 35 | env := goodEnv() |
| 36 | env["SHEETS_AGENT_ISSUER"] = f.a.issuer |
| 37 | env["SHEETS_AGENT_TEAM"] = "blue" |
| 38 | env["SHEETS_HOME_ISSUER"] = cfg.Issuer |
| 39 | env["SHEETS_HOME_CONSUMER_ID"] = cfg.ConsumerID |
| 40 | env["SHEETS_HOME_KEYS_BASE64"] = cfg.KeysBase64 |
| 41 | env["SHEETS_HOME_DEK_BASE64"] = cfg.DEKBase64 |
| 42 | if missing != "" { |
| 43 | delete(env, missing) |
| 44 | } |
| 45 | setEnv(t, env) |
| 46 | _, e := LoadAuthEnv(quietLogger()) |
| 47 | if (e == nil) != (missing == "") { |
| 48 | t.Fatal("environment acceptance", missing, e) |
| 49 | } |
| 50 | }) |
| 51 | } |
| 52 | } |
| 53 | func TestAgentSheetConcurrentFirstCreation(t *testing.T) { |
| 54 | f := newSheetAgentFixture(t) |
| 55 | input := initialSheetAgentInput() |
| 56 | start := make(chan struct{}) |
| 57 | responses := make(chan []byte, 6) |
| 58 | codes := make(chan int, 6) |
| 59 | var wg sync.WaitGroup |
| 60 | for i := 0; i < 6; i++ { |
| 61 | wg.Add(1) |
| 62 | go func() { |
| 63 | defer wg.Done() |
| 64 | <-start |
| 65 | rec := f.call(t, "sheets_create", input) |
| 66 | codes <- rec.Code |
| 67 | responses <- rec.Body.Bytes() |
| 68 | }() |
| 69 | } |
| 70 | close(start) |
| 71 | wg.Wait() |
| 72 | close(codes) |
| 73 | close(responses) |
| 74 | for code := range codes { |
| 75 | if code != 200 { |
| 76 | t.Fatal("concurrent initial request", code) |
| 77 | } |
| 78 | } |
| 79 | var first sheetAgentCreated |
| 80 | for raw := range responses { |
| 81 | var got sheetAgentCreated |
| 82 | if e := json.Unmarshal(raw, &got); e != nil { |
| 83 | t.Fatal(e) |
| 84 | } |
| 85 | if first.SheetID == "" { |
| 86 | first = got |
| 87 | } |
| 88 | if got != first || got.SheetID == "" { |
| 89 | t.Fatal("divergent first receipts", got, first) |
| 90 | } |
| 91 | } |
| 92 | for _, model := range []any{&store.Sheet{}, &store.Tab{}, &sheetAgentReceipt{}, &sheetAgentAttribution{}} { |
| 93 | var n int64 |
| 94 | if e := f.a.app.db.Model(model).Count(&n).Error; e != nil || n != 1 { |
| 95 | t.Fatal("duplicate first creation", n, e) |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | func TestAgentSheetInputAndResultLimits(t *testing.T) { |
| 100 | f := newSheetAgentFixture(t) |
| 101 | if rec := f.raw(t, "sheets_create", bytes.Repeat([]byte(" "), sheetAgentLimit+1)); rec.Code != 413 { |
| 102 | t.Fatal("oversize request", rec.Code) |
| 103 | } |
| 104 | input := initialSheetAgentInput() |
| 105 | input.Cells = nil |
| 106 | for i := 0; i < 20; i++ { |
| 107 | input.Cells = append(input.Cells, sheetAgentCell{Row: i, Column: 0, Raw: strings.Repeat("<", 4000)}) |
| 108 | } |
| 109 | made := decodeSheetCreated(t, f.call(t, "sheets_create", input)) |
| 110 | if got := decodeSheetCreated(t, f.call(t, "sheets_create", input)); got != made { |
| 111 | t.Fatal("large create not retryable") |
| 112 | } |
| 113 | args := f.rangeInput(made) |
| 114 | args["height"] = 20 |
| 115 | args["width"] = 1 |
| 116 | if rec := f.call(t, "sheets_get_range", args); rec.Code != 413 || strings.Contains(rec.Body.String(), "<<<<") { |
| 117 | t.Fatal("oversize result", rec.Code) |
| 118 | } |
| 119 | for _, change := range []map[string]any{{"height": 0}, {"width": 0}, {"height": 501}, {"height": 25, "width": 25}, {"row": 4999, "height": 2}, {"column": 599, "width": 2}} { |
| 120 | in := f.rangeInput(made) |
| 121 | for key, value := range change { |
| 122 | in[key] = value |
| 123 | } |
| 124 | if rec := f.call(t, "sheets_get_range", in); rec.Code != 422 { |
| 125 | t.Fatal("invalid range", change, rec.Code) |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |