Skip to content

Test failures with a fake world ​

To see the failed state of your view and try its Retry button while you build, make a fake world fail on purpose. failNext: true makes the next message of the person fail like a message lost on a bad network. Press Send hello to see the message fail and then Retry to see it go through and the world answer.

jsx
import { useWorld } from './useWorld.js';

// A fake world to build with, made to fail. The next message the person sends
// fails, as on a bad network, and the ones after it go through.
const arena = {
	name: 'Arena',
	failNext: true,
	onMessage: (message) => `Got it: ${message.text}`,
};

export default function App() {
	// While building: the fake world. In the webapp: useWorld()
	const { world, error } = useWorld({ fake: { world: arena } });

	if (error) return <p>Could not connect ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;

	// Drawn again at every change, including when the delivery of a message changes.
	return (
		<>
			<button onClick={() => world.send('hello')}>Send hello</button>
			{world.messages.map((message) => (
				<p key={message.id}>
					{message.fromMe ? `You: ${message.text} (${message.status})` : `${message.from}: ${message.text}`}
					{/* A failed message from the person can be sent again. */}
					{message.status === 'failed' && <> <button onClick={() => message.retry()}>Retry</button></>}
				</p>
			))}
		</>
	);
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// A fake world to build with, made to fail. The next message the person sends
// fails, as on a bad network, and the ones after it go through.
const arena = {
	name: 'Arena',
	failNext: true,
	onMessage: (message) => `Got it: ${message.text}`,
};

// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: arena } });

const send = document.createElement('button');
send.textContent = 'Send hello';
send.onclick = () => world.send('hello');
const list = document.createElement('div');
document.body.append(send, list);

// Called at once, then after every change, including when the delivery of a message changes.
world.subscribe((node) => {
	list.replaceChildren(...node.messages.map((message) => {
		const line = document.createElement('p');
		line.textContent = message.fromMe ? `You: ${message.text} (${message.status})` : `${message.from}: ${message.text}`;
		// A failed message from the person can be sent again.
		if (message.status === 'failed') {
			const retry = document.createElement('button');
			retry.textContent = 'Retry';
			retry.onclick = () => message.retry();
			line.append(' ', retry);
		}
		return line;
	}));
});
Live, on a fake world
Open

How it works ​

When the next message fails, send resolves with 'failed', the status of the message becomes 'failed' and message.retry() sends it again. The messages after it go through. To see the failure again, reload the page (Restart here).

Two more failures are easy to cause while you build:

  • 'not-allowed' comes from an empty message or from a question answered twice.
  • connectToHost() without fake rejects at once with 'no-host' in a page opened alone in a tab.

A fake world says yes at once and the failures 'declined' and 'unavailable' of askFor therefore come only from the webapp. Write the code for them with the recipes below and try it in the webapp by saying no when it asks. For that test, enter the address of your view (http://localhost included) in the Own view field of a world of yours (Going live).

See also ​

The UNaIVERSE GUI kit, released under the Apache License 2.0. Copyright 2024-2026 UNaIVERSE S.r.l.