Skip to content

Commit

Permalink
Merge 1731328 into 4bf203c
Browse files Browse the repository at this point in the history
  • Loading branch information
hughluo committed Oct 7, 2022
2 parents 4bf203c + 1731328 commit dc50e39
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 3 deletions.
56 changes: 56 additions & 0 deletions src/core/reducer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,62 @@ describe('Random inside setup()', () => {
});
});

describe('redact', () => {
const game: Game = {
setup: () => ({
isASecret: false,
}),
moves: {
A: {
move: (G) => G,
redact: (G) => G.isASecret,
},
B: (G) => {
return { ...G, isASecret: true };
},
},
};

const reducer = CreateGameReducer({ game });

let state = InitializeGame({ game });

test('move A is not secret and is not redact', () => {
state = reducer(state, makeMove('A', ['not redact'], '0'));
expect(state.G).toMatchObject({
isASecret: false,
});
const [lastLogEntry] = state.deltalog.slice(-1);
expect(lastLogEntry).toMatchObject({
action: {
payload: {
type: 'A',
args: ['not redact'],
},
},
redact: false,
});
});

test('move A is secret and is redact', () => {
state = reducer(state, makeMove('B', ['not redact'], '0'));
state = reducer(state, makeMove('A', ['redact'], '0'));
expect(state.G).toMatchObject({
isASecret: true,
});
const [lastLogEntry] = state.deltalog.slice(-1);
expect(lastLogEntry).toMatchObject({
action: {
payload: {
type: 'A',
args: ['redact'],
},
},
redact: true,
});
});
});

describe('undo / redo', () => {
const game: Game = {
seed: 0,
Expand Down
14 changes: 12 additions & 2 deletions src/core/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,18 @@ function initializeDeltalog(
logEntry.metadata = pluginLogMetadata;
}

if (typeof move === 'object' && move.redact === true) {
logEntry.redact = true;
function IsRedactFunction(
redact: boolean | ((...args: any[]) => any)
): redact is (...args: any[]) => any {
return redact instanceof Function;
}

if (typeof move === 'object') {
if (IsRedactFunction(move.redact)) {
logEntry.redact = move.redact(state.G, state.ctx);
} else if (move.redact === true) {
logEntry.redact = true;
}
}

return {
Expand Down
71 changes: 71 additions & 0 deletions src/master/filter-player-view.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,74 @@ describe('redactLog', () => {
]);
});
});

test('make move args to be secret depends on G via conditional redact', async () => {
const game = {
setup: () => ({
isASecret: false,
}),
moves: {
A: {
move: (G) => G,
redact: (G) => G.isASecret,
},
B: (G) => {
return { ...G, isASecret: true };
},
},
};

const send = jest.fn();
const master = new Master(game, new InMemory(), TransportAPI(send));
const filterPlayerView = getFilterPlayerView(game);

const actionA0 = ActionCreators.makeMove('A', ['not redacted'], '0');
const actionB = ActionCreators.makeMove('B', ['not redacted'], '0');
const actionA1 = ActionCreators.makeMove('A', ['redacted'], '0');

// test: ping-pong two moves, then sync and check the log
await master.onSync('matchID', '0', undefined, 2);
await master.onUpdate(actionA0, 0, 'matchID', '0');
await master.onUpdate(actionB, 1, 'matchID', '0');
await master.onUpdate(actionA1, 2, 'matchID', '0');
await master.onSync('matchID', '1', undefined, 2);

const payload = send.mock.calls[send.mock.calls.length - 1][0];
expect(
(filterPlayerView('1', payload).args[1] as SyncInfo).log
).toMatchObject([
{
action: {
type: 'MAKE_MOVE',
payload: {
type: 'A',
args: ['not redacted'],
playerID: '0',
},
},
_stateID: 0,
},
{
action: {
type: 'MAKE_MOVE',
payload: {
type: 'B',
args: ['not redacted'],
playerID: '0',
},
},
_stateID: 1,
},
{
action: {
type: 'MAKE_MOVE',
payload: {
type: 'A',
args: null,
playerID: '0',
},
},
_stateID: 2,
},
]);
});
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export interface LongFormMove<
CtxWithPlugins extends Ctx = Ctx
> {
move: MoveFn<G, CtxWithPlugins>;
redact?: boolean;
redact?: boolean | ((G: G, ctx: CtxWithPlugins) => boolean);
noLimit?: boolean;
client?: boolean;
undoable?: boolean | ((G: G, ctx: CtxWithPlugins) => boolean);
Expand Down

0 comments on commit dc50e39

Please sign in to comment.