-
Notifications
You must be signed in to change notification settings - Fork 44
[AIT-767] uts: live objects ground work #1209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ttypic
wants to merge
10
commits into
main
Choose a base branch
from
AIT-767/live-object-ground-work
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ee117f0
uts: add basic unit test for `ClientOptions` and setup test module
ttypic d4271e2
uts: introduce mock HTTP and WebSocket engines for testing
ttypic 4c80813
uts: extend WebSocket mock with connection lifecycle events and messa…
ttypic fc8461c
uts: introduce `Clock` abstraction for time operations and testability
ttypic 8317619
uts: add `uts-to-kotlin` skill for translating UTS pseudocode to Kotl…
ttypic 7408981
uts: first UTS generation attempt
ttypic 8b95ba0
uts: add outgoing frame inspection and extend `MockWebSocket` with me…
ttypic 4fe5784
uts: add support for query parameter parsing and assertions in mock c…
ttypic c9d79f2
uts: add inline documentation for fake clocks, HTTP, and WebSocket mocks
ttypic cdf3a7c
uts: updated skill to reference spec guide for writing tests
ttypic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,356 @@ | ||
| --- | ||
| description: "Translate a UTS pseudocode test spec into Kotlin tests in the uts module. Usage: /uts-to-kotlin <path-to-spec-file>" | ||
| allowed-tools: Bash, Read, Edit, Write | ||
| --- | ||
|
|
||
| Translate the UTS pseudocode test spec at `$ARGUMENTS` into a runnable Kotlin test in the `uts` module. | ||
|
|
||
| Reference: [Writing Derived Tests](https://github.com/ably/specification/blob/main/uts/docs/writing-derived-tests.md) | ||
|
|
||
| --- | ||
|
|
||
| ## Step 1 — Read the spec | ||
|
|
||
| Read the file at `$ARGUMENTS`. Identify: | ||
| - All test cases — each has a structured ID like `realtime/unit/RSA4c2/callback-error-connecting-disconnected-0` and a description | ||
| - The protocol used (WebSocket for Realtime, HTTP for REST) | ||
| - Any timer usage (`enable_fake_timers`, `ADVANCE_TIME`) | ||
|
|
||
| --- | ||
|
|
||
| ## Step 2 — Determine output path and package | ||
|
|
||
| Map the spec path to a test path: | ||
|
|
||
| | Spec location | Test location | | ||
| |---|---| | ||
| | `.../uts/test/rest/unit/<name>.md` | `uts/src/test/kotlin/io/ably/lib/rest/unit/<Name>Test.kt` | | ||
| | `.../uts/test/realtime/unit/<sub>/<name>.md` | `uts/src/test/kotlin/io/ably/lib/realtime/unit/<sub>/<Name>Test.kt` | | ||
|
|
||
| Class name: take the file name, strip `_test` suffix, convert `snake_case` → `PascalCase`, append `Test`. | ||
|
|
||
| Example: `connection_state_machine_test.md` → `ConnectionStateMachineTest` | ||
|
|
||
| Package: derived from the output path under `kotlin/`. | ||
|
|
||
| --- | ||
|
|
||
| ## Step 3 — Read infrastructure files | ||
|
|
||
| Read ALL of these before generating any code (you need exact method signatures): | ||
|
|
||
| ``` | ||
| uts/src/test/kotlin/io/ably/lib/ClientFactories.kt | ||
| uts/src/test/kotlin/io/ably/lib/test/mock/MockWebSocket.kt | ||
| uts/src/test/kotlin/io/ably/lib/test/mock/MockHttpClient.kt | ||
| uts/src/test/kotlin/io/ably/lib/test/mock/PendingRequest.kt | ||
| uts/src/test/kotlin/io/ably/lib/test/mock/PendingConnection.kt | ||
| uts/src/test/kotlin/io/ably/lib/test/mock/FakeClock.kt | ||
| uts/src/test/kotlin/io/ably/lib/test/mock/MockEvent.kt | ||
| ``` | ||
|
ttypic marked this conversation as resolved.
|
||
|
|
||
| --- | ||
|
|
||
| ## Step 4 — Generate the Kotlin test file | ||
|
|
||
| Apply the translation rules below, then write the file. | ||
|
|
||
| ### Client construction | ||
|
|
||
| Use `TestRealtimeClient { }` and `TestRestClient { }` from `io.ably.lib`. These are DSL builders that extend `DebugOptions` — all `ClientOptions` fields (`autoConnect`, `recover`, `disconnectedRetryTimeout`, etc.) are settable directly inside the block. The default API key is `"appId.keyId:keySecret"`. | ||
|
|
||
| | Pseudocode | Kotlin | | ||
| |---|---| | ||
| | `Rest(options: ClientOptions(key: "..."))` | `TestRestClient { key = "..." }` | | ||
| | `Realtime(options: ClientOptions(key: "...", autoConnect: false))` | `TestRealtimeClient { key = "..."; autoConnect = false }` | | ||
| | `Realtime(options: ClientOptions(autoConnect: false))` | `TestRealtimeClient { autoConnect = false }` | | ||
| | Installing mocks | `install(mock)` inside the builder block | | ||
| | Fake timers | Create `val fakeClock = FakeClock()` before the builder, then call `enableFakeTimers(fakeClock)` inside | | ||
|
|
||
| ### Mock setup | ||
|
|
||
| **Prefer `onConnectionAttempt` callback** over `launch { awaitConnectionAttempt() }` for straightforward connections. Use `respondWithSuccess(message)` to open the socket and deliver the CONNECTED message in a single call. | ||
|
|
||
| ```kotlin | ||
| val mock = MockWebSocket { | ||
| onConnectionAttempt = { conn -> | ||
| conn.respondWithSuccess(ProtocolMessage().apply { | ||
| action = ProtocolMessage.Action.connected | ||
| connectionId = "test-connection-id" | ||
| connectionDetails = ConnectionDetails { | ||
| connectionKey = "test-key" | ||
| maxIdleInterval = 15_000L | ||
| connectionStateTtl = 120_000L | ||
| } | ||
| }) | ||
| } | ||
| } | ||
| val client = TestRealtimeClient { | ||
| autoConnect = false | ||
| install(mock) | ||
| } | ||
| ``` | ||
|
|
||
| **Use `awaitConnectionAttempt()` only** when different connection attempts need different behaviour (e.g. first attempt succeeds, subsequent ones are refused). In that case, set up the initial connection in a `launch` block before calling `connect()`, then handle reconnections separately: | ||
|
|
||
| ```kotlin | ||
| val mockWs = MockWebSocket() | ||
| val client = TestRealtimeClient { | ||
| autoConnect = false | ||
| install(mockWs) | ||
| } | ||
|
|
||
| launch { | ||
| mockWs.awaitConnectionAttempt().respondWithSuccess(ProtocolMessage().apply { ... }) | ||
| } | ||
|
|
||
| client.connect() | ||
| awaitState(client, ConnectionState.connected) | ||
|
|
||
| // handle reconnection attempts differently | ||
| val refuseJob = launch { | ||
| repeat(10) { | ||
| fakeClock.advance(2.seconds) | ||
| mockWs.awaitConnectionAttempt().respondWithRefused() | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| For HTTP: | ||
| ```kotlin | ||
| val mockHttp = MockHttpClient { onRequest = { req -> req.respondWith(200, body) } } | ||
| val client = TestRestClient { install(mockHttp) } | ||
| ``` | ||
|
|
||
| ### Inspecting outgoing frames | ||
|
|
||
| `ClientOptionsBuilder` sets `useBinaryProtocol = false`, so the SDK sends JSON text frames. Every outgoing frame is captured as `MockEvent.MessageFromClient` and queued in `awaitNextMessageFromClient()`. | ||
|
|
||
| To assert on a message the client sent: | ||
|
|
||
| ```kotlin | ||
| // After triggering the SDK action (e.g. attach): | ||
| channelOne.attach() | ||
| val msg = mock.awaitNextMessageFromClient() | ||
| assertEquals(ProtocolMessage.Action.attach, msg.action) | ||
| assertEquals("channel-one", msg.channel) | ||
| assertEquals("expected-serial", msg.channelSerial) | ||
|
|
||
| // Or filter the full event log when order doesn't matter: | ||
| val sent = mock.events | ||
| .filterIsInstance<MockEvent.MessageFromClient>() | ||
| .firstOrNull { it.message.action == ProtocolMessage.Action.attach && it.message.channel == "channel-one" } | ||
| assertNotNull(sent) | ||
| assertEquals("expected-serial", sent!!.message.channelSerial) | ||
| ``` | ||
|
|
||
| ### Mock method reference | ||
|
|
||
| | Pseudocode | Kotlin | | ||
| |---|---| | ||
| | `conn.respond_with_success()` | `conn.respondWithSuccess()` | | ||
| | `conn.respond_with_success(msg)` | `conn.respondWithSuccess(msg)` | | ||
| | `conn.respond_with_refused()` | `conn.respondWithRefused()` | | ||
| | `conn.respond_with_timeout()` | `conn.respondWithTimeout()` | | ||
| | `conn.respond_with_dns_error()` | `conn.respondWithDnsError()` | | ||
| | `mock_ws.send_to_client(msg)` | `mock.sendToClient(msg)` | | ||
| | `mock_ws.send_to_client_and_close(msg)` | `mock.sendToClientAndClose(msg)` | | ||
| | `mock_ws.simulate_disconnect()` | `mock.simulateDisconnect()` | | ||
| | `req.respond_with(200, {...})` | `req.respondWith(200, mapOf(...))` | | ||
| | `req.respond_with_timeout()` | `req.respondWithTimeout()` | | ||
|
|
||
| ### Protocol messages and types | ||
|
|
||
| | Pseudocode | Kotlin | | ||
| |---|---| | ||
| | `ProtocolMessage(action: CONNECTED, ...)` | `ProtocolMessage().apply { action = ProtocolMessage.Action.connected; ... }` | | ||
| | `CONNECTED` / `DISCONNECTED` / `ERROR` / `HEARTBEAT` / `ATTACH` / `DETACHED` | `.connected` / `.disconnected` / `.error` / `.heartbeat` / `.attach` / `.detached` | | ||
| | `ErrorInfo(code: X, statusCode: Y, message: "...")` | `ErrorInfo("...", Y, X)` — arg order: message, statusCode, code | | ||
| | `ConnectionDetails(connectionKey: ..., maxIdleInterval: ..., connectionStateTtl: ...)` | `ConnectionDetails { connectionKey = "..."; maxIdleInterval = ...; connectionStateTtl = ... }` | | ||
| | `ConnectionState.connected` etc. | `ConnectionState.connected`, `.disconnected`, `.suspended`, `.failed`, `.connecting`, `.closing`, `.closed` | | ||
|
|
||
| ### Awaiting state | ||
|
|
||
| `AWAIT_STATE client.connection.state == ConnectionState.X` → use the top-level `awaitState()` helper: | ||
|
|
||
| ```kotlin | ||
| awaitState(client, ConnectionState.x) // default 5s timeout | ||
| awaitState(client, ConnectionState.x, 10.seconds) | ||
| ``` | ||
|
|
||
| For channels: `awaitChannelState(channel, ChannelState.attached)`. | ||
|
|
||
| ### Timer control | ||
|
|
||
| ```kotlin | ||
| val fakeClock = FakeClock() | ||
| val client = TestRealtimeClient { | ||
| autoConnect = false | ||
| install(mock) | ||
| enableFakeTimers(fakeClock) | ||
| } | ||
|
|
||
| // Advance time — timer callbacks fire synchronously within advance() | ||
| fakeClock.advance(30_000) | ||
| // or | ||
| fakeClock.advance(30.seconds) | ||
| ``` | ||
|
|
||
| After `fakeClock.advance()` inside a coroutine, yield to let newly dispatched coroutines run: | ||
|
|
||
| ```kotlin | ||
| fakeClock.advance(30.seconds) | ||
| yield() | ||
| ``` | ||
|
|
||
| ### Assertions | ||
|
|
||
| | Pseudocode | Kotlin | | ||
| |---|---| | ||
| | `ASSERT x == y` | `assertEquals(y, x)` | | ||
| | `ASSERT x IS NOT null` | `assertNotNull(x)` | | ||
| | `ASSERT x IS null` | `assertNull(x)` | | ||
| | `ASSERT x IS Auth` | `assertIs<Auth>(x)` | | ||
| | `ASSERT "key" IN map` | `assertContains(map, "key")` | | ||
| | `ASSERT x matches pattern "..."` | `assertTrue(x.matches(Regex("...")))` | | ||
| | `ASSERT list CONTAINS_IN_ORDER [a, b, c]` | `val it = list.iterator(); assertEquals(a, it.next()); assertEquals(b, it.next()); ...` | | ||
| | `AWAIT expr FAILS WITH error` | `val error = assertFailsWith<AblyException> { expr }; assertEquals(..., error.errorInfo.code)` | | ||
| | `ASSERT list.length == N` | `assertEquals(N, list.size)` | | ||
|
|
||
| ### Test naming and annotation | ||
|
|
||
| - KDoc comment immediately above `@Test` using `/** @UTS <spec-id> */` format | ||
| - Method name: backtick string `` `<spec-id> - <description>` `` | ||
| - Use `runTest { }` from `kotlinx.coroutines.test` for all async tests | ||
|
|
||
| ```kotlin | ||
| /** | ||
| * @UTS realtime/unit/RTN4a/some-description-0 | ||
| */ | ||
| @Test | ||
| fun `RTN4a - description of what is being tested`() = runTest { | ||
| ... | ||
| } | ||
| ``` | ||
|
|
||
| ### File template | ||
|
|
||
| ```kotlin | ||
| package io.ably.lib.<category>.unit[.<subcategory>] | ||
|
|
||
| import io.ably.lib.TestRealtimeClient // or TestRestClient | ||
| import io.ably.lib.awaitChannelState // if testing channels | ||
| import io.ably.lib.awaitState | ||
| import io.ably.lib.realtime.ChannelState // if testing channels | ||
| import io.ably.lib.realtime.ConnectionState | ||
| import io.ably.lib.test.mock.FakeClock // if using fake timers | ||
| import io.ably.lib.test.mock.MockWebSocket // or MockHttpClient | ||
| import io.ably.lib.types.ConnectionDetails | ||
| import io.ably.lib.types.ErrorInfo | ||
| import io.ably.lib.types.ProtocolMessage | ||
| import kotlinx.coroutines.launch | ||
| import kotlinx.coroutines.test.runTest | ||
| import kotlin.test.* | ||
| import kotlin.time.Duration.Companion.seconds // if using Duration literals | ||
|
|
||
| class <Name>Test { | ||
|
|
||
| /** | ||
| * @UTS realtime/unit/<spec-id>/<test-slug> | ||
| */ | ||
| @Test | ||
| fun `<spec-id> - <description>`() = runTest { | ||
| val mock = MockWebSocket { | ||
| onConnectionAttempt = { conn -> | ||
| conn.respondWithSuccess(ProtocolMessage().apply { | ||
| action = ProtocolMessage.Action.connected | ||
| connectionId = "test-connection-id" | ||
| connectionDetails = ConnectionDetails { | ||
| connectionKey = "test-key" | ||
| maxIdleInterval = 15_000L | ||
| connectionStateTtl = 120_000L | ||
| } | ||
| }) | ||
| } | ||
| } | ||
| val client = TestRealtimeClient { | ||
| autoConnect = false | ||
| install(mock) | ||
| } | ||
|
|
||
| client.connect() | ||
| awaitState(client, ConnectionState.connected) | ||
|
|
||
| assertEquals(ConnectionState.connected, client.connection.state) | ||
| client.close() | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Step 5 — Compile | ||
|
|
||
| ```bash | ||
| ./gradlew :uts:compileTestKotlin | ||
| ``` | ||
|
|
||
| Fix any compilation errors and recompile until clean. Common issues: | ||
| - Missing imports | ||
| - Method names differ from what you read in the mock files (use the exact names from Step 3) | ||
| - `ErrorInfo` constructor arg order is `(message, statusCode, code)` | ||
|
|
||
| --- | ||
|
|
||
| ## Step 6 — Run tests | ||
|
|
||
| ```bash | ||
| ./gradlew :uts:test --tests "<package>.<ClassName>" | ||
| ``` | ||
|
|
||
| Handle test failures using this decision tree (see [reference doc](https://github.com/ably/specification/blob/main/uts/docs/writing-derived-tests.md) for full detail): | ||
|
|
||
| ``` | ||
| Test fails | ||
| | | ||
| +-- Does UTS spec match features spec? | ||
| | NO → fix test, record UTS spec error in deviations file | ||
| | YES | ||
| | +-- Does test accurately translate the UTS spec? | ||
| | NO → fix the test (no deviation entry needed) | ||
| | YES → SDK deviation — adapt test, record in deviations file | ||
| ``` | ||
|
|
||
| ### Deviation patterns | ||
|
|
||
| **Env-gated skip (preferred)** — test contains spec-correct assertions but is skipped by default: | ||
|
|
||
| ```kotlin | ||
| /** | ||
| * @UTS realtime/unit/RSA4c2/callback-error-connecting-disconnected-0 | ||
| */ | ||
| @Test | ||
| fun `RSA4c2 - callback error connecting disconnected`() = runTest { | ||
| // DEVIATION: see deviations.md | ||
| if (System.getenv("RUN_DEVIATIONS") != null) return@runTest | ||
|
|
||
| // ... spec-correct setup and assertions ... | ||
| } | ||
| ``` | ||
|
|
||
| **Adapted assertion** — when you still want to assert on the SDK's actual behaviour to prevent regressions: | ||
|
|
||
| ```kotlin | ||
| // DEVIATION: spec requires error code 40106, SDK returns 40160 — see deviations.md | ||
| assertEquals(40160, error.errorInfo.code) | ||
| ``` | ||
|
|
||
| **Never use the accommodate-both pattern** (accept either spec or SDK behaviour). Every test must assert either spec behaviour or the SDK's actual behaviour — never both at once. | ||
|
|
||
| ### Deviations file | ||
|
|
||
| Append to `uts/src/test/kotlin/io/ably/lib/deviations.md`. Each entry needs: | ||
| 1. The spec point (e.g. `RSA4c2`) | ||
| 2. What the spec says | ||
| 3. What the SDK does | ||
| 4. Which test is affected and how it was adapted | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there be a reference to https://github.com/ably/specification/blob/main/uts/docs/writing-derived-tests.md ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added