Scheduled on the server's real routes: runs in full, the models, the push
CI / check (push) Canceled after 0s

api/Schedules.kt and ApiClient now follow /api/v2 exactly (src/lembas/api/v2/schedules.py):
the schedule's enabled/description/request/rule, a run's model object, error class and
text, waiting and retry, preview; compile returns ok/reason and the rule in words; a run
in full (GET runs/{run}, Markdown through Markwon); the scheduling models (GET/PUT) with
a tick per model; /events keeps the list current while it is on screen.

Notifications are built from the payload notify.py sends (ids, status, private, actions,
ask): VISIBILITY_PRIVATE with a public version, Open always, Pause when the server offers
it, a tap opens the run (switching to the instance it came from).

ScheduleContractTest reads all eight fixtures and the push payload; shots for the run and
the models screen, fakes shaped like the fixtures.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
HomerandClaude Opus 5.5 committed 2026-11-09 13:34:18 +00:00
1 parent b52b6daaf0
commit 0982af8cfb
25 files changed
+1335 -208

No files matched your search

@@ -0,0 +1,205 @@
package eu.llembas.android
import eu.llembas.android.api.LembasJson
import eu.llembas.android.api.PushPayload
import eu.llembas.android.api.RulePreview
import eu.llembas.android.api.RunEnvelope
import eu.llembas.android.api.RunResult
import eu.llembas.android.api.Schedule
import eu.llembas.android.api.ScheduleCompiled
import eu.llembas.android.api.ScheduleEnvelope
import eu.llembas.android.api.ScheduleList
import eu.llembas.android.api.ScheduleRuns
import eu.llembas.android.api.SchedulesFrame
import eu.llembas.android.api.SchedulingModels
import eu.llembas.android.push.ScheduledNotice
import eu.llembas.android.ui.scheduled.SchedulingLogic
import kotlinx.serialization.KSerializer
import kotlinx.serialization.json.int
import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
/**
* The server's scheduling contract (`contract/schedule*.json`, `event_schedules.json`, written
* by its serialisers): every shape parses into this app's models and says what it said. And the
* push a scheduled run sends (services/schedule/notify.py `payload`), read into a notification.
*/
class ScheduleContractTest {
private fun text(name: String): String = requireNotNull(javaClass.classLoader!!.getResource("$name.json")) { "contract/$name.json is missing" }.readText()
private fun <T> read(name: String, serializer: KSerializer<T>): T = LembasJson.decodeFromString(serializer, text(name))
/** The schedule every fixture carries: sched1, "Waybread check", with run1 as its last run. */
private fun assertWaybread(s: Schedule) {
assertEquals("sched1", s.id)
assertEquals("Waybread check", s.title)
assertEquals("Check the waybread is wrapped.", s.instruction)
assertEquals("every second Monday at 7:30", s.request)
assertEquals("On the second Monday of each month at 07:30", s.description)
assertEquals("UTC", s.timezone)
assertTrue(s.enabled)
assertFalse(s.paused)
assertEquals("2026-01-12T07:30:00+00:00", s.nextRun)
assertEquals(5, s.nextRuns.size)
assertEquals("fresh", s.mode)
assertEquals("matters", s.notify)
assertEquals("one", s.catchUp)
assertNull(s.graceMinutes)
assertEquals("qwen", s.model)
assertEquals("high", s.effort)
assertNull(s.agents)
assertTrue("plan" in s.tools)
assertEquals("chat1", s.chatId)
assertEquals(3, s.firedCount)
assertEquals(0, s.failureStreak)
assertFalse(s.running)
// The rule is the server's own form, kept whole to be handed back.
val nth = s.rule.getValue("at").jsonObject.getValue("nth").jsonArray.single().jsonObject
assertEquals(2, nth.getValue("n").jsonPrimitive.int)
val run = s.lastRun!!
assertEquals("run1", run.id)
assertEquals("sched1", run.scheduleId)
assertEquals("ok", run.status)
assertEquals("late", run.timing)
assertEquals("GPT-OSS", run.model.label)
assertEquals("switched", run.model.reason)
assertEquals("Qwen", run.model.switchedFrom)
assertEquals(1200L, run.tokens.prompt)
assertNull(run.cost)
assertNull(run.error)
assertEquals("msg2", run.messageId)
assertEquals("All the waybread is wrapped.", run.preview)
assertTrue(run.notified)
}
@Test
fun theListOneScheduleAndTheEventFrame() {
assertWaybread(read("schedules", ScheduleList.serializer()).schedules.single())
assertWaybread(read("schedule", ScheduleEnvelope.serializer()).schedule)
val frame = read("event_schedules", SchedulesFrame.serializer())
assertEquals("597aab1ff7b62194", frame.version)
assertWaybread(frame.schedules.single())
}
@Test
fun theRunsAWaitingOneAndOneInFull() {
val page = read("schedule_runs", ScheduleRuns.serializer())
assertFalse(page.more)
val (waiting, done) = page.runs
assertEquals("waiting", waiting.status)
assertEquals("model", waiting.waitingFor)
assertEquals("2026-01-02T03:09:05+00:00", waiting.retryAt)
assertEquals(2, waiting.missed)
assertEquals(2, waiting.attempts)
assertNull(waiting.model.label)
assertNull(waiting.chatId)
assertEquals("no_model", waiting.error?.kind)
assertEquals("Waiting for a free model: Qwen: it is answering someone.", waiting.error?.text)
assertEquals("run1", done.id)
val one = read("schedule_run", RunResult.serializer())
assertEquals("run1", one.run.id)
assertEquals("chat1", one.run.chatId)
assertEquals("All the waybread is *wrapped*.", one.text)
// `POST /schedules/{id}/run` answers 202 with the run alone.
val started = LembasJson.decodeFromString(RunEnvelope.serializer(), """{"run":{"id":"run3","status":"running","timing":"manual"}}""")
assertEquals("manual", started.run.timing)
}
@Test
fun theCompileAndThePreview() {
val compiled = read("schedule_compile", ScheduleCompiled.serializer())
assertTrue(compiled.ok)
assertNull(compiled.reason)
assertEquals("Waybread check", compiled.title)
assertEquals("chat", compiled.target)
assertEquals("On the second Monday of each month at 07:30", compiled.description)
assertEquals(5, compiled.nextRuns.size)
assertTrue(compiled.savable)
// A rule that cannot run comes back empty, in no words, with no runs: not savable.
val preview = read("schedule_preview", RulePreview.serializer())
assertTrue(preview.rule.isEmpty())
assertNull(preview.description)
assertTrue(preview.nextRuns.isEmpty())
assertFalse(ScheduleCompiled(ok = false, reason = "Say what you want to happen.", instruction = "x").savable)
}
@Test
fun theSchedulingModelsAndANarrowing() {
val models = read("schedule_models", SchedulingModels.serializer())
val (gpt, qwen) = models.models
assertEquals("gpt-oss", gpt.id)
assertFalse(gpt.free)
assertEquals("it is answering someone", gpt.busy)
assertTrue(gpt.chosen)
assertTrue(qwen.free)
assertNull(qwen.busy)
assertFalse(qwen.chosen)
assertEquals("/api/v2/models/images/0a1b2c3d4e5f.png", qwen.image)
assertEquals(listOf("gpt-oss", "qwen"), models.instance)
assertEquals(listOf("gpt-oss"), models.mine)
// Ticking Qwen ticks every one: no narrowing at all. Unticking the last one: refused.
assertEquals(emptyList<String>(), SchedulingLogic.toggled(models.models, "qwen"))
assertNull(SchedulingLogic.toggled(models.models, "gpt-oss"))
val all = models.models.map { it.copy(chosen = true) }
assertEquals(listOf("gpt-oss"), SchedulingLogic.toggled(all, "qwen"))
}
@Test
fun aResultsPushOpensTheRun() {
// What notify.payload and push.announce send for a run that finished: ids, a fixed
// sentence, never the result.
val payload = LembasJson.decodeFromString(
PushPayload.serializer(),
"""{"schedule_id":"sched1","run_id":"run1","chat_id":"chat1","status":"ok","visibility":"private",
"actions":["open"],"ask":"","pause_label":"Pause","title":"LLeMbas",
"body":"A scheduled task has finished.","url":"/scheduled/sched1?run=run1","kind":"schedule"}""",
)
val notice = ScheduledNotice.from(payload)!!
assertEquals("sched1", notice.scheduleId)
assertEquals("run1", notice.runId)
assertFalse(notice.failed)
assertTrue(notice.open)
assertFalse(notice.pause)
assertEquals("private", payload.visibility)
}
@Test
fun aFailurePushOffersPauseAndAsksAfterThree() {
val failed = LembasJson.decodeFromString(
PushPayload.serializer(),
"""{"schedule_id":"sched1","run_id":"run2","status":"failed","visibility":"private","actions":["open","pause"],
"ask":"pause","body":"A scheduled task failed 3 times in a row. Pause it?","url":"/scheduled/sched1?run=run2",
"kind":"schedule_failed"}""",
)
val notice = ScheduledNotice.from(failed)!!
assertTrue(notice.failed)
assertTrue(notice.askPause)
assertTrue(notice.pause)
// A push about the same run replaces its notification; another run gets its own.
assertEquals(notice.id, notice.copy(failed = false).id)
assertTrue(notice.id != notice.copy(runId = "run3").id)
}
@Test
fun aPushWithOnlyItsUrlAndOneThatIsNotScheduled() {
val bare = PushPayload(kind = "schedule", url = "/scheduled/sched1?run=run1")
val notice = ScheduledNotice.from(bare)!!
assertEquals("sched1", notice.scheduleId)
assertEquals("run1", notice.runId)
assertTrue(notice.open)
assertNull(ScheduledNotice.from(PushPayload(kind = "reply", chatId = "chat1")))
assertNull(ScheduledNotice.from(PushPayload(kind = "schedule", url = "/chat/chat1")))
assertEquals(null to null, ScheduledNotice.fromUrl("/elsewhere"))
}
}
@@ -6,6 +6,7 @@ import eu.llembas.android.ui.scheduled.ScheduleLogic
import eu.llembas.android.ui.scheduled.Tone
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
import java.time.ZoneId
import java.util.Locale
@@ -38,8 +39,11 @@ class ScheduleLogicTest {
@Test
fun aScheduleListThatSaysLittleStillParses() {
val list = LembasJson.decodeFromString(ScheduleList.serializer(), """{"schedules":[{"id":"s1","paused":true,"later":1}]}""")
assertEquals("s1", list.schedules.single().id)
assertNull(list.schedules.single().nextRun)
val list = LembasJson.decodeFromString(ScheduleList.serializer(), """{"schedules":[{"id":"s1","enabled":false,"model":null,"later":1}]}""")
val one = list.schedules.single()
assertEquals("s1", one.id)
assertTrue(one.paused)
assertNull(one.nextRun)
assertNull(one.model)
}
}
@@ -10,9 +10,15 @@ import eu.llembas.android.api.LiveReply
import eu.llembas.android.api.Message
import eu.llembas.android.api.Metrics
import eu.llembas.android.api.ModelList
import eu.llembas.android.api.RunError
import eu.llembas.android.api.RunModel
import eu.llembas.android.api.RunResult
import eu.llembas.android.api.RunTokens
import eu.llembas.android.api.Schedule
import eu.llembas.android.api.SchedulePreview
import eu.llembas.android.api.ScheduleCompiled
import eu.llembas.android.api.ScheduleRun
import eu.llembas.android.api.SchedulingModel
import eu.llembas.android.api.SchedulingModels
import eu.llembas.android.api.Step
import eu.llembas.android.api.Tail
import eu.llembas.android.api.ToolBlock
@@ -23,9 +29,12 @@ import eu.llembas.android.ui.chat.ChatState
import eu.llembas.android.ui.chat.Draft
import eu.llembas.android.ui.home.ChatsState
import eu.llembas.android.ui.scheduled.EditState
import eu.llembas.android.ui.scheduled.ModelsState
import eu.llembas.android.ui.scheduled.RunState
import eu.llembas.android.ui.scheduled.ScheduleState
import eu.llembas.android.ui.scheduled.ScheduledState
import eu.llembas.android.ui.settings.SettingsState
import kotlinx.serialization.json.jsonObject
import java.time.LocalDate
/** Made-up data for the screenshots: the server's own contract files where they fit. */
@@ -141,8 +150,25 @@ object Fakes {
selfUpdate = true,
)
private val ok =
ScheduleRun(id = "r1", dueAt = "2026-03-10T07:30:00+00:00", startedAt = "2026-03-10T07:30:02+00:00", status = "ok", model = "qwen", chatId = "c9")
// Scheduled: shaped as the server's contract fixtures are (contract/schedule*.json).
private fun rule(text: String) = LembasJson.parseToJsonElement(text).jsonObject
private val ok = ScheduleRun(
id = "r1",
scheduleId = "s1",
status = "ok",
timing = "on_time",
dueAt = "2026-03-10T07:30:00+00:00",
startedAt = "2026-03-10T07:30:02+00:00",
finishedAt = "2026-03-10T07:31:40+00:00",
attempts = 1,
model = RunModel(id = "qwen", name = "Qwen", reason = "pinned"),
tokens = RunTokens(prompt = 1200, completion = 340),
chatId = "c9",
messageId = "m9",
preview = "1. Two new offshore wind farms approved in the North Sea. 2. Solar output hit a March record in Spain.",
notified = true,
)
private val nextRuns = listOf(11, 12, 13, 16, 17).map { "2026-03-${it}T07:30:00+00:00" }
val schedules = listOf(
@@ -150,71 +176,137 @@ object Fakes {
id = "s1",
title = "Morning news on renewables",
instruction = "Sum up the news about renewable energy from the last day, in five points.",
rule = "Every weekday at 07:30",
request = "Every weekday at 7:30, sum up the news about renewable energy in five points",
rule = rule("""{"at":{"weekdays":[0,1,2,3,4],"times":["07:30"]}}"""),
description = "Every weekday at 07:30",
timezone = "Europe/Bratislava",
nextRun = nextRuns.first(),
nextRuns = nextRuns,
lastRun = ok,
model = "qwen",
mode = "fresh",
notify = "matters",
model = "qwen",
chatId = "c9",
firedCount = 41,
lastRun = ok,
),
Schedule(
id = "s2",
title = "Check the garden sensors",
instruction = "Read the sensors and tell me if the soil is too dry.",
rule = "Every 3 hours",
rule = rule("""{"every":{"hours":3}}"""),
description = "Every 3 hours",
nextRun = "2026-03-10T12:00:00+00:00",
nextRuns = listOf("2026-03-10T12:00:00+00:00"),
lastRun = ScheduleRun(id = "r2", status = "silent"),
mode = "continue",
notify = "matters",
running = true,
lastRun = ScheduleRun(id = "r2", scheduleId = "s2", status = "silent", timing = "on_time"),
),
Schedule(
id = "s3",
title = "Weekly backup report",
instruction = "Summarise last week's backups and anything that failed.",
rule = "Every Monday at 09:00",
paused = true,
lastRun = ScheduleRun(id = "r3", status = "failed", error = "The model could not be reached."),
description = "Every Monday at 09:00",
enabled = false,
failureStreak = 3,
lastRun = ScheduleRun(
id = "r3",
scheduleId = "s3",
status = "failed",
error = RunError(kind = "unreachable", text = "The model could not be reached."),
),
),
Schedule(
id = "s4",
title = "Dentist",
instruction = "Remind me to call the dentist.",
rule = "Once, on Thursday at 10:00",
description = "Once, on Thursday 12 March at 10:00",
nextRun = "2026-03-12T09:00:00+00:00",
nextRuns = listOf("2026-03-12T09:00:00+00:00"),
lastRun = ScheduleRun(id = "r4", status = "waiting"),
notify = "always",
lastRun = ScheduleRun(
id = "r4",
scheduleId = "s4",
status = "waiting",
waitingFor = "model",
retryAt = "2026-03-10T07:35:00+00:00",
attempts = 2,
error = RunError(kind = "no_model", text = "Waiting for a free model: Qwen: it is answering someone."),
),
),
)
val scheduled = ScheduledState(schedules = schedules)
private val switched = ScheduleRun(
id = "r0",
scheduleId = "s1",
status = "ok",
timing = "late",
dueAt = "2026-03-09T07:30:00+00:00",
startedAt = "2026-03-09T07:41:00+00:00",
model = RunModel(id = "gpt-oss", name = "GPT-OSS", reason = "switched", switchedFrom = "Qwen"),
chatId = "c8",
preview = "Little happened over the weekend: one grid-storage tender in Austria.",
)
val schedule = ScheduleState(
schedule = schedules[0],
runs = listOf(
ok,
ScheduleRun(
id = "r0",
startedAt = "2026-03-09T07:41:00+00:00",
status = "ok",
model = "gpt-oss",
modelReason = "switched: qwen busy",
chatId = "c8",
),
ScheduleRun(id = "r-1", dueAt = "2026-03-06T07:30:00+00:00", status = "silent", model = "qwen"),
ScheduleRun(id = "r-2", dueAt = "2026-03-05T07:30:00+00:00", status = "missed", missed = 2),
switched,
ScheduleRun(id = "r-1", scheduleId = "s1", dueAt = "2026-03-06T07:30:00+00:00", status = "silent", model = RunModel(id = "qwen", name = "Qwen")),
ScheduleRun(id = "r-2", scheduleId = "s1", dueAt = "2026-03-05T07:30:00+00:00", status = "missed", missed = 2),
ScheduleRun(
id = "r-3",
scheduleId = "s1",
startedAt = "2026-03-04T07:30:00+00:00",
status = "failed",
model = "qwen",
error = "The model stopped answering after 10 minutes.",
model = RunModel(id = "qwen", name = "Qwen"),
error = RunError(kind = "timeout", text = "The model stopped answering after 10 minutes."),
),
),
)
val run = RunState(
title = "Morning news on renewables",
result = RunResult(
run = switched,
text = """
|Little happened over the weekend. **One thing** worth knowing:
|
|1. Austria opened a tender for **200 MW** of grid storage, bids due in May.
|2. A [report](https://example.org/report) puts Europe's March solar output 18 % above last year.
|
|```text
|storage tender: 200 MW, deadline 2026-05-15
|```
""".trimMargin(),
),
)
val schedulingModels = ModelsState(
models = SchedulingModels(
models = listOf(
SchedulingModel(id = "gpt-oss", name = "GPT-OSS", free = false, busy = "it is answering someone", chosen = true),
SchedulingModel(id = "qwen", name = "Qwen", free = true, chosen = true),
SchedulingModel(id = "gemma", name = "Gemma 3 27B", free = true, chosen = false),
),
instance = listOf("gpt-oss", "qwen", "gemma"),
mine = listOf("gpt-oss", "qwen"),
),
)
val editing = EditState(
words = "Every weekday at 7:30, sum up the news about renewable energy in five points",
preview = SchedulePreview(
compiled = ScheduleCompiled(
ok = true,
title = "Morning news on renewables",
ruleText = "Every weekday at 07:30",
instruction = "Sum up the news about renewable energy from the last day, in five points.",
rule = rule("""{"at":{"weekdays":[0,1,2,3,4],"times":["07:30"]}}"""),
description = "Every weekday at 07:30",
nextRuns = nextRuns,
timezone = "Europe/Bratislava",
),
)
}
@@ -23,9 +23,11 @@ import eu.llembas.android.ui.chat.TuneContent
import eu.llembas.android.ui.chat.modelId
import eu.llembas.android.ui.home.DrawerActions
import eu.llembas.android.ui.home.DrawerContent
import eu.llembas.android.ui.scheduled.RunContent
import eu.llembas.android.ui.scheduled.ScheduleContent
import eu.llembas.android.ui.scheduled.ScheduleEditContent
import eu.llembas.android.ui.scheduled.ScheduledContent
import eu.llembas.android.ui.scheduled.SchedulingModelsContent
import eu.llembas.android.ui.settings.SettingsActions
import eu.llembas.android.ui.settings.SettingsContent
import eu.llembas.android.ui.theme.LocalPalette
@@ -118,6 +120,15 @@ class ScreenShots {
@Test
fun scheduleEdit() = shoot("11-schedule-edit") { ScheduleEditContent(Fakes.editing, editing = false) }
@Test
fun scheduleRun() {
shoot("12-schedule-run") { RunContent(Fakes.run) }
shoot("12-schedule-run", Phone.Small) { RunContent(Fakes.run) }
}
@Test
fun schedulingModels() = shoot("13-scheduling-models") { SchedulingModelsContent(Fakes.schedulingModels) }
}
/**