Skip to content

Commit

Permalink
test(internal): fake vstorage advance blockHeight
Browse files Browse the repository at this point in the history
  • Loading branch information
mhofman committed Feb 4, 2025
1 parent 9555ae0 commit 68416ba
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
18 changes: 15 additions & 3 deletions packages/internal/src/storage-test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ export const makeFakeStorageKit = (rootPath, rootOptions) => {
const resolvedOptions = { sequence: true, ...rootOptions };
/** @type {TotalMap<string, string>} */
const data = new Map();
let currentBlockHeight = 0;

const updateNewCellBlockHeight = (blockHeight = currentBlockHeight + 1) => {
blockHeight > currentBlockHeight ||
Fail`blockHeight ${blockHeight} must be greater than ${currentBlockHeight}`;
currentBlockHeight = blockHeight;
};

/** @param {string} prefix */
const getChildEntries = prefix => {
assert(prefix.endsWith('.'));
Expand Down Expand Up @@ -177,10 +185,13 @@ export const makeFakeStorageKit = (rootPath, rootOptions) => {
streamCell = undefined;
}
}
if (streamCell === undefined) {
if (
streamCell === undefined ||
Number(streamCell.blockHeight) !== currentBlockHeight
) {
streamCell = {
blockHeight: '0',
values: oldVal != null ? [oldVal] : [],
blockHeight: String(currentBlockHeight),
values: !streamCell && oldVal != null ? [oldVal] : [],
};
}
streamCell.values.push(value);
Expand Down Expand Up @@ -219,6 +230,7 @@ export const makeFakeStorageKit = (rootPath, rootOptions) => {
rootNode,
// eslint-disable-next-line object-shorthand
data: /** @type {Map<string, string>} */ (data),
updateNewCellBlockHeight,
getValues,
messages,
toStorage,
Expand Down
30 changes: 25 additions & 5 deletions packages/internal/test/storage-test-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,10 @@ test('makeFakeStorageKit', async t => {

test('makeFakeStorageKit sequence data', async t => {
const rootPath = 'root';
const { rootNode, messages } = makeFakeStorageKit(rootPath, {
sequence: true,
});
const { rootNode, messages, updateNewCellBlockHeight, getValues } =
makeFakeStorageKit(rootPath, {
sequence: true,
});

await t.throwsAsync(
// @ts-expect-error
Expand Down Expand Up @@ -233,20 +234,23 @@ test('makeFakeStorageKit sequence data', async t => {
[{ method: 'append', args: [[deepPath, 'foo']] }],
'auto-sequence grandchild setValue message',
);
t.deepEqual(getValues(deepPath), ['foo']);
deepNode = childNode.makeChildNode('grandchild', { sequence: false });
await deepNode.setValue('bar');
t.deepEqual(
messages.slice(-1),
[{ method: 'set', args: [[deepPath, 'bar']] }],
'manual-single grandchild setValue message',
);
t.throws(() => getValues(deepPath), { name: 'SyntaxError' });
childNode = rootNode.makeChildNode('child', { sequence: false });
await childNode.setValue('bar');
await childNode.setValue(''); // Results in removal
t.deepEqual(
messages.slice(-1),
[{ method: 'set', args: [[childPath, 'bar']] }],
[{ method: 'set', args: [[childPath]] }],
'manual-single child setValue message',
);
t.throws(() => getValues(childPath), { message: /^no data at path/ });
deepNode = childNode.makeChildNode('grandchild');
await deepNode.setValue('baz');
t.deepEqual(
Expand All @@ -261,6 +265,22 @@ test('makeFakeStorageKit sequence data', async t => {
[{ method: 'append', args: [[deepPath, 'qux']] }],
'manual-sequence grandchild setValue message',
);
t.deepEqual(getValues(deepPath), ['baz', 'qux']);
await deepNode.setValue('quux');
t.deepEqual(
messages.slice(-1),
[{ method: 'append', args: [[deepPath, 'quux']] }],
'manual-sequence grandchild setValue message',
);
t.deepEqual(getValues(deepPath), ['baz', 'qux', 'quux']);
updateNewCellBlockHeight();
await deepNode.setValue('quuz');
t.deepEqual(
messages.slice(-1),
[{ method: 'append', args: [[deepPath, 'quuz']] }],
'manual-sequence grandchild setValue message',
);
t.deepEqual(getValues(deepPath), ['quuz']);
});

const testUnmarshaller = test.macro((t, format) => {
Expand Down

0 comments on commit 68416ba

Please sign in to comment.