Skip to content

Simulate Tesla's Owner API, its tokens and the car's sleep - #148

Merged
vcanuel merged 3 commits into
mainfrom
feat/tesla-simulator
Oct 27, 2026
Merged

vcanuel merged 3 commits into
mainfrom
feat/tesla-simulator

Conversation

@vcanuel

@vcanuel vcanuel commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

The simulator now serves Tesla's Owner API, so the collector's Tesla step (the sleep window, the state-conditioned read, the token's rotation) can be built and tested end to end. Its tokens rotate and its cars fall asleep the way a real Tesla's do, without anyone needing a car.

Changes

  • Owner API (internal/simulator/teslaapi): the refresh of the owner's token (client_id=ownerapi, form or JSON); products; vehicles/{id}; vehicle_data, filled from a real Model Y response (anonymised) with the live state: miles, charging_state, shift_state, speed, power, charge_energy_added, AC phases, DC fast charger. Errors: 401 invalid bearer token, 404 not_found.
  • Tokens: every refresh issues a new refresh token, and the one given is then refused (invalid_grant). This is the strictest assumption, so a client that keeps the old token gets caught. Tokens are signed, so they survive a restart of the simulator.
  • Sleep: a car that is neither driving nor charging falls asleep SIM_TESLA_SLEEP_AFTER after its last activity or its last vehicle_data read (10 min by default; the real delay is unknown). Reading vehicles/{id} does not keep it awake. Asleep, it reports itself offline, as observed, and vehicle_data answers 408.
  • Faults on demand under /sim/tesla/: rate-limit?for= (429 with Retry-After), owner-api?closed= (the 403 that points to the Fleet API), revoke, and refresh-token (a new token, as Tesla Auth gives one).
  • Vehicle model and scenarios: the speed while driving and the energy a charge added; brand: tesla and trim; StateAt for the live state; scenarios/model3.yaml (a Model 3 driving home to work and back, then an AC charge at 11 kW).
  • runsten-simulator serves it (SIM_TESLA_SLEEP_AFTER, SIM_TESLA_TOKEN_TTL). The Tesla client is tested against it over the scenario's day: refresh and rotation, sleep and 408, the drive, the charge, 429, 403, revocation, expiry.
  • archtest, depguard and AGENTS.md updated.

Known limits: the position stays at the start of a trip until the car arrives (the vehicle model does not move it while driving). The sleep delay and the shape of a 429 are assumptions, to check on a real car.

No screenshots: nothing changes in the web interface.

Tests

  • task unit-go: format, lint (0 issues), unit tests, govulncheck and build, all passed. task unit-web not run: nothing changed in web/.
  • Each commit's simulator tests pass on their own.
  • The simulator binary was run by hand with a Volvo and a Tesla scenario: refresh, products and vehicle_data answer as expected.
  • Local CodeRabbit review over the whole diff: no findings.

Closes #147

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added Tesla Owner API simulation, including authentication, token rotation, vehicle status and data, and configurable fault responses.
    • Added Tesla support in fleet scenarios alongside Volvo and Polestar, including a Model 3 scenario.
    • Tesla simulations now support configurable vehicle sleep timing and access-token lifetime.
    • Vehicle data reflects driving and charging state, including speed, charge added, and Model Y response formats.

vcanuel and others added 3 commits October 27, 2026 15:42
Tesla's Owner API answers with the live state: the speed while driving, and the
energy the charge put into the battery, kept until the next plug-in.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A scenario's brand may be tesla, and its vehicle a trim (Tesla's trim_badging).
StateAt advances the simulation and gives the actual state, not only what was
uploaded: what an awake Tesla answers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
teslaapi serves the refresh of the owner's token, rotated on each refresh and the
one given then refused, the products, a vehicle's state and its vehicle_data,
shaped as a real Model Y's. A car neither driving nor charging falls asleep after
its last activity or vehicle_data read, reports itself offline and answers
vehicle_data with a 408; reading its state does not keep it awake. A 429, the
Owner API closed and a revocation are set on demand under /sim/tesla/.

runsten-simulator serves it for the scenarios of brand tesla (scenarios/model3.yaml),
and the Tesla client is tested against it over the scenario's day.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 5aee914 and 143646d.

Files selected for processing: 17
  • .golangci.yml
  • AGENTS.md
  • cmd/runsten-simulator/main.go
  • cmd/runsten-simulator/main_test.go
  • internal/archtest/deps_test.go
  • internal/simulator/scenario/scenario.go
  • internal/simulator/scenario/scenario_test.go
  • internal/simulator/scenario/simulation.go
  • internal/simulator/teslaapi/auth.go
  • internal/simulator/teslaapi/handler.go
  • internal/simulator/teslaapi/handler_test.go
  • internal/simulator/teslaapi/render.go
  • internal/simulator/teslaapi/vehicle_data.json
  • internal/simulator/vehicle/vehicle.go
  • internal/simulator/vehicle/vehicle_test.go
  • internal/tesla/simulator_test.go
  • scenarios/model3.yaml

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.

Walkthrough

The simulator now supports Tesla scenarios and a simulated Tesla Owner API. It includes token handling, vehicle sleep and data responses, configurable faults, and command-line configuration. Vehicle state now records driving speed and charge energy.

Changes: Tesla simulator

