askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_ai_attachments.py · 7.2 KB · 169 lines
| 1 | """Files dropped into the chat: classified, capped, and refused by name. |
| 2 | |
| 3 | askrobots had attach-a-file; the object server had the attachments |
| 4 | CAPABILITY (files on records) but no way to hand a file to the AI chat. |
| 5 | The split of labour that makes this testable: the server resolves file |
| 6 | ids to bytes (ownership, blob reads — all I/O), and object_ai classifies |
| 7 | and formats — pure, so everything below the endpoint runs with no data |
| 8 | directory and no network. |
| 9 | |
| 10 | THE GATE IS THE POINT. A binary the model cannot see must be refused |
| 11 | loudly at send time, not arrive as mojibake the model hallucinates an |
| 12 | interpretation of. That silent path is why classification is a gate |
| 13 | rather than a best-effort decoder. |
| 14 | """ |
| 15 | |
| 16 | import base64 |
| 17 | import json |
| 18 | |
| 19 | import pytest |
| 20 | |
| 21 | import object_ai |
| 22 | |
| 23 | |
| 24 | PNG = b"\x89PNG\r\n\x1a\n" + b"\x00" * 64 |
| 25 | |
| 26 | |
| 27 | # --- classification (pure) ------------------------------------------------------ |
| 28 | |
| 29 | def test_an_image_passes_through_as_base64(): |
| 30 | part = object_ai.attachment_part("chart.png", "image/png", PNG) |
| 31 | assert part["kind"] == "image" |
| 32 | assert part["media_type"] == "image/png" |
| 33 | assert base64.b64decode(part["data_b64"]) == PNG |
| 34 | |
| 35 | |
| 36 | def test_text_is_inlined_and_keeps_its_name(): |
| 37 | part = object_ai.attachment_part("notes.md", "text/markdown", b"# Plan\nship it") |
| 38 | assert part == {"kind": "text", "name": "notes.md", "text": "# Plan\nship it"} |
| 39 | |
| 40 | |
| 41 | def test_an_honest_text_file_under_a_lazy_mime_type_still_inlines(): |
| 42 | """Uploads arrive as application/octet-stream constantly. Bytes that |
| 43 | decode as UTF-8 are text whatever the header claims.""" |
| 44 | part = object_ai.attachment_part("data.txt", "application/octet-stream", |
| 45 | b"plain enough") |
| 46 | assert part["kind"] == "text" |
| 47 | |
| 48 | |
| 49 | def test_a_binary_is_refused_naming_what_is_supported(): |
| 50 | """THE gate. The refusal must name the supported set, because the |
| 51 | commonest next question is 'well what CAN I attach'.""" |
| 52 | with pytest.raises(object_ai.InvalidChatRequestError) as err: |
| 53 | object_ai.attachment_part("report.pdf", "application/pdf", |
| 54 | b"%PDF-1.7\x00\xff\xfe garbage") |
| 55 | message = str(err.value) |
| 56 | assert "report.pdf" in message |
| 57 | assert "image/png" in message |
| 58 | assert "extracted first" in message |
| 59 | |
| 60 | |
| 61 | def test_an_oversized_image_is_refused_with_both_numbers(): |
| 62 | big = b"x" * (object_ai.MAX_IMAGE_BYTES + 1) |
| 63 | with pytest.raises(object_ai.InvalidChatRequestError) as err: |
| 64 | object_ai.attachment_part("huge.png", "image/png", big) |
| 65 | assert str(object_ai.MAX_IMAGE_BYTES // 1024) in str(err.value) |
| 66 | |
| 67 | |
| 68 | def test_an_oversized_text_file_is_refused_suggesting_an_excerpt(): |
| 69 | big = b"a" * (object_ai.MAX_TEXT_BYTES + 1) |
| 70 | with pytest.raises(object_ai.InvalidChatRequestError) as err: |
| 71 | object_ai.attachment_part("dump.txt", "text/plain", big) |
| 72 | assert "excerpt" in str(err.value) |
| 73 | |
| 74 | |
| 75 | def test_the_per_turn_count_cap_is_enforced_in_run_chat(): |
| 76 | parts = [{"kind": "text", "name": f"f{i}", "text": "x"} |
| 77 | for i in range(object_ai.MAX_ATTACHMENTS_PER_TURN + 1)] |
| 78 | with pytest.raises(object_ai.InvalidChatRequestError) as err: |
| 79 | object_ai.run_chat( |
| 80 | send_http=lambda *a: (_ for _ in ()).throw(AssertionError("no call")), |
| 81 | dispatch_tool=lambda *a: None, |
| 82 | service="anthropic", model="m", key="k", |
| 83 | message="hello", attachments=parts) |
| 84 | assert str(object_ai.MAX_ATTACHMENTS_PER_TURN) in str(err.value) |
| 85 | |
| 86 | |
| 87 | # --- provider payload shapes ---------------------------------------------------- |
| 88 | |
| 89 | def capture_one_turn(service, attachments): |
| 90 | """Run one chat turn against a fake provider, returning the outbound |
| 91 | request body — the thing that must be shaped right, since the provider |
| 92 | is the one party we cannot test against.""" |
| 93 | captured = {} |
| 94 | |
| 95 | def send_http(url, headers, body): |
| 96 | captured["body"] = json.loads(body) |
| 97 | reply = ({"content": [{"type": "text", "text": "seen"}], "usage": {}} |
| 98 | if service == "anthropic" else |
| 99 | {"choices": [{"message": {"content": "seen"}}], "usage": {}}) |
| 100 | return 200, json.dumps(reply).encode() |
| 101 | |
| 102 | object_ai.run_chat( |
| 103 | send_http=send_http, dispatch_tool=lambda *a: None, |
| 104 | service=service, model="test-model", key="k", |
| 105 | message="what is in this file?", attachments=attachments) |
| 106 | return captured["body"] |
| 107 | |
| 108 | |
| 109 | def test_anthropic_gets_a_content_array_with_the_message_last(): |
| 110 | image = object_ai.attachment_part("pic.png", "image/png", PNG) |
| 111 | note = object_ai.attachment_part("note.txt", "text/plain", b"context here") |
| 112 | body = capture_one_turn("anthropic", [image, note]) |
| 113 | |
| 114 | content = body["messages"][0]["content"] |
| 115 | assert [c["type"] for c in content] == ["image", "text", "text"] |
| 116 | assert content[0]["source"]["media_type"] == "image/png" |
| 117 | assert content[1]["text"].startswith("[file: note.txt]\n") |
| 118 | assert content[-1]["text"] == "what is in this file?" |
| 119 | |
| 120 | |
| 121 | def test_openai_gets_data_urls(): |
| 122 | image = object_ai.attachment_part("pic.png", "image/png", PNG) |
| 123 | body = capture_one_turn("openai", [image]) |
| 124 | content = body["messages"][-1]["content"] |
| 125 | assert content[0]["type"] == "image_url" |
| 126 | assert content[0]["image_url"]["url"].startswith("data:image/png;base64,") |
| 127 | assert content[-1] == {"type": "text", "text": "what is in this file?"} |
| 128 | |
| 129 | |
| 130 | def test_a_chat_with_no_attachments_sends_a_plain_string(): |
| 131 | """The outbound payload of a text-only chat must be byte-identical to |
| 132 | what it always was — a content array where a string used to be is the |
| 133 | kind of change that breaks a provider-side cache or a logged-payload |
| 134 | consumer silently.""" |
| 135 | body = capture_one_turn("anthropic", None) |
| 136 | assert body["messages"][0]["content"] == "what is in this file?" |
| 137 | |
| 138 | |
| 139 | # --- the endpoint's ownership gate ---------------------------------------------- |
| 140 | |
| 141 | def test_the_ownership_refusal_says_why_sharing_does_not_apply(): |
| 142 | """v1 is strict owner-only, and the refusal explains the decision |
| 143 | rather than reciting 'forbidden': a share grants READ of a record, and |
| 144 | whether that extends to feeding the blob to an AI provider is a real |
| 145 | question deliberately not yet answered. Pinned as a source assertion |
| 146 | because the reasoning IS the contract.""" |
| 147 | import pathlib |
| 148 | source = pathlib.Path("object_server.py").read_text() |
| 149 | assert "Chat " in source and "attachments are owner-only for now" in source |
| 150 | assert "deliberately not yet" in source |
| 151 | assert "attachments=attachment_parts or None" in source |
| 152 | |
| 153 | |
| 154 | def test_the_shell_script_still_encodes_to_utf8(): |
| 155 | """Caught live, not by the original tests: emoji written as \\ud83d |
| 156 | \\udcce escape pairs inside the shell's plain triple-quoted _SCRIPT |
| 157 | string become LONE SURROGATES in the Python str — which compile fine, |
| 158 | pass ast checks, pass every JS-shape assertion, and then crash with |
| 159 | UnicodeEncodeError the moment the server encodes the page for HTTP. |
| 160 | A page that 500s on encode is invisible to every test that only |
| 161 | parses the source.""" |
| 162 | import importlib.util |
| 163 | spec = importlib.util.spec_from_file_location( |
| 164 | "shellmod", "packages/app-shell/objects/site/shell.py") |
| 165 | module = importlib.util.module_from_spec(spec) |
| 166 | spec.loader.exec_module(module) |
| 167 | module._SCRIPT.encode("utf-8") # raises if a surrogate survives |
| 168 | assert "pendingAttach" in module._SCRIPT |
| 169 | assert "attachments: attachIds.length" in module._SCRIPT |