# Testing with local mode and the Preview tool

There are three places to test, from fastest to most realistic.

| Where | Needs | What it gives you |
| --- | --- | --- |
| **Local mode** (your machine) | Nothing: a local web server | The SDK answers from an in-page mock. Instant feedback, works offline. |
| **[Preview tool](https://www.zokogames.com/preview)** (`/preview`) | Nothing: no account, no upload | Your game, served from any `http(s)` address such as `localhost`, runs inside the real portal frame with a live requirements checklist and an event log. |
| **Build preview** (developer console, game, **Preview and QA** tab) | An uploaded, validated build | Your uploaded build inside the real portal frame, with ads switched on so every module can be exercised. Tracking, votes and reports are blocked, so testing never touches your numbers. |

## Local mode

Open your game from `localhost`, `127.0.0.1`, `[::1]` or any `*.localhost` host, or add `?useLocalSdk=true` to the URL on any other host. The SDK then runs an in-page mock instead of talking to the portal. Serve over HTTP; `file://` pages have no host name and are not treated as local (add `?useLocalSdk=true` if you must open one).

### Exactly when local mode starts

`SDK.init()` decides once, when it is called:

1. **Top-level page** (not inside an iframe): `local` if the host is a local one or the URL contains `useLocalSdk=true`; otherwise `disabled`.
2. **Inside an iframe**: the SDK first asks the parent page for the portal handshake and waits up to **1.5 seconds** (15 seconds when the parent page is the portal itself). If the portal answers, the environment is `zokogames` and `useLocalSdk` is ignored. If nothing answers, it falls back to `local` under the same rule as above (local host or `?useLocalSdk=true`), and to `disabled` otherwise. So `?useLocalSdk=true` on a game framed by your own test page works, with a 1.5 second delay at startup.

`useLocalSdk` is read from the game's own URL, not from the parent's. After `init()`, `SDK.environment` is `"local"`. Use `?sdk_debug=true` to log every call, and look for the `[local host]` lines.

Since SDK 1.6.2: the SDK also writes one console line, once per page, when it ends up `disabled` (it names the page that framed the game when that page is not the portal or did not answer, or says the page is neither framed nor on a local host) and the first time a member is used before `init()` resolved (`sdkNotInitialized`). Both are info lines that point back here. A cloud save that fails to reach the portal logs one warning saying the save is kept locally and the SDK retries. None of them contains player data.

What the mock does:

| Module | Local behaviour |
| --- | --- |
| Ads | A full-screen overlay labelled `Local ad (midgame)` or `Local ad (rewarded)` for 3 seconds, firing `adStarted` and `adFinished`. A midgame request less than three minutes after the previous ad (of either type) fails with `adCooldown`. A second request while an ad is on screen fails with `other`. Like the portal, local mode (SDK 1.6.4) refuses a midgame request with `adCooldown` and the message "Midgame ads are available after gameplay starts: call game.gameplayStart() first" until your game has called `gameplayStart()` or 30 seconds of visible page time have passed. Rewarded requests are not gated. SDK 1.6.3 and earlier played the ad in that case. |
| Banners | A dashed placeholder box appended to your container (for a responsive request, the largest listed size that fits). The SDK's own checks still run, so `missingId`, `invalidSize`, `notVisible`, `bannerCooldown` (30 seconds per container) and `maxRefreshReached` happen locally too. A missing container fails with `notCreated`. |
| User | Two fake users, `LocalUser1` and `LocalUser2` (`LocalUser1` is signed in at start), twelve fake friends, and an unsigned test token. Friends, inventory and purchases need a signed-in user and fail with `userNotAuthenticated` otherwise. |
| Store | `SDK.store.isAvailable` is `true`. A fake catalog of three items (`local-coins-100`, `local-gems-50`, `local-no-ads`) and an inventory that starts with three `local-coins-100` and one `local-no-ads`. `buyItem` and `consumeItem` update it in memory; it resets on reload. `getPurchaseToken` returns a placeholder string that your server cannot verify. |
| Data | Saved in the browser's `localStorage` under the key `zoko-sdk-data`, so it survives reloads on the same origin. The 1,048,576 byte limit still applies. |
| Game | Settings come from the query string. `happytime()` shows a short "Happy time!" toast. Invite links are your own URL with the parameters added. Room, loading, context and completion calls and `trackOrder` are accepted and do nothing visible. `submitScore` records nothing and logs one console line; offline (`?offline=true`) with a signed-in player and `delayed` (SDK 1.6.4) it shows "Score saved offline". |

Local mode is for developing. By default every ad is filled, so force a failure with `ad_response` (below) and always re-test in the Preview tool.

### Query switches

Add any of these to the page URL.

| Parameter | Values | Effect |
| --- | --- | --- |
| `sdk_debug` | `true` | Logs every SDK call and result to the console. Works in every environment. |
| `disableChat` | `true` | `SDK.game.settings.disableChat` becomes `true`. |
| `kidsMode` | `true` | From SDK 1.5.0: `SDK.game.settings.kidsMode` and `disableChat` become `true`; without it `kidsMode` is `false`. The other kids rules are not simulated; test them in the build preview. |
| `muteAudio` | `true` | `SDK.game.settings.muteAudio` becomes `true`. |
| `user_account_available` | `false` | Simulates a host without accounts: no user, `isUserAccountAvailable` is `false`. |
| `ad_response` | `unfilled`, `adblock`, `adsDisabled`, `other` | Every ad request fails with that code. `adblock` also makes `hasAdblock()` return `true`. |
| `user_response` | `user1`, `user2`, `logged_out` | Who `getUser()` returns at start (any other value means signed out). |
| `show_auth_prompt_response` | `user1`, `user2`, `user_cancelled` | What `showAuthPrompt()` does (default `user1`; any other value fails with `userCancelled`). It fails with `userAlreadySignedIn` when someone is already signed in, so combine it with `user_response=logged_out`. |
| `link_account_response` | `yes`, `no`, `logged_out` | What `showAccountLinkPrompt()` answers (`logged_out` fails with `userNotAuthenticated`). |
| `token_response` | `user1`, `user2`, `expired_token`, `logged_out` | Which user the token is for, or that it is already expired, or that it fails with `userNotAuthenticated`. |
| `offline` | `true` | `SDK.game.isOffline` is `true` and stays so; without it the mock follows the browser's online and offline events. |
| `instantJoin` | `true` | `SDK.game.isInstantMultiplayer` becomes `true`. |
| `qaTool` | `true` | Sets `SDK.isQaTool`, so the game can show extra diagnostics. Read in every environment. |

> [!NOTE]
> In local mode every query parameter is also an invite parameter: `SDK.game.inviteParams` is not `null` as soon as the URL has any query string (even `?sdk_debug=true`), and `getInviteParam("room")` reads `?room=...`. Do not treat "has invite parameters" as "was invited" while testing locally; test join flows with explicit parameters. Also, `SDK.data` is always enabled locally, so `dataModuleDisabled` never appears; the portal raises it when the data module is switched off for the game.

```text
http://localhost:8080/?sdk_debug=true&user_response=logged_out&show_auth_prompt_response=user2&ad_response=unfilled
```

> [!WARNING]
> The local token is unsigned (`alg: none`, `local: true` in the payload). Your server must reject it. If your backend accepts a local token, your verification is broken.

### Simulating portal events

From SDK 1.5.1, local mode can also play the events that only the portal sends, so you can test your listeners without the portal. Dispatch a `CustomEvent` on `window`:

| Event | `detail` | What the SDK does |
| --- | --- | --- |
| `zokogames:local-settings` | Any of `disableChat`, `kidsMode` and `muteAudio`, as booleans | Fires `settingsChange` with the updated settings. `kidsMode: true` also turns chat off, as `?kidsMode=true` does. |
| `zokogames:local-join-room` | Invite parameters as strings, for example `{ room: "abc" }` | Fires `joinRoom` with those parameters. |
| `zokogames:local-offline` | `{ offline: true }` or `{ offline: false }` | Fires `offlineChange`. Ignored while `?offline=true` pins the state. |

```js
window.dispatchEvent(new CustomEvent("zokogames:local-settings", { detail: { muteAudio: true } }));
window.dispatchEvent(new CustomEvent("zokogames:local-join-room", { detail: { room: "abc" } }));
window.dispatchEvent(new CustomEvent("zokogames:local-offline", { detail: { offline: true } }));
```

Run the lines from the browser console or wire them to a debug button. Outside local mode nothing listens for these events.

## The Preview tool

Open [`/preview`](https://www.zokogames.com/preview), type the address of your game (for example `http://localhost:8080/index.html`) and press **Load game**. No account is needed and nothing is uploaded: the game loads straight from your address inside a sandboxed frame, and the event log stays in your tab.

The portal frame answers SDK calls with test data: ads are placeholder screens, there is no signed-in user, and cloud saves stay in your browser. Use it to:

- Play at every window size from the [gameplay requirements](https://docs.zokogames.com/requirements/gameplay) with one-click **Frame size** presets.
- Watch every SDK call as it happens in the **event log**; filter it by method or text and export it as JSON to attach to a support request.
- Tick off the **requirements checklist**: SDK initialised, `loadingStop`, `gameplayStart`, `gameplayStop`, a midgame ad, a rewarded ad, a banner, data module use and `happytime`. Items turn green when your game makes the call.
- Check **time to the first `gameplayStart`**. The tool flags a start later than 20 seconds; trim your initial download if it does (see [first frame fast](https://docs.zokogames.com/guides/first-frame)).
- Toggle **Simulate settings** (mute audio, disable chat) and confirm your game reacts. The **Kids mode (SDK 1.5.0)** switch sends a settings change with `kidsMode` and `disableChat` set to `true`, as the portal does.

If the Preview page is served over `https`, browsers block plain `http` games unless they run on `localhost`. Serve your game over `https` or from `localhost`. Addresses with a username or password, and pages on this site (other than the bundled demos), are refused.

### Run a build zip from your computer

The Preview tool can also run a build zip chosen from your computer without uploading it. Pick the file under **Or run a build zip from this computer**: the zip is unpacked in your tab and served to the frame from memory. Local zips run on a separate preview origin. Each run starts with empty storage, so saves from an earlier run are gone, and only one zip preview can run at a time.

Before anything runs, the zip is checked like an upload: `index.html` must sit at the root of the zip (or inside a single top-level folder), and the build may hold at most 1,500 files and 250 MB unpacked. A zip that fails a check shows the reason instead of running.

## The build preview in the console

After you upload a build and it passes validation, the game's **Preview and QA** tab in the console runs that exact build in the real portal frame, with ads enabled and your game's own data, leaderboard and multiplayer settings applied; **Preview** on a build in the build list opens it there. The checklist is grouped into **Basic Launch**, **Full Launch**, **Limits the docs set** and **For your settings**, and it shows **SDK ready** and **First gameplay** timings. Play through the menus, a round, a pause and an ad break, then check that every item you need has ticked. The checks are advice and never stop the **Submit for review** button; a game waiting in its menu shows **Waiting for you to play**, not a failure, until you press Play. If the frame stays blank or the notice **The frame loaded but the SDK never started** appears, open the browser console (F12): *Refused to display in a frame* means your own server sends `X-Frame-Options` or a `frame-ancestors` rule that excludes zokogames.com; *Mixed Content* means an `http://` address on the https page; a Content-Security-Policy error means `https://sdk.zokogames.com` is not allowed in `script-src`. Each fix is also listed on the troubleshooting page.

### Simulate the player

The **Simulate the player** card in the build preview changes what the portal tells your game, so you can test situations that are hard to create. It works only in the console's build preview. Public game pages never honour it. Every simulated answer appears in the event log under **Portal replies**, next to the calls your game made.

| Setting | What the SDK reports |
| --- | --- |
| Player: Guest | `user.getUser()` resolves `null`, `SDK.user` is `null` at start, and token, score and friends calls fail as for a signed-out player. |
| Accounts unavailable | `user.isUserAccountAvailable` is `false`, no user is reported and `showAuthPrompt()` is cancelled. |
| Ad blocker installed | `ad.hasAdblock()` is `true` and `ad.requestAd()` fails with the `adblock` error code. Not in kids mode. |
| Ad-free member | A midgame break resolves at once as a finished ad, with no error. Banners fail with `bannersDisabled`. |
| Ads switched off for the game | `ad.requestAd()` fails with `adsDisabled`. |
| Banners unavailable | `banner.requestBanner()` fails with `bannersDisabled`. |
| Offline | `game.isOffline` is `true` and an `offlineChange` event arrives. Ad and banner requests fail as without a connection (`unfilled`, `bannersDisabled`). |
| Platform mute is on | `game.settings.muteAudio` is `true` and a `settingsChange` event arrives live. |
| Kids mode | `game.settings.kidsMode` and `game.settings.disableChat` are `true`, no ad blocker check runs, and sign-in, leaderboards and multiplayer are not offered. |
| Language | `user.systemInfo.locale` is the language you pick. |
| Device type and orientation | `user.systemInfo.device.type` follows the window size group (or the type you pick), and **Portrait** swaps the frame's width and height. |

Settings the game reads once at start (player, accounts, ad-free, ads off, kids, language, device type) restart the game when you change them. Ad blocker, banners, offline and mute reach the running game live.

## Checklist before you submit

- [ ] No console errors on load or during the first minute of play.
- [ ] `gameplayStart` is the first SDK event after the player acts, not at page load.
- [ ] A failed ad never leaves the game paused or muted.
- [ ] The game works with the SDK `disabled` (open it on a plain web server without `?useLocalSdk=true`).
- [ ] Nothing depends on `localStorage` surviving; use the [data module](https://docs.zokogames.com/sdk/data).
- [ ] The first `gameplayStart` arrives well within 20 seconds on a throttled connection.

Every line of the three checklists your game meets, word for word as the Preview tool, the build preview and the reviewer show them, is on the [testing checklist](https://docs.zokogames.com/getting-started/testing-checklist).

## Next steps

- [Testing checklist before you submit](https://docs.zokogames.com/getting-started/testing-checklist)
- [Submission walkthrough](https://docs.zokogames.com/getting-started/submission-walkthrough)
