Pin the server contract, tokens and strings to server dev a25db24; app API 2

The contract moves from f5b3dbb to the server's dev at a25db24971a0: version.json says
{"app_api": 2}, and the server now writes its own show cards (tool_show.json: one from the tree,
one from a device) and the files routes (files_listing.json, file_content.json,
file_content_withheld.json). Tokens from the same commit change only in the header; the strings
regenerate unchanged.

- APP_API is 2, and it is now the floor as well as the ceiling: a protocol-3 server that still
  says app API 1 is a 2.0 build from before the show route and the files, and is refused as too
  old, as 1.x is.
- The files routes are modelled (FileEntry, FileListing, FileContent, Withheld) and ApiClient
  reads them: files(path), fileContent(path), and webFile(path) for the web UI's /files/view.
- shownText is documented against the server's real route, GET /api/v2/chats/{chat}/show with
  ?node=&version= or ?shown=, plain text up to 1 MB, 413 / 415 / 404; ClientTest pins the paths.
- The hand-written show cards leave ShowAndServerTest; ContractTest reads the server's.

Nothing in the app reads /api/v2/devices for its own token (devices() has no caller), so apps no
longer being listed there changes nothing here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
HomerandClaude Opus 5.5 committed 2026-11-03 14:48:03 +00:00
1 parent ac0c1c58e0
commit 0c5aa4d45f
15 files changed
+302 -57

No files matched your search

