okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

nekomimi.petapi: zstd responses, streams and request bodies; images served as raw bytes; drop redundant re-encodes7fd8cf5

main
3.3 KiB72 linesraw
1// A controlled peer supplies invalid offsets and false completeness claims.
2// The real daemon cannot deterministically violate its own paging contract.
3package daemon
4
5import (
6	"encoding/json"
7	"net/http"
8	"strings"
9	"testing"
10)
11
12func TestPagedContentValidatesOffsetsAndReconstructsUTF8(t *testing.T) {
13	for _, failure := range []string{"", "offset", "false completeness"} {
14		t.Run(failure, func(t *testing.T) {
15			imageReads := 0
16			image := map[string]any{"mime_type": "image/png", "width": 1, "height": 1, "original_bytes": 3, "reference": map[string]any{"url": "/sessions/s/history/u/image-0", "field": "image-0", "bytes": 3}}
17			conn := controlledConnection(t, func(w http.ResponseWriter, r *http.Request) {
18				if r.URL.Path == "/sessions/s/history" {
19					entry := func(id, kind string, position int, content ...any) map[string]any {
20						return map[string]any{"id": id, "kind": kind, "position": position, "created_at": nil, "input_id": nil, "turn_id": nil, "content_complete": false, "content": content, "tool": nil, "checkpoint_id": nil}
21					}
22					partial := entry("e", "assistant", 2, map[string]any{"kind": "reference", "reference": map[string]any{"url": "/sessions/s/history/e", "field": "text", "bytes": 6}})
23					partial["content_complete"] = failure == "false completeness"
24					complete := entry("full", "assistant", 3, map[string]any{"kind": "text", "text": strings.Repeat("complete reply ", 128)})
25					complete["content_complete"] = true
26					_ = json.NewEncoder(w).Encode(map[string]any{"items": []any{
27						entry("u", "user", 1, map[string]any{"kind": "text", "text": "inspect this image"}, map[string]any{"kind": "image", "image": image}),
28						partial, complete,
29					}, "older": nil, "newer": nil, "high_water": 3})
30					return
31				}
32				if strings.HasPrefix(r.URL.Path, "/sessions/s/history/u") {
33					imageReads++
34					return
35				}
36				if r.URL.Path != "/sessions/s/history/e" {
37					t.Errorf("wrong content URL %s", r.URL)
38				}
39				offset := 0
40				text := "雪"
41				complete := false
42				var next any = "next-a"
43				if r.URL.Query().Get("next") != "" {
44					offset = 3
45					text = "月"
46					complete = true
47					next = nil
48					if failure == "offset" {
49						offset = 1
50					}
51				}
52				_ = json.NewEncoder(w).Encode(map[string]any{"entry_id": "e", "parts": []any{map[string]any{"field": "text", "offset_bytes": offset, "encoding": "utf8", "text": text, "complete": complete}}, "next": next})
53			})
54			page, err := NewChatClient(conn, "s").History(t.Context(), 0, 10)
55			if failure != "" {
56				if err == nil {
57					t.Fatalf("invalid %s accepted", failure)
58				}
59				return
60			}
61			if err != nil || len(page.Events) != 6 || page.Events[0].Type != EventUser || page.Events[0].Text != "inspect this image" || len(page.Events[0].Images) != 1 || page.Events[0].Images[0].Bytes != 3 || page.Events[2].Type != EventMessage || page.Events[2].Text != "雪月" || page.Events[4].Type != EventMessage || page.Events[4].Text != strings.Repeat("complete reply ", 128) {
62				t.Fatalf("history lost full text or image metadata: %+v, %v", page, err)
63			}
64			if imageReads != 0 {
65				t.Fatal("displaying image metadata fetched the image")
66			}
67		})
68	}
69}
70
71// A malformed acknowledgement must retain the original login intent even when
72// the form is cleared or edited while its retained decision is being queried.