Testing a plugin
The tool for testing plugins is part of the app’s repository, because it runs the real core, which is not published. It is not in the public repository tessera-plugins, so the examples below can be run by the maintainers of the app only. A plugin written outside the app’s repository is checked in two other ways: npm run typecheck in the plugins repository checks its code against the types of ctx, and installing the plugin file in the app, with Settings › Plugins, runs it with the diagnostics of the plugin shown in the list. The description below explains what the tool does, so that you know what the plugins of the repository are tested against.
The tool builds the plugin and runs it inside the real core, in the same isolated environment that the app uses. The library that a test starts with is small and contains three rows.
The test controls the clock, the random numbers and the timers. It can therefore move time forward, and it produces the same result every time it runs.
The following test loads a plugin that protects rows, attempts to delete a row, and verifies that the plugin refused the deletion:
import { intents } from '@tessera/core';import { loadPlugin } from '@tessera/plugin-sdk/testing';
// start the plugin in the real core, with the permission "read"const plugin = await loadPlugin({ entry: './index.ts', permissions: ['read'] });
// try to delete the row "a"const outcome = await plugin.core.dispatch(intents.deleteRows.intent({ ids: ['a'] }));// the plugin refused: outcome.status is 'vetoed' and outcome.code is 'protected'
await plugin.dispose();loadPlugin receives the file in which the plugin starts and the permissions to grant to it. It returns plugin.core, which you use to make changes in the same way as the app does. At the end of the test, plugin.dispose() stops the plugin.
A plugin that locks text for friends
Section titled “A plugin that locks text for friends”A plugin that uses crypto:friends must name its publisher, and the app installs it only with the signature of that publisher. In a test, loadPlugin signs the plugin with a key that it makes for the occasion, and it gives the plugin a core that has no friends.
A plugin that ships with the app names no publisher. For such a plugin, give loadPlugin the option bundled: true: it installs the plugin as the app installs its own, without a signature, and two plugins that load this way are the same plugin for each other.
To test what two people do with the same plugin, give each person a core, and give each core the people that it knows. createKeyring comes from @tessera/core/testing: it takes the keys of the devices of the person, the friends that the person has, and the devices of each friend. keyring gives it to the core. For a plugin from a publisher, publisherKey is the secret key of the publisher, and two plugins that load with the same key are the same plugin for each other, so that one opens what the other locked.
The library of the person lists the friends too, because ctx.state.friends() reads them there. A plugin that reads who the person is (ctx.state.viewer) needs the group of the library as well, and one that lists the friends on a shared outline (ctx.state.friends(outline)) needs the outline’s share and the library’s shares:
import { createKeyring, noblePrimitives, TesseraCrypto } from '@tessera/core/testing';import { loadPlugin, sampleLibrary } from '@tessera/plugin-sdk/testing';
// randomBytes(n) returns n bytes. A fixed seed makes the test give the same result every time.const crypto = new TesseraCrypto(noblePrimitives, randomBytes);const eric = crypto.device(), ann = crypto.device();const member = (d) => ({ signPub: d.signPub, boxPub: d.boxPub, status: 'active' as const });
// what each person knows: their own devices, and a friend whose devices are those of the other personconst keyringOf = (me, other, name) => createKeyring({ me: () => me, devices: () => [{ id: me.device, ...member(me) }], friends: () => [{ group: name, genesis: 'g-' + name, name }], roster: async () => ({ members: new Map([[other.device, member(other)]]) }), now: () => 0,});const friend = (name) => ({ group: name, name, avatar: '', genesis: 'g-' + name, since: 1 });
const PUBLISHER = randomBytes(32); // the same publisher for both peopleconst onEricsDevice = await loadPlugin({ entry: './index.ts', permissions: ['read', 'crypto:friends'], publisherKey: PUBLISHER, keyring: keyringOf(eric, ann, 'Ann'), library: { ...sampleLibrary(), friends: { Ann: friend('Ann') } },});const onAnnsDevice = await loadPlugin({ entry: './index.ts', permissions: ['read', 'crypto:friends'], publisherKey: PUBLISHER, keyring: keyringOf(ann, eric, 'Eric'), library: { ...sampleLibrary(), friends: { Eric: friend('Eric') } },});The sync between the two people is not part of the test. To move a change from one person to the other, read what the first core saved with plugin.core.read, and start the second core with a library that holds it. A change that reaches a core that is already running is made there with plugin.core.dispatch, as the sync would. The tests of the plugin A letter that opens only when everyone agrees do this for four people.
A plugin that writes in the audit trail
Section titled “A plugin that writes in the audit trail”ctx.audit.record writes its note through a change of the outline, which only this device keeps, and the device signs the note into the trail when it belongs to a group. The core that loadPlugin starts belongs to no group, so it has no trail, but the change is made all the same. A test reads it on the outline, under the key tessera.audit/note: the plugin as the app named it, and the text.
// the plugin writes a note when its command `approve` runs on a rowconst plugin = await loadPlugin({ entry: './index.ts', permissions: ['read', 'write:rows', 'audit:write'], manifest: { id: 'com.example.approvals' } });await plugin.core.dispatch({ command: 'com.example.approvals/approve', args: { id: 'a' } });
const facets = await plugin.core.read('outline:o1', 'facets');// { plugin: 'com.example.approvals', text: 'Approved "a"', n: '…' }console.log(facets['tessera.audit/note']);Only the last note is kept on the outline: each note replaces the one before it there, while the trail keeps them all.