Layer / File(s) Summary
Tesla scenario and simulation state
internal/simulator/scenario/*, scenarios/model3.yaml
Scenario validation accepts Tesla and passes vehicle trim into the simulation. Simulation.StateAt returns vehicle state at a requested time. A Model 3 scenario and related tests are added.
Vehicle speed and charging state
internal/simulator/vehicle/vehicle.go, internal/simulator/vehicle/vehicle_test.go
Vehicle state records driving speed and charge energy. Driving updates speed, and charging records delivered energy until the next plug-in.
Owner API authentication and endpoints
internal/simulator/teslaapi/auth.go, internal/simulator/teslaapi/handler.go, internal/simulator/teslaapi/handler_test.go
The handler implements token issuance, rotation, and authorization; vehicle and product routes; sleep behavior; and controls for rate limiting, account closure, and token revocation. Tests cover these behaviors and API responses.
Vehicle-data response rendering
internal/simulator/teslaapi/render.go, internal/simulator/teslaapi/vehicle_data.json, internal/simulator/teslaapi/handler_test.go
The renderer builds vehicle-data responses from simulator state and a JSON template. It populates charge, drive, configuration, climate, and timestamp fields, including odometer and range conversions.
Command wiring and simulator validation
cmd/runsten-simulator/*, internal/tesla/simulator_test.go, internal/archtest/deps_test.go, .golangci.yml, AGENTS.md
The command routes Tesla scenarios to the Tesla handler and reads Tesla sleep and token-lifetime settings. Fleet and end-to-end tests, package dependency rules, and simulator documentation are updated.

Possibly related issues

  • Issue 131 includes a simulator checklist that this change addresses; its broader collector, schema, and real-car work remains outside this change.

Possibly related PRs

Priority: Normal

Merge Risk: Minimal · up to 14364

No concrete blocking issue remains; the Tesla simulator is ready to merge after normal checks.

Security architecture risk: Low · up to 14364

The change is confined to a development simulator. A refresh-token revocation inconsistency after restart can weaken authentication-recovery testing. The new fault controls also rely on deployment isolation rather than caller authentication; that isolation is not established by the available evidence.

Retained concerns

  • Low · reliability · inferred: Refresh-token revocation is inconsistent after recovery. Issue an unused token at generation 1, restart the simulator, then revoke once: the handler returns to generation 1 and accepts that pre-restart token because validation rejects only lower generations. Restart deliberately resets token-consumption state, but a later revoke still promises to refuse previously issued refresh tokens. This weakens the simulator's authentication-failure contract and can mask client recovery defects; it does not establish access to real Tesla accounts.
Security review details

Security Blast Radius

  • inferred — A caller able to reach the Tesla control routes can mint synthetic refresh tokens, revoke refresh credentials, or change faults for every Tesla vehicle assigned to that handler. The inspected wiring bounds these effects to one simulated account; it does not supply real-car credentials or command authority.

Trust Boundaries and Controls

  • observed — Bearer middleware protects the Owner API submux, but not simulator controls. Tests assert rejection of missing, forged, wrong-kind, and expired access tokens. These are simulated protocol controls, not a production credential boundary: development token generation and its signing key are intentionally available in source.

Resilience and Maintainability Implications

  • observed — Fault controls provide explicit recovery operations: a zero duration lifts rate limiting, closed=false reopens the account, and refresh-token issues a replacement credential after revocation. These operations mutate shared handler state under its mutex, but that state is not durable.

Hardening Proposals

  • proposed — For shared or remotely reachable test environments, isolate the simulator listener or separately authorize the mutable control routes. This is a deployment-hardening proposal, not evidence of an exposed production service.
Pre-merge checks: Passed 4
Check name Status Explanation
Title check Passed The title is an imperative English summary that clearly describes the simulated Tesla Owner API, token handling, and vehicle sleep behavior.
Description check Passed The description explains the purpose, implementation scope, known limits, linked issue, and test results. It is mostly complete and covers the required information, although it uses task unit-go ins…
Linked Issues check Passed The PR implements the coding requirements in #147. internal/simulator/teslaapi provides token refresh and rotation, signed access-token validation, products, vehicle, and vehicle_data endpoints. I…
Out of Scope Changes check Passed The changes remain within #147. Scenario-model extensions support Tesla state and charging data. Vehicle speed and charge-energy fields support the simulator responses. Command wiring, dependency rule…
Full details: Description check

Explanation

The description explains the purpose, implementation scope, known limits, linked issue, and test results. It is mostly complete and covers the required information, although it uses task unit-go instead of the template's exact task unit checkbox format.

Full details: Linked Issues check

Explanation

The PR implements the coding requirements in #147. internal/simulator/teslaapi provides token refresh and rotation, signed access-token validation, products, vehicle, and vehicle_data endpoints. It returns 408 while a parked car sleeps, keeps the car awake on successful vehicle_data reads, does not wake it with vehicle-state reads, and wakes it during driving or charging. The control API provides 429 with Retry-After, Owner API closed 403, refresh-token revocation, and refresh-token issuance. scenarios/model3.yaml defines a brand: tesla Model 3 drive and AC charge. internal/tesla/simulator_test.go exercises the scenario, sleep, charging, token rotation and expiry, and required faults. The simulator command serves the Tesla API with configurable sleep and token TTL.

Full details: Out of Scope Changes check

Explanation

The changes remain within #147. Scenario-model extensions support Tesla state and charging data. Vehicle speed and charge-energy fields support the simulator responses. Command wiring, dependency rules, documentation, and tests support serving and validating the simulator. No unrelated product or web-interface changes are shown.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tesla: the simulator (internal/simulator/teslaapi) and a brand: tesla scenario

1 participant