Documentation
¶
Overview ¶
Package fakerelay is an in-process WebSocket server that speaks the routing half of the mobile↔relay protocol (docs/protocol-mobile.md § Authentication, § Routing envelope). It exists so daemon-side e2e tests can exercise the full WS roundtrip — binary ↔ relay ↔ phone — without depending on the pyrycode-relay binary or live infrastructure.
The package lives under internal/e2e/internal/ to visibility-fence it from non-e2e callers. The sibling fake-phone client (separate ticket) and the consuming roundtrip test (third ticket) wire up around it.
Wire contract ¶
Binary upgrades on /v1/server with header x-pyrycode-server (the claimed server-id). First-claim-wins: while a binary holds a server-id, further /v1/server upgrades for that id are rejected.
Phone upgrades on /v1/client with headers x-pyrycode-server, x-pyrycode-token, x-pyrycode-device-name. The relay does NOT validate the token contents; the binary owns that check. If no binary is bound to the requested server-id, the upgrade is rejected.
Each accepted phone receives a fresh, monotonically-numbered conn_id ("c-1", "c-2", …). The relay wraps every phone→binary frame as {"conn_id": "...", "frame": <phone-frame>} and unwraps every binary→phone frame, sending only the inner frame onto the phone WS.
Deviations from the production wire spec (deliberate) ¶
- Rejections happen pre-upgrade as HTTP 400/409/503 rather than as post-upgrade WS close codes (4409/4404/4401). The AC is satisfied by either form, and HTTP status surfaces directly in the dial error, which is simpler for consumer tests to assert against.
- No 30-second grace period on server-id release: when the binary disconnects, its server-id is immediately reusable.
- No TLS, no persistence, no rate limiting.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a running fake relay. Construct with New; shut down with Close. Safe for concurrent use across handler goroutines; consumers observe behavior through the WS endpoints, not by reaching in.
func New ¶
New returns a running fake relay bound to a random localhost port. The returned Server is ready for connections; Close shuts it down. logger is required (Debug-level lifecycle and routing events); nil panics at construction.
func (*Server) ForceCloseBinary ¶
ForceCloseBinary closes the WS conn currently bound to serverID with websocket.StatusInternalError (1011), simulating a non-fatal relay-side drop. Returns true if a binary was bound and the close was issued; false if no binary is currently bound to serverID. Used by e2e tests to assert that the daemon's claude child survives a non-fatal close (transport reconnects, supervisor stays up).
func (*Server) RejectBinaryWith4409Persistently ¶ added in v0.16.0
func (s *Server) RejectBinaryWith4409Persistently()
RejectBinaryWith4409Persistently arms a sticky mode: EVERY /v1/server upgrade accepts the WS handshake and immediately closes with WS code 4409, and the mode never clears. Simulates a genuine duplicate binary already holding the server-id, so the daemon draws a 4409 on every reconnect and, after ServerIDConflictThreshold consecutive closes, treats the conflict as fatal. Used by the e2e suite to drive the "persistent conflict → daemon exits non-zero" path; contrast RejectNextBinaryWith4409 (one-shot, the transient self-heal case).
func (*Server) RejectNextBinaryWith4409 ¶
func (s *Server) RejectNextBinaryWith4409()
RejectNextBinaryWith4409 arms a one-shot mode: the next /v1/server upgrade accepts the WS handshake and immediately closes with WS code 4409 ("server-id already claimed"). Subsequent connects follow normal logic. Used by e2e tests to drive the daemon's "fatal close → daemon shuts down" path without racing against the harness's HTTP-409 first-claim branch.
func (*Server) URL ¶
URL reports the base ws:// URL (no trailing path). Callers append "/v1/server" or "/v1/client".
func (*Server) WaitBinary ¶ added in v0.13.0
WaitBinary blocks until a binary is registered for serverID, the context is done, or the deadline expires. Returns true if the binary became registered before ctx expired; false otherwise. Unit and e2e tests call this between dialing a binary and probing server-side state (e.g. ForceCloseBinary) to close the upgrade→registration race: websocket.Accept unblocks the client before the server-side handler finishes inserting into s.binaries.