Skip to content

Commit

Permalink
fix: attr to false on server (#518)
Browse files Browse the repository at this point in the history
  • Loading branch information
npaton committed Mar 16, 2024
1 parent 144f928 commit c8319ce
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/fix-attr-set-false.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@empirica/core": patch
---

Fix a bug where setting an attribute to `false` on the server, after it was
initially set to true on the client, would not work as expected.
1 change: 1 addition & 0 deletions lib/@empirica/core/src/shared/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ export class Attribute {
}

this.attr = attr;
this.serVal = attr?.val === undefined || attr?.val === null ? "" : attr.val;
let value: JsonValue | undefined = undefined;
if (this.attr?.val) {
value = JSON.parse(this.attr.val);
Expand Down
5 changes: 4 additions & 1 deletion tests/stress/experiment/server/src/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ Empirica.onGameStart(({ game }) => {

Empirica.onRoundStart(({ round }) => {});

Empirica.onStageStart(({ stage }) => {});
Empirica.onStageStart(({ stage }) => {
// Used for test called: "attribute as bool, correct equality check"
stage.currentGame.set("key1", false);
});

Empirica.onStageEnded(({ stage }) => {});

Expand Down
48 changes: 47 additions & 1 deletion tests/stress/tests/attributes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
const { test } = require("@playwright/test");
import { Context } from "./context";
import { adminNewBatch, quickGame } from "./admin";
import { playerStart, submitStage, waitGameFinished } from "./player";
import {
playerStart,
submitStage,
waitGameFinished,
waitNextStage,
} from "./player";
import { sleep } from "./utils";

// At the moment, we use the same empirica server for all tests, so we need to
Expand Down Expand Up @@ -61,3 +66,44 @@ test("attribute as object, correct equality check", async ({ browser }) => {

await ctx.close();
});

test("attribute as bool, correct equality check", async ({ browser }) => {
const ctx = new Context(browser);

const playerCount = 2;
const roundCount = 1;
const stageCount = 2;

ctx.logMatching(/mutObj/);
ctx.logMatching(/key1/);
ctx.logMatching(/ set/);

await ctx.start();
await ctx.addPlayers(playerCount);
ctx.players[0].logWS();
ctx.players[1].logWS();

await ctx.applyAdmin(
adminNewBatch({
treatmentConfig: quickGame(playerCount, roundCount, stageCount),
})
);

await ctx.applyPlayers(playerStart);

// Initial value
await ctx.players[0].set("game", "key1", true);
await ctx.expectPlayers("game", "key1", true);

await ctx.applyPlayers(submitStage);
await ctx.applyPlayers(waitNextStage);

// We mutate key1 from true in the previous stage to false in the onStageStart
// callback, here we verify that value is updated.
await ctx.expectPlayers("game", "key1", false);

await ctx.applyPlayers(submitStage);
await ctx.applyPlayers(waitGameFinished);

await ctx.close();
});
2 changes: 1 addition & 1 deletion tests/stress/tests/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export class Player extends Actor {
await sleep(options.interval);
}

throw new Error(`value not as expected (${kind}/${key}${value})`);
throw new Error(`value not as expected (${kind}.${key}=${value})`);
}

async valueEquals(kind, key, value) {
Expand Down

0 comments on commit c8319ce

Please sign in to comment.