Skip to content

Commit

Permalink
test(cli): Add demo section tests, excluding familiar-chat
Browse files Browse the repository at this point in the history
  • Loading branch information
grypez committed Aug 14, 2024
1 parent e84a161 commit 8425882
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/cli/test/demo/confined-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** @import {TestRoutine} from '../types */

/** @type {TestRoutine} */
export const section = async (execa, testLine) => {
// If a runlet returns a promise for some value, it will print that value before exiting gracefully.
await testLine(execa`endo run runlet.js a b c`, {
stdout: "Hello, World! [ 'a', 'b', 'c' ]\n42",
});
};
60 changes: 60 additions & 0 deletions packages/cli/test/demo/counter-example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/** @import {Context, TestRoutine} from '../types' */

/** @type {TestRoutine} */
export const section = async (execa, testLine) => {
// We can create an instance of the counter and give it a name.
await testLine(execa`endo make counter.js --name counter`, {
stdout: 'Object [Alleged: Counter] {}',
});

// Then, we can send messages to the counter and see their responses...
await testLine(execa`endo eval E(counter).incr() counter`, {
stdout: '1',
});
await testLine(execa`endo eval E(counter).incr() counter`, {
stdout: '2',
});
await testLine(execa`endo eval E(counter).incr() counter`, {
stdout: '3',
});

// Aside: in all the above cases, we use counter both as the property name...
await testLine(execa`endo eval E(c).incr() c:counter`, {
stdout: '4',
});

// Endo preserves the commands that led to the creation of the counter value...
await testLine(execa`endo restart`);
await testLine(execa`endo eval E(counter).incr() counter`, {
stdout: '1',
});
await testLine(execa`endo eval E(counter).incr() counter`, {
stdout: '2',
});
await testLine(execa`endo eval E(counter).incr() counter`, {
stdout: '3',
});

// Aside, since Eventual Send, the machinery under the E operator...
await testLine(execa`endo spawn greeter`);
await testLine(
execa`endo eval --worker greeter '${'Hello, World!'}' --name greeting`,
{
stdout: 'Hello, World!',
},
);
await testLine(execa`endo show greeting`, {
stdout: 'Hello, World!',
});
};

/** @type {Context} */
export const context = {
setup: async execa => {
await execa`endo make counter.js --name counter`;
await execa`endo eval E(counter).incr() counter`;
await execa`endo eval E(counter).incr() counter`;
await execa`endo eval E(counter).incr() counter`;
await execa`endo spawn greeter`;
},
};
52 changes: 52 additions & 0 deletions packages/cli/test/demo/doubler-agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/** @import {Context, TestRoutine} from '../types' */

/** @type {TestRoutine} */
export const section = async (execa, testLine) => {
// We make a doubler mostly the same way we made the counter...
await testLine(execa`endo mkguest doubler-handle doubler-agent`, {
stdout: 'Object [Alleged: EndoGuest] {}',
});
await testLine(
execa`endo make doubler.js --name doubler --powers doubler-agent`,
);

// This creates a doubler, but the doubler cannot respond until we resolve...
await testLine(execa`endo inbox`, {
stdout:
/^0\. "doubler-handle" requested "a counter, suitable for doubling"/,
});
await testLine(execa`endo resolve 0 counter`);

// Now we can get a response from the doubler.
await testLine(execa`endo eval E(doubler).incr() doubler`, {
stdout: '8',
});
await testLine(execa`endo eval E(doubler).incr() doubler`, {
stdout: '10',
});
await testLine(execa`endo eval E(doubler).incr() doubler`, {
stdout: '12',
});

// Also, in the optional second argument to request, doubler.js names...
await testLine(execa`endo restart`);
await testLine(execa`endo eval E(doubler).incr() doubler`, {
stdout: '2',
});
await testLine(execa`endo eval E(doubler).incr() doubler`, {
stdout: '4',
});
await testLine(execa`endo eval E(doubler).incr() doubler`, {
stdout: '6',
});
};

/** @type {Context} */
export const context = {
setup: async execa => {
await execa`endo mkguest doubler-handle doubler-agent`;
await execa`endo make doubler.js --name doubler --powers doubler-agent`;
await execa`endo inbox`;
await execa`endo resolve 0 counter`;
},
};
69 changes: 69 additions & 0 deletions packages/cli/test/demo/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { $ } from 'execa';
import { makeSectionTest } from '../section.js';
import { withContext } from '../with-context.js';
import { daemonContext } from '../daemon-context.js';
import * as counterExample from './counter-example.js';
import * as doublerAgent from './doubler-agent.js';
import * as confinedScript from './confined-script.js';
import * as sendingMessages from './sending-messages.js';
import * as namesInTransit from './names-in-transit.js';
import * as mailboxesAreSymmetric from './mailboxes-are-symmetric.js';

test.serial(
'trivial',
Expand All @@ -14,3 +20,66 @@ test.serial(
}),
),
);

test.serial(
'counter-example',
makeSectionTest(
$({ cwd: 'demo' }),
withContext(daemonContext)(counterExample.section),
),
);

test.serial(
'doubler-agent',
makeSectionTest(
$({ cwd: 'demo' }),
withContext(daemonContext, counterExample.context)(doublerAgent.section),
),
);

test.serial.failing(
'sending-messages',
makeSectionTest(
$({ cwd: 'demo' }),
withContext(
daemonContext,
counterExample.context,
doublerAgent.context,
)(sendingMessages.section),
),
);

test.serial.failing(
'names-in-transit',
makeSectionTest(
$({ cwd: 'demo' }),
withContext(
daemonContext,
counterExample.context,
doublerAgent.context,
sendingMessages.context,
)(namesInTransit.section),
),
);

test.serial.failing(
'mailboxes-are-symmetric',
makeSectionTest(
$({ cwd: 'demo' }),
withContext(
daemonContext,
counterExample.context,
doublerAgent.context,
sendingMessages.context,
namesInTransit.context,
)(mailboxesAreSymmetric.section),
),
);

test.serial(
'confined-script',
makeSectionTest(
$({ cwd: 'demo' }),
withContext(daemonContext)(confinedScript.section),
),
);
14 changes: 14 additions & 0 deletions packages/cli/test/demo/mailboxes-are-symmetric.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/** @import {TestRoutine} from '../types' */

/** @type {TestRoutine} */
export const section = async (execa, testLine) => {
// Guests can also send their host messages...
await testLine(
execa`endo send HOST --as alice-agent ${'This is the @doubler you sent me.'}`,
);
await testLine(execa`endo inbox`, {
stdout: /^0\. "alice" sent "This is the @doubler you sent me\."/,
});
await testLine(execa`endo adopt 0 doubler doubler-from-alice`);
await testLine(execa`endo dismiss 0`);
};
27 changes: 27 additions & 0 deletions packages/cli/test/demo/names-in-transit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/** @import {Context, TestRoutine} from '../types' */

/** @type {TestRoutine} */
export const section = async (execa, testLine) => {
// In this example, we send alice our "doubler" but let it appear...
await testLine(
execa`endo send alice ${'Please enjoy this @counter:doubler.'}`,
);
await testLine(execa`endo inbox --as alice-agent`, {
stdout: /^1\. "HOST" sent "Please enjoy this @counter\."/,
});
await testLine(execa`endo adopt --as alice-agent 1 counter --name redoubler`);
await testLine(execa`endo list --as alice-agent`, {
stdout: 'redoubler',
});
await testLine(execa`endo dismiss --as alice-agent 1`);
};

/** @type {Context} */
export const context = {
setup: async execa => {
await execa`endo send alice ${'Please enjoy this @counter:doubler.'}`;
await execa`endo inbox --as alice-agent`;
await execa`endo adopt --as alice-agent 1 counter --name redoubler`;
await execa`endo dismiss --as alice-agent 1`;
},
};
28 changes: 28 additions & 0 deletions packages/cli/test/demo/sending-messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/** @import {Context, TestRoutine} from '../types' */

/** @type {TestRoutine} */
export const section = async (execa, testLine) => {
// So far, we have run guest programs like the doubler. Guests and hosts can exchange messages...
await testLine(execa`endo mkguest alice alice-agent`, {
stdout: 'Object [Alleged: EndoGuest] {}',
});
await testLine(execa`endo send alice ${'Please enjoy this @doubler.'}`);
await testLine(execa`endo inbox --as alice-agent`, {
stdout: /^0\. "HOST" sent "Please enjoy this @doubler\."/,
});
await testLine(execa`endo adopt --as alice-agent 0 doubler`);
await testLine(execa`endo list --as alice-agent`, {
stdout: 'doubler',
});
await testLine(execa`endo dismiss --as alice-agent 0`);
};

/** @type {Context} */
export const context = {
setup: async execa => {
await execa`endo mkguest alice alice-agent`;
await execa`endo send alice ${'Please enjoy this @doubler.'}`;
await execa`endo adopt alice-agent 0 doubler`;
await execa`endo dismiss --as alice-agent 0`;
},
};

0 comments on commit 8425882

Please sign in to comment.