Support paid current-turn travel deals
Verify and publish checkpoint container / container (push) Successful in 10m34s
Verify and publish checkpoint container / container (push) Successful in 10m34s
This commit is contained in:
1 parent
82c639c977
commit
a1b2aacbac
9 files changed
+371
-23
No files matched your search
@@ -1,7 +1,15 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { runInNewContext } from 'node:vm';
|
||||
import { createElement, isValidElement, type ReactElement, type ReactNode } from 'react';
|
||||
import { renderToStaticMarkup } from 'react-dom/server';
|
||||
import * as jsxRuntime from 'react/jsx-runtime';
|
||||
import ts from 'typescript';
|
||||
import * as board from '../game/board';
|
||||
import * as catalog from '../game/catalog';
|
||||
import * as coalitionAlliance from '../game/coalition-alliance';
|
||||
import * as publicDeals from '../game/public-deals';
|
||||
import { PublicDealList, PublicDeals, publicDealOfferAction, publicDealRequests } from '../components/public-deals';
|
||||
import { TravelPromises } from '../components/travel-promises';
|
||||
import type { Action, GameView } from '../game/engine';
|
||||
@@ -29,7 +37,11 @@ void test('the offer packet retains the original recipient action and validates
|
||||
assert.deepEqual(publicDealOfferAction(['b'], 'Avoid', undefined, 'a', 2, avoid),
|
||||
{type:'dealOffer', target:'b', terms:'Avoid', travel:avoid});
|
||||
assert.equal(publicDealOfferAction(['b','c'], 'Terms', undefined, 'a', 2, travel), null);
|
||||
assert.equal(publicDealOfferAction(['b'], 'Terms', payment, 'a', 2, travel), null);
|
||||
assert.deepEqual(publicDealOfferAction(['b'], 'Terms', payment, 'a', 2, travel),
|
||||
{type:'dealOffer', target:'b', terms:'Terms', payment, travel});
|
||||
assert.equal(publicDealOfferAction(['b'], 'Terms', {...payment,payer:'b',recipient:'a'}, 'a', 2, travel), null);
|
||||
assert.equal(publicDealOfferAction(['b'], 'Terms', {kind:'deferredBribe',payer:'a',recipient:'b',amount:2,dueTurn:2}, 'a', 2, travel), null);
|
||||
assert.equal(publicDealOfferAction(['b','c'], 'Terms', payment, 'a', 2, travel), null);
|
||||
assert.equal(publicDealOfferAction(['a'], 'Terms', undefined, 'a', 2, travel), null);
|
||||
assert.equal(publicDealOfferAction(['b'], 'Terms', undefined, undefined, 2, travel), null);
|
||||
for (const invalid of [
|
||||
@@ -43,6 +55,94 @@ void test('the offer packet retains the original recipient action and validates
|
||||
assert.deepEqual(publicDealOfferAction(['b','c'], 'Terms'), {type:'dealOffer', targets:['b','c'], terms:'Terms'});
|
||||
});
|
||||
|
||||
void test('the combined offer explains payment timing and the recipient can accept or decline', () => {
|
||||
const offered = {...base, deals:[{...deal,status:'offered',payment}], dealTravelStatus:{},
|
||||
bribeOptions:{blocked:null}} as unknown as GameView;
|
||||
const html = markup(offered);
|
||||
assert.match(html, /Alice \(Atreides\) pays 2 spice to Basil \(Harkonnen\) on final acceptance/);
|
||||
assert.match(html, /recipient collects their share at the next Mentat opening/);
|
||||
assert.match(html, /cannot use this payment to fund the current travel/);
|
||||
const sent:Action[] = [];
|
||||
const tree = PublicDealList({game:offered, disabled:false, act(action) {sent.push(action);}});
|
||||
const accept = elements(tree).find(node => node.props.children === 'Accept deal');
|
||||
const decline = elements(tree).find(node => node.props.children === 'Decline deal');
|
||||
assert.ok(accept); assert.ok(decline);
|
||||
assert.equal(accept.props.disabled, false); assert.equal(decline.props.disabled, false);
|
||||
(accept.props.onClick as () => void)();
|
||||
assert.deepEqual(sent, [{type:'dealRespond',deal:'deal',accept:true}]);
|
||||
const busyTree = PublicDealList({game:offered, disabled:true, act() {}});
|
||||
assert.equal(elements(busyTree).find(node => node.props.children === 'Accept deal')?.props.disabled,true);
|
||||
});
|
||||
|
||||
/** Stateful VM exercises the actual authoring callbacks without claiming browser hydration. */
|
||||
function combinedForm() {
|
||||
const states:unknown[] = [], sent:Action[] = [];
|
||||
let cursor = 0;
|
||||
const exported:{PublicDeals?:typeof PublicDeals} = {};
|
||||
runInNewContext(ts.transpileModule(readFileSync(new URL('../components/public-deals.tsx', import.meta.url),'utf8'), {
|
||||
compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022,jsx:ts.JsxEmit.ReactJSX},
|
||||
}).outputText, {exports:exported, require(name:string) {
|
||||
if (name === 'react/jsx-runtime') return jsxRuntime;
|
||||
if (name === 'react') return {useState(initial:unknown) {
|
||||
const index = cursor++;
|
||||
if (!(index in states)) states[index] = typeof initial === 'function' ? initial() : initial;
|
||||
return [states[index], (next:unknown) => {states[index] = typeof next === 'function' ? next(states[index]) : next;}];
|
||||
}, useRef:() => ({current:null}), useEffect:() => {}};
|
||||
if (name === '@/components/ui/button') return {Button:'button'};
|
||||
if (name === '@/game/board') return board;
|
||||
if (name === '@/game/catalog') return catalog;
|
||||
if (name === '@/game/coalition-alliance') return coalitionAlliance;
|
||||
if (name === '@/game/public-deals') return publicDeals;
|
||||
throw new Error(`Unexpected import ${name}`);
|
||||
}});
|
||||
assert.ok(exported.PublicDeals);
|
||||
const game = {...base, me:'a', deals:[]} as GameView;
|
||||
const render = () => {cursor = 0; return elements(exported.PublicDeals!({game,busy:false,act(action) {sent.push(action);}}));};
|
||||
const check = (index:number) => {
|
||||
const node = render().filter(element => element.type === 'input' && element.props.type === 'checkbox')[index];
|
||||
assert.ok(node); assert.equal(node.props.disabled,false);
|
||||
(node.props.onChange as (event:{target:{checked:boolean}}) => void)({target:{checked:true}});
|
||||
};
|
||||
const select = (index:number,value:string) => {
|
||||
const node = render().filter(element => element.type === 'select')[index]; assert.ok(node);
|
||||
(node.props.onChange as (event:{target:{value:string}}) => void)({target:{value}});
|
||||
};
|
||||
return {render, check, select, sent};
|
||||
}
|
||||
|
||||
void test('the authoring controls compose a proposer payment with recipient travel and reject invalid combinations', () => {
|
||||
const ui = combinedForm();
|
||||
ui.check(0); // Select recipient.
|
||||
ui.check(1); // Include payment.
|
||||
ui.select(1,'a'); ui.select(2,'b');
|
||||
ui.check(2); // Include travel while payment is selected.
|
||||
ui.select(5,'carthag');
|
||||
const terms = ui.render().find(node => node.type === 'textarea'); assert.ok(terms);
|
||||
(terms.props.onChange as (event:{target:{value:string}}) => void)({target:{value:'Travel for spice'}});
|
||||
const button = () => ui.render().find(node => node.type === 'button' && node.props.type === 'submit');
|
||||
assert.equal(button()?.props.disabled,false);
|
||||
const form = ui.render().find(node => node.type === 'form'); assert.ok(form);
|
||||
(form.props.onSubmit as (event:{preventDefault():void}) => void)({preventDefault() {}});
|
||||
assert.deepEqual(JSON.parse(JSON.stringify(ui.sent[0])), {type:'dealOffer',target:'b',terms:'Travel for spice',
|
||||
payment:{kind:'immediateBribe',payer:'a',recipient:'b',amount:1},
|
||||
travel:{kind:'ship',territory:'carthag',minimum:1,answer:true}});
|
||||
ui.select(0,'deferredBribe'); assert.equal(button()?.props.disabled,true);
|
||||
ui.select(0,'immediateBribe'); ui.select(1,'b'); ui.select(2,'a');
|
||||
assert.equal(button()?.props.disabled,true);
|
||||
});
|
||||
|
||||
void test('a payment can also be added after selecting the travel term', () => {
|
||||
const ui = combinedForm();
|
||||
ui.check(0); ui.check(2);
|
||||
ui.select(2,'carthag');
|
||||
ui.check(1);
|
||||
ui.select(1,'a'); ui.select(2,'b');
|
||||
const terms = ui.render().find(node => node.type === 'textarea'); assert.ok(terms);
|
||||
(terms.props.onChange as (event:{target:{value:string}}) => void)({target:{value:'Travel for spice'}});
|
||||
const submit = ui.render().find(node => node.type === 'button' && node.props.type === 'submit');
|
||||
assert.equal(submit?.props.disabled,false);
|
||||
});
|
||||
|
||||
void test('the public deal shows the exact recipient, travel intent, turn and authoritative status', () => {
|
||||
const html = markup(base);
|
||||
assert.match(html, /Current-turn travel term/);
|
||||
|
||||
Reference in new issue
Block a user