@@ -3,6 +3,7 @@ package eu.llembas.android
import eu.llembas.android.api.ApiClient
import eu.llembas.android.api.ApiException
import eu.llembas.android.api.CardAnswer
import eu.llembas.android.api.ShowCard
import eu.llembas.android.api.normaliseBase
import kotlinx.coroutines.test.runTest
import okhttp3.mockwebserver.MockResponse
@@ -61,4 +62,39 @@ class ClientTest {
val body = server.takeRequest().body.readUtf8()
assertTrue(body, body.contains(""""choices":{"q0":["Tea"]}"""))
}
@Test
fun aShownFileIsTheShowRouteAsPlainText() = runTest {
server.enqueue(MockResponse().setHeader("Content-Type", "text/plain; charset=utf-8").setBody("# Lembas\n"))
server.enqueue(MockResponse().setBody("x"))
val api = client()
val tree = ShowCard(path = "/home/frodo/recipe.md", nodeId = "node1", versionId = "ver1")
assertEquals("# Lembas\n", api.shownText("chat1", tree))
assertEquals("/api/v2/chats/chat1/show?node=node1&version=ver1", server.takeRequest().path)
api.shownText("chat1", ShowCard(path = "src/oven.py", source = "device", shownId = "shown1"))
assertEquals("/api/v2/chats/chat1/show?shown=shown1", server.takeRequest().path)
}
@Test
fun aShownFileTheServerWillNotGiveIsItsStatus() = runTest {
server.enqueue(MockResponse().setResponseCode(415).setBody("""{"detail":"This is not a text file, so it is not shown here."}"""))
try {
client().shownText("chat1", ShowCard(nodeId = "n", versionId = "v"))
fail("a 415 must throw")
} catch (e: ApiException) {
assertEquals(415, e.status)
assertEquals("This is not a text file, so it is not shown here.", e.message)
}
}
@Test
fun theFilesRoutesTakeThePathAsAQuery() = runTest {
server.enqueue(MockResponse().setBody("""{"path":"/home/frodo","parent":"/home","entries":[]}"""))
server.enqueue(MockResponse().setBody("""{"path":"/home/frodo/a b.md","name":"a b.md","text":"hi"}"""))
val api = client()
assertEquals("/home", api.files("/home/frodo").parent)
assertEquals("/api/v2/files?path=%2Fhome%2Ffrodo", server.takeRequest().path)
assertEquals("hi", api.fileContent("/home/frodo/a b.md").text)
assertEquals("/api/v2/files/content?path=%2Fhome%2Ffrodo%2Fa%20b.md", server.takeRequest().path)
}
}
@@ -1,8 +1,11 @@
package eu.llembas.android
import eu.llembas.android.api.APP_API
import eu.llembas.android.api.AskFrame
import eu.llembas.android.api.Chat
import eu.llembas.android.api.DoneFrame
import eu.llembas.android.api.FileContent
import eu.llembas.android.api.FileListing
import eu.llembas.android.api.JobsFrame
import eu.llembas.android.api.LembasJson
import eu.llembas.android.api.LiveReply
@@ -13,8 +16,12 @@ import eu.llembas.android.api.StatusFrame
import eu.llembas.android.api.SteeredFrame
import eu.llembas.android.api.StepsFrame
import eu.llembas.android.api.Tail
import eu.llembas.android.api.ToolCall
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.json.jsonObject
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
@@ -32,7 +39,59 @@ class ContractTest {
@Test
fun theVersionIsOneThisAppReads() {
val version = LembasJson.parseToJsonElement(text("version")).toString()
assertEquals("""{"app_api":1}""", version)
assertEquals("""{"app_api":$APP_API}""", version)
}
@Test
fun aShowCardFromTheTreeAndOneFromADevice() {
// The server's own `show` cards (`tool_show.json`): what the app reads them by, and the
// query that fetches the text from `GET /api/v2/chats/{chat}/show`.
val calls = LembasJson.parseToJsonElement(text("tool_show")).jsonObject.getValue("calls")
val (tree, device) = LembasJson.decodeFromJsonElement(ListSerializer(ToolCall.serializer()), calls)
val show = tree.show!!
assertEquals("show", tree.name)
assertEquals("recipe.md", show.name)
assertEquals("/home/frodo/recipe.md", show.path)
assertEquals("markdown", show.viewKind)
assertEquals(3, show.line)
assertEquals(120L, show.size)
assertEquals(listOf("node" to "node1", "version" to "ver1"), show.query)
val kept = device.show!!
assertEquals("device", kept.source)
assertEquals("code", kept.viewKind)
assertEquals("python", kept.language)
// `line: null` reads as no line, and the tree's two ids are empty on a device's card.
assertEquals(0, kept.line)
assertEquals(listOf("shown" to "shown1"), kept.query)
}
@Test
fun aDirectoryOfThePersonsFiles() {
val listing = read("files_listing", FileListing.serializer())
assertEquals("/home/frodo", listing.path)
assertEquals("/home", listing.parent)
assertEquals(listOf("recipes", "lembas.md"), listing.entries.map { it.name })
assertTrue(listing.entries[0].isFolder)
val file = listing.entries[1]
assertFalse(file.isFolder)
assertEquals(27L, file.size)
assertEquals("text/markdown", file.mime)
}
@Test
fun aFileAndAWithheldOne() {
val file = read("file_content", FileContent.serializer())
assertNull(file.withheld)
assertEquals("markdown", file.viewKind)
assertTrue(file.text.startsWith("# Lembas"))
val image = read("file_content_withheld", FileContent.serializer())
assertEquals("", image.text)
assertEquals("not_text", image.withheld!!.reason)
assertTrue(image.withheld!!.message.isNotBlank())
// An image is no kind the viewer draws, so if its text ever came it would be plain text.
assertEquals("text", image.viewKind)
}
@Test
@@ -10,47 +10,13 @@ import org.junit.Assert.assertNull
import org.junit.Test
/**
* The `show` card and the server gate. The card's shape is the server's own payload
* (`services/tools/files.py` `_run_show` for a tree file, `services/shown.py` `payload` for a
* device's) -- written out here because the server's contract fixtures do not carry a `show`
* call yet. When they do, this moves into `ContractTest`.
* What the server's contract fixtures cannot say: how the app reads a `show` card the server
* never wrote (an unknown kind), and which servers it works with at all. The server's own `show`
* cards are in `contract/tool_show.json` and read by `ContractTest`.
*/
class ShowAndServerTest {
private fun call(json: String) = LembasJson.decodeFromString(ToolCall.serializer(), json)
@Test
fun aTreeFileShowCard() {
val c = call(
"""
{"id": "call3", "name": "show", "status": "ok", "path": "/home/frodo/notes/road.md",
"show": {"path": "/home/frodo/notes/road.md", "name": "road.md", "kind": "markdown",
"language": "", "line": 12, "node_id": "n1", "version_id": "v7",
"sha": "ab12", "size": 2048, "mime": "text/markdown"},
"block": {"kind": "show", "title": "/home/frodo/notes/road.md"}}
""",
)
val show = c.show!!
assertEquals("road.md", show.name)
assertEquals("markdown", show.viewKind)
assertEquals(12, show.line)
assertEquals(listOf("node" to "n1", "version" to "v7"), show.query)
}
@Test
fun aDeviceFileShowCardNamesTheKeptCopy() {
val c = call(
"""
{"name": "show", "show": {"path": "src/main.rs", "name": "main.rs", "kind": "code",
"language": "rust", "line": 0, "node_id": "", "version_id": "", "sha": "cd34",
"size": 10, "mime": "text/plain", "source": "device", "shown_id": "s9",
"url": "/api/chats/chat1/shown/s9"}}
""",
)
assertEquals("device", c.show!!.source)
assertEquals(listOf("shown" to "s9"), c.show!!.query)
assertEquals("rust", c.show!!.language)
}
@Test
fun anUnknownKindIsPlainText() {
val c = call("""{"name": "show", "show": {"path": "x.svg", "kind": "svg"}}""")
@@ -61,17 +27,19 @@ class ShowAndServerTest {
private fun discovery(json: String) = LembasJson.decodeFromString(Discovery.serializer(), json)
@Test
fun onlyAServerOnTheTwoPointOhProtocolFits() {
fun onlyAServerOnTheTwoPointOhProtocolAndAppApiTwoFits() {
val two = """{"service": "lembas", "version": "2.0.0-beta.1", "protocol": 3, "protocols": [3],
"app_api": 1, "scopes": ["app", "models"]}"""
"app_api": 2, "scopes": ["app", "models"]}"""
assertEquals(ServerFit.FITS, fit(discovery(two)))
// A 2.0 `dev` server from before the show route and the files: protocol 3, app API 1.
assertEquals(ServerFit.TOO_OLD, fit(discovery(two.replace("\"app_api\": 2", "\"app_api\": 1"))))
// 1.7: protocol 2, the app API already there -- and still refused.
val old = """{"service": "lembas", "version": "1.7.3", "protocol": 2, "protocols": [1, 2],
"app_api": 1, "scopes": ["app", "models"]}"""
assertEquals(ServerFit.TOO_OLD, fit(discovery(old)))
// A server with no protocol at all is older still.
assertEquals(ServerFit.TOO_OLD, fit(discovery("""{"service": "lembas", "app_api": 1, "scopes": ["app"]}""")))
assertEquals(ServerFit.TOO_NEW, fit(discovery(two.replace("\"app_api\": 1", "\"app_api\": 2"))))
assertEquals(ServerFit.TOO_OLD, fit(discovery("""{"service": "lembas", "app_api": 2, "scopes": ["app"]}""")))
assertEquals(ServerFit.TOO_NEW, fit(discovery(two.replace("\"app_api\": 2", "\"app_api\": 3"))))
assertEquals(ServerFit.NOT_LEMBAS, fit(discovery("""{"service": "other", "protocol": 3}""")))
}
}