Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(internal): fake vstorage advance blockHeight #10933

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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] : [],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion for clarifying the StreamCell reset behavior:

        case 'append': {
          trace('toStorage append', message);
          /** @type {StorageEntry[]} */
          const newEntries = message.args;
          for (const [key, value] of newEntries) {
            value != null || Fail`attempt to append with no value`;

            let oldVal = data.get(key);
            let streamCell;
            if (oldVal != null) {
              try {
                streamCell = JSON.parse(oldVal);
                assert(isStreamCell(streamCell));
              } catch (_err) {
                streamCell = undefined;
              }
              // StreamCells reset at block boundaries.
              if (streamCell?.blockHeight !== currentBlockHeight) {
                streamCell = undefined;
                oldVal = undefined;
              }
            }

            if (streamCell === undefined) {
              streamCell = {
                blockHeight: String(currentBlockHeight),
                values: oldVal != null ? [oldVal] : [],
              };
            }

            streamCell.values.push(value);
            data.set(key, JSON.stringify(streamCell));
          }
          break;
        }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry this was set on auto merge. I did consider something along those lines. I agree it does read better

};
}
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
Loading