diff --git a/.changelog/6053-new-graph-store.md b/.changelog/6053-new-graph-store.md index 1891e1e8c1..2d6cd88e05 100644 --- a/.changelog/6053-new-graph-store.md +++ b/.changelog/6053-new-graph-store.md @@ -1,3 +1,3 @@ ### Added -- Added new JSON-LD based graph backend store, in PR [#6053](https://github.com/microsoft/BotFramework-WebChat/pull/6053), by [@compulim](https://github.com/compulim) +- Added new JSON-LD based graph backend store, in PR [#6053](https://github.com/microsoft/BotFramework-WebChat/pull/6053) and [#6093](https://github.com/microsoft/BotFramework-WebChat/pull/6093), by [@compulim](https://github.com/compulim) diff --git a/.github/workflows/pull-request-validation.yml b/.github/workflows/pull-request-validation.yml index 5f0e1e819f..28c9905f28 100644 --- a/.github/workflows/pull-request-validation.yml +++ b/.github/workflows/pull-request-validation.yml @@ -264,9 +264,14 @@ jobs: --logHeapUsage \ --runInBand \ --silent \ - '--testPathPattern=/packages/(?!store/)' + '--testPathPattern=/packages/(?!store(-graph)?/)' timeout-minutes: 10 + - name: Run node --test under /packages/store-graph/ + run: npm test + timeout-minutes: 5 + working-directory: ./packages/store-graph/ + - name: Run node --test under /packages/store/ run: npm test timeout-minutes: 5 diff --git a/package-lock.json b/package-lock.json index 2011231c46..648f5c9a79 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "packages/test/dev-server", "packages/test/harness", "packages/test/web-server", + "packages/store-graph", "packages/store", "packages/core-debug-api", "packages/core-graph", @@ -4083,6 +4084,10 @@ "resolved": "packages/redux-store", "link": true }, + "node_modules/@msinternal/botframework-webchat-store-graph": { + "resolved": "packages/store-graph", + "link": true + }, "node_modules/@msinternal/botframework-webchat-styles": { "resolved": "packages/styles", "link": true @@ -22558,12 +22563,38 @@ "dependencies": { "valibot": "1.2.0" }, + "devDependencies": { + "@msinternal/botframework-webchat-store-graph": "0.0.0-0", + "@types/node": "^25.9.1" + } + }, + "packages/store/node_modules/valibot": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/valibot/-/valibot-1.2.0.tgz", + "integrity": "sha512-mm1rxUsmOxzrwnX5arGS+U4T25RdvpPjPN4yR0u9pUBov9+zGVtO84tif1eY4r6zWxVxu3KzIyknJy3rxfRZZg==", + "license": "MIT", + "peerDependencies": { + "typescript": ">=5" + }, + "peerDependenciesMeta": { + "typescript": { + "optional": true + } + } + }, + "packages/store-graph": { + "name": "@msinternal/botframework-webchat-store-graph", + "version": "0.0.0-0", + "license": "MIT", + "dependencies": { + "valibot": "1.2.0" + }, "devDependencies": { "@types/node": "^25.9.1", "type-fest": "^5.6.0" } }, - "packages/store/node_modules/type-fest": { + "packages/store-graph/node_modules/type-fest": { "version": "5.10.0", "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-5.10.0.tgz", "integrity": "sha512-NoSdpq/WEiAg5sjmBkmV/hfxv6HJH4NqPNrqjtSO5CwRmpsDfaf4begxW34KdJykH/l1yHtwBWQkCRdoXO8mPA==", @@ -22579,7 +22610,7 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "packages/store/node_modules/valibot": { + "packages/store-graph/node_modules/valibot": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/valibot/-/valibot-1.2.0.tgz", "integrity": "sha512-mm1rxUsmOxzrwnX5arGS+U4T25RdvpPjPN4yR0u9pUBov9+zGVtO84tif1eY4r6zWxVxu3KzIyknJy3rxfRZZg==", diff --git a/package.json b/package.json index e9f5d241f9..62e0fd9975 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "packages/test/dev-server", "packages/test/harness", "packages/test/web-server", + "packages/store-graph", "packages/store", "packages/core-debug-api", "packages/core-graph", @@ -113,6 +114,7 @@ "precommit:eslint:repack-react@baseline": "cd packages && cd repack && cd react@baseline && npm run precommit:eslint", "precommit:eslint:repack-react@umd": "cd packages && cd repack && cd react@umd && npm run precommit:eslint", "precommit:eslint:store": "cd packages && cd store && npm run precommit:eslint", + "precommit:eslint:store-graph": "cd packages && cd store-graph && npm run precommit:eslint", "precommit:eslint:styles": "cd packages && cd styles && npm run precommit:eslint", "precommit:eslint:support-cldr-data": "cd packages && cd support && cd cldr-data && npm run precommit:eslint", "precommit:eslint:support-cldr-data-downloader": "cd packages && cd support && cd cldr-data-downloader && npm run precommit:eslint", @@ -149,6 +151,7 @@ "precommit:typecheck:repack-react@baseline": "cd packages && cd repack && cd react@baseline && npm run precommit:typecheck", "precommit:typecheck:repack-react@umd": "cd packages && cd repack && cd react@umd && npm run precommit:typecheck", "precommit:typecheck:store": "cd packages && cd store && npm run precommit:typecheck", + "precommit:typecheck:store-graph": "cd packages && cd store-graph && npm run precommit:typecheck", "precommit:typecheck:styles": "cd packages && cd styles && npm run precommit:typecheck", "prepare": "husky", "start": "cross-env NODE_OPTIONS=--no-deprecation concurrently --kill-others --prefix-colors \"auto\" \"npm:start:*\"", diff --git a/packages/store-graph/.eslintrc.yml b/packages/store-graph/.eslintrc.yml new file mode 100644 index 0000000000..1aa1350134 --- /dev/null +++ b/packages/store-graph/.eslintrc.yml @@ -0,0 +1,6 @@ +extends: + - ../../.eslintrc.production.yml + +# This package is compatible with web browser. +env: + browser: true diff --git a/packages/store-graph/.gitignore b/packages/store-graph/.gitignore new file mode 100644 index 0000000000..365f971e5b --- /dev/null +++ b/packages/store-graph/.gitignore @@ -0,0 +1,4 @@ +/.env +/dist +/node_modules +/static diff --git a/packages/store-graph/package.json b/packages/store-graph/package.json new file mode 100644 index 0000000000..7eea4ae946 --- /dev/null +++ b/packages/store-graph/package.json @@ -0,0 +1,39 @@ +{ + "name": "@msinternal/botframework-webchat-store-graph", + "version": "0.0.0-0", + "description": "", + "license": "MIT", + "private": true, + "author": "", + "exports": { + ".": { + "import": { + "types": "./dist/botframework-webchat-store-graph.d.mts", + "default": "./dist/botframework-webchat-store-graph.mjs" + }, + "require": { + "types": "./dist/botframework-webchat-store-graph.d.ts", + "default": "./dist/botframework-webchat-store-graph.js" + } + } + }, + "scripts": { + "benchmark": "node --disable-warning=MODULE_TYPELESS_PACKAGE_JSON --expose-gc ./src/benchmark.ts", + "benchmark:json": "node --disable-warning=MODULE_TYPELESS_PACKAGE_JSON ./src/benchmark.json.ts", + "benchmark:node": "node --disable-warning=MODULE_TYPELESS_PACKAGE_JSON ./src/benchmark.node.ts", + "build": "npm run build:run", + "build:run": "tsdown", + "precommit": "npm run precommit:eslint -- src && npm run precommit:typecheck", + "precommit:eslint": "../../node_modules/.bin/eslint --report-unused-disable-directives --max-warnings 0", + "precommit:typecheck": "tsc --project ./src --emitDeclarationOnly false --esModuleInterop true --noEmit --pretty false", + "test": "npm run test:node", + "test:node": "node --test" + }, + "dependencies": { + "valibot": "1.2.0" + }, + "devDependencies": { + "@types/node": "^25.9.1", + "type-fest": "^5.6.0" + } +} diff --git a/packages/store/src/Store.test.ts b/packages/store-graph/src/Graph.test.ts similarity index 72% rename from packages/store/src/Store.test.ts rename to packages/store-graph/src/Graph.test.ts index bd31f61bbb..cb246c5343 100644 --- a/packages/store/src/Store.test.ts +++ b/packages/store-graph/src/Graph.test.ts @@ -3,7 +3,7 @@ import { expect } from 'expect'; import NodeTest from 'node:test'; import type { VettedNode } from './node/Node.ts'; import type { NodeIdentifier } from './node/NodeIdentifier.ts'; -import Store, { type GetSortedPartsInit, type StoreController, type StoreTransaction } from './Store.ts'; +import Graph, { type GetSortedPartsInit, type GraphController, type GraphTransaction } from './Graph.ts'; function catchError(fn: () => void): unknown { try { @@ -15,10 +15,10 @@ function catchError(fn: () => void): unknown { return undefined; } -function createStore() { - let controller: StoreController | undefined; +function createGraph() { + let controller: GraphController | undefined; - const store = new Store({ + const graph = new Graph({ start(nextController) { controller = nextController; } @@ -28,7 +28,7 @@ function createStore() { throw new Error('Controller should be set after start'); } - return { controller, store }; + return { controller, graph }; } /** @@ -51,8 +51,8 @@ function getChangedNodeIds(before: ReadonlyMap, after: Readonly .sort(); } -function createStoreWithNodes() { - const { controller, store } = createStore(); +function createGraphWithNodes() { + const { controller, graph } = createGraph(); controller.edit(transaction => { transaction.upsert('_:conversation', () => ({ @@ -78,14 +78,14 @@ function createStoreWithNodes() { })); }); - return { controller, store }; + return { controller, graph }; } scenario( 'upsert', bdd => { bdd - .given('an empty store', () => createStore()) + .given('an empty graph', () => createGraph()) .when('a node is upserted', ({ controller }) => { const nodesPassedToUpdater: unknown[] = []; @@ -102,8 +102,8 @@ scenario( .then('updater should receive undefined', (_, nodesPassedToUpdater) => expect(nodesPassedToUpdater).toEqual([undefined]) ) - .and('should add the node', ({ store }) => - expect(store.getSnapshot().get('_:m-1')).toEqual({ + .and('should add the node', ({ graph }) => + expect(graph.getSnapshot().get('_:m-1')).toEqual({ '@id': '_:m-1', '@type': new Set(['Message']), text: ['Hello'] @@ -111,7 +111,7 @@ scenario( ); bdd - .given('a store with a node', () => createStoreWithNodes()) + .given('a graph with a node', () => createGraphWithNodes()) .when('the node is updated', ({ controller }) => { const nodesPassedToUpdater: unknown[] = []; @@ -128,12 +128,12 @@ scenario( .then('updater should receive the existing node', (_, nodesPassedToUpdater) => expect(nodesPassedToUpdater).toEqual([expect.objectContaining({ '@id': '_:m-1', text: ['Hello'] })]) ) - .and('should update the node', ({ store }) => - expect(store.getSnapshot().get('_:m-1')).toHaveProperty('text', ['Aloha']) + .and('should update the node', ({ graph }) => + expect(graph.getSnapshot().get('_:m-1')).toHaveProperty('text', ['Aloha']) ); bdd - .given('a store with a node', () => createStoreWithNodes()) + .given('a graph with a node', () => createGraphWithNodes()) .when('the updater changes the @id', ({ controller }) => { try { controller.edit(transaction => transaction.upsert('_:m-1', node => ({ ...node!, '@id': '_:m-999' }))); @@ -144,11 +144,11 @@ scenario( return undefined; }) .then('should throw', (_, error) => expect(error).toEqual(expect.any(Error))) - .and('should not add the node with new @id', ({ store }) => - expect(store.getSnapshot().has('_:m-999')).toBe(false) + .and('should not add the node with new @id', ({ graph }) => + expect(graph.getSnapshot().has('_:m-999')).toBe(false) ) - .and('should keep the original node', ({ store }) => - expect(store.getSnapshot().get('_:m-1')).toHaveProperty('text', ['Hello']) + .and('should keep the original node', ({ graph }) => + expect(graph.getSnapshot().get('_:m-1')).toHaveProperty('text', ['Hello']) ); }, NodeTest @@ -158,43 +158,43 @@ scenario( 'delete', bdd => { bdd - .given('a store with cross-referencing nodes', () => createStoreWithNodes()) - .when('a node is deleted', ({ controller, store }) => { - const snapshot = store.getSnapshot(); + .given('a graph with cross-referencing nodes', () => createGraphWithNodes()) + .when('a node is deleted', ({ controller, graph }) => { + const snapshot = graph.getSnapshot(); controller.edit(transaction => transaction.delete('_:m-1')); return snapshot; }) - .then('should only modify nodes which referenced the deleted node', ({ store }, snapshot) => - expect(getChangedNodeIds(snapshot, store.getSnapshot())).toEqual(['_:conversation', '_:m-2']) + .then('should only modify nodes which referenced the deleted node', ({ graph }, snapshot) => + expect(getChangedNodeIds(snapshot, graph.getSnapshot())).toEqual(['_:conversation', '_:m-2']) ) - .and('should remove the node', ({ store }) => expect(store.getSnapshot().has('_:m-1')).toBe(false)) - .and('should remove the reference from hasPart', ({ store }) => - expect(store.getSnapshot().get('_:conversation')).toHaveProperty('hasPart', new Set([{ '@id': '_:m-2' }])) + .and('should remove the node', ({ graph }) => expect(graph.getSnapshot().has('_:m-1')).toBe(false)) + .and('should remove the reference from hasPart', ({ graph }) => + expect(graph.getSnapshot().get('_:conversation')).toHaveProperty('hasPart', new Set([{ '@id': '_:m-2' }])) ) - .and('should remove the reference from other property but keep other values', ({ store }) => - expect(store.getSnapshot().get('_:m-2')).toHaveProperty('citation', ['https://example.com/']) + .and('should remove the reference from other property but keep other values', ({ graph }) => + expect(graph.getSnapshot().get('_:m-2')).toHaveProperty('citation', ['https://example.com/']) ) - .and('should remove the property if it become empty', ({ store }) => - expect(store.getSnapshot().get('_:m-2')).not.toHaveProperty('mentions') + .and('should remove the property if it become empty', ({ graph }) => + expect(graph.getSnapshot().get('_:m-2')).not.toHaveProperty('mentions') ) - .and('should keep unrelated properties', ({ store }) => - expect(store.getSnapshot().get('_:m-2')).toHaveProperty('text', ['World']) + .and('should keep unrelated properties', ({ graph }) => + expect(graph.getSnapshot().get('_:m-2')).toHaveProperty('text', ['World']) ); bdd - .given('a store with cross-referencing nodes', () => createStoreWithNodes()) + .given('a graph with cross-referencing nodes', () => createGraphWithNodes()) .when('a node referenced by other nodes is deleted', ({ controller }) => controller.edit(transaction => transaction.delete('_:conversation')) ) - .then('should remove isPartOf property', ({ store }) => { - expect(store.getSnapshot().get('_:m-1')).not.toHaveProperty('isPartOf'); - expect(store.getSnapshot().get('_:m-2')).not.toHaveProperty('isPartOf'); + .then('should remove isPartOf property', ({ graph }) => { + expect(graph.getSnapshot().get('_:m-1')).not.toHaveProperty('isPartOf'); + expect(graph.getSnapshot().get('_:m-2')).not.toHaveProperty('isPartOf'); }); bdd - .given('a store with cross-referencing nodes', () => createStoreWithNodes()) + .given('a graph with cross-referencing nodes', () => createGraphWithNodes()) .when('a node is deleted and upserted in the same transaction', ({ controller }) => { const nodesPassedToUpdater: unknown[] = []; @@ -212,8 +212,8 @@ scenario( .then('updater should receive undefined', (_, nodesPassedToUpdater) => expect(nodesPassedToUpdater).toEqual([undefined]) ) - .and('should have the new node', ({ store }) => - expect(store.getSnapshot().get('_:m-1')).toHaveProperty('text', ['Again']) + .and('should have the new node', ({ graph }) => + expect(graph.getSnapshot().get('_:m-1')).toHaveProperty('text', ['Again']) ); }, NodeTest @@ -223,13 +223,13 @@ scenario( 'transaction', bdd => { bdd - .given('a store with cross-referencing nodes and a listener', () => { - const { controller, store } = createStoreWithNodes(); + .given('a graph with cross-referencing nodes and a listener', () => { + const { controller, graph } = createGraphWithNodes(); const numCalls = { value: 0 }; - store.subscribe(() => numCalls.value++); + graph.subscribe(() => numCalls.value++); - return { controller, numCalls, store, snapshot: store.getSnapshot() }; + return { controller, numCalls, graph, snapshot: graph.getSnapshot() }; }) .when('the transactor throws after edits', ({ controller }) => { try { @@ -247,14 +247,14 @@ scenario( return undefined; }) .then('should rethrow the error', (_, error) => expect(error).toHaveProperty('message', 'Artificial')) - .and('should keep the same snapshot', ({ snapshot, store }) => expect(store.getSnapshot()).toBe(snapshot)) - .and('should not update the node', ({ store }) => - expect(store.getSnapshot().get('_:m-1')).toHaveProperty('text', ['Hello']) - ) - .and('should not add the node', ({ store }) => expect(store.getSnapshot().has('_:m-3')).toBe(false)) - .and('should not delete the node', ({ store }) => expect(store.getSnapshot().has('_:m-2')).toBe(true)) - .and('should not remove references', ({ store }) => - expect(store.getSnapshot().get('_:conversation')).toHaveProperty( + .and('should keep the same snapshot', ({ snapshot, graph }) => expect(graph.getSnapshot()).toBe(snapshot)) + .and('should not update the node', ({ graph }) => + expect(graph.getSnapshot().get('_:m-1')).toHaveProperty('text', ['Hello']) + ) + .and('should not add the node', ({ graph }) => expect(graph.getSnapshot().has('_:m-3')).toBe(false)) + .and('should not delete the node', ({ graph }) => expect(graph.getSnapshot().has('_:m-2')).toBe(true)) + .and('should not remove references', ({ graph }) => + expect(graph.getSnapshot().get('_:conversation')).toHaveProperty( 'hasPart', new Set([{ '@id': '_:m-1' }, { '@id': '_:m-2' }]) ) @@ -262,18 +262,18 @@ scenario( .and('should not notify', ({ numCalls }) => expect(numCalls.value).toBe(0)); bdd - .given('a store with cross-referencing nodes and a listener', () => { - const { controller, store } = createStoreWithNodes(); + .given('a graph with cross-referencing nodes and a listener', () => { + const { controller, graph } = createGraphWithNodes(); const numCalls = { value: 0 }; - store.subscribe(() => numCalls.value++); + graph.subscribe(() => numCalls.value++); - return { controller, numCalls, store, snapshot: store.getSnapshot() }; + return { controller, numCalls, graph, snapshot: graph.getSnapshot() }; }) .when('the transaction succeed', ({ controller }) => controller.edit(transaction => transaction.upsert('_:m-1', node => ({ ...node!, text: ['Aloha'] }))) ) - .then('should have a new snapshot', ({ snapshot, store }) => expect(store.getSnapshot()).not.toBe(snapshot)) + .then('should have a new snapshot', ({ snapshot, graph }) => expect(graph.getSnapshot()).not.toBe(snapshot)) .and('should notify once', ({ numCalls }) => expect(numCalls.value).toBe(1)); }, NodeTest @@ -283,7 +283,7 @@ scenario( 'upsert with node reference', bdd => { bdd - .given('an empty store', () => createStore()) + .given('an empty graph', () => createGraph()) .when('a node with a full node in a property is upserted', ({ controller }) => { try { controller.edit(transaction => @@ -300,10 +300,10 @@ scenario( return undefined; }) .then('should throw', (_, error) => expect(error).toEqual(expect.any(Error))) - .and('should not add the node', ({ store }) => expect(store.getSnapshot().has('_:m-1')).toBe(false)); + .and('should not add the node', ({ graph }) => expect(graph.getSnapshot().has('_:m-1')).toBe(false)); bdd - .given('an empty store', () => createStore()) + .given('an empty graph', () => createGraph()) .when('a node with a full node in hasPart is upserted', ({ controller }) => { try { controller.edit(transaction => @@ -320,10 +320,10 @@ scenario( return undefined; }) .then('should throw', (_, error) => expect(error).toEqual(expect.any(Error))) - .and('should not add the node', ({ store }) => expect(store.getSnapshot().has('_:conversation')).toBe(false)); + .and('should not add the node', ({ graph }) => expect(graph.getSnapshot().has('_:conversation')).toBe(false)); bdd - .given('an empty store', () => createStore()) + .given('an empty graph', () => createGraph()) .when('a node with a node reference is upserted', ({ controller }) => controller.edit(transaction => { transaction.upsert('https://example.com/p-1', () => ({ @@ -338,8 +338,8 @@ scenario( })); }) ) - .then('should add the node', ({ store }) => - expect(store.getSnapshot().get('_:m-1')).toHaveProperty('mentions', [{ '@id': 'https://example.com/p-1' }]) + .then('should add the node', ({ graph }) => + expect(graph.getSnapshot().get('_:m-1')).toHaveProperty('mentions', [{ '@id': 'https://example.com/p-1' }]) ); }, NodeTest @@ -349,8 +349,8 @@ scenario( 'reference tracking', bdd => { bdd - .given('a store with cross-referencing nodes', () => createStoreWithNodes()) - .when('a reference is removed by upsert, then the referenced node is deleted', ({ controller, store }) => { + .given('a graph with cross-referencing nodes', () => createGraphWithNodes()) + .when('a reference is removed by upsert, then the referenced node is deleted', ({ controller, graph }) => { controller.edit(transaction => { transaction.upsert('_:m-2', node => { const { citation: _citation, mentions: _mentions, ...rest } = node!; @@ -359,19 +359,19 @@ scenario( }); }); - const snapshot = store.getSnapshot(); + const snapshot = graph.getSnapshot(); controller.edit(transaction => transaction.delete('_:m-1')); return snapshot; }) - .then('should only modify nodes which still reference it', ({ store }, snapshot) => - expect(getChangedNodeIds(snapshot, store.getSnapshot())).toEqual(['_:conversation']) + .then('should only modify nodes which still reference it', ({ graph }, snapshot) => + expect(getChangedNodeIds(snapshot, graph.getSnapshot())).toEqual(['_:conversation']) ); bdd - .given('a store with cross-referencing nodes', () => createStoreWithNodes()) - .when('a reference is added by upsert, then the referenced node is deleted', ({ controller, store }) => { + .given('a graph with cross-referencing nodes', () => createGraphWithNodes()) + .when('a reference is added by upsert, then the referenced node is deleted', ({ controller, graph }) => { controller.edit(transaction => { transaction.upsert('_:m-3', () => ({ '@id': '_:m-3', @@ -380,22 +380,22 @@ scenario( })); }); - const snapshot = store.getSnapshot(); + const snapshot = graph.getSnapshot(); controller.edit(transaction => transaction.delete('_:m-2')); return snapshot; }) - .then('should modify the newly referencing node', ({ store }, snapshot) => - expect(getChangedNodeIds(snapshot, store.getSnapshot())).toEqual(['_:conversation', '_:m-3']) + .then('should modify the newly referencing node', ({ graph }, snapshot) => + expect(getChangedNodeIds(snapshot, graph.getSnapshot())).toEqual(['_:conversation', '_:m-3']) ) - .and('should remove the reference from the new node', ({ store }) => - expect(store.getSnapshot().get('_:m-3')).not.toHaveProperty('mentions') + .and('should remove the reference from the new node', ({ graph }) => + expect(graph.getSnapshot().get('_:m-3')).not.toHaveProperty('mentions') ); bdd - .given('a store with cross-referencing nodes', () => createStoreWithNodes()) - .when('a failed transaction added a reference, then the referenced node is deleted', ({ controller, store }) => { + .given('a graph with cross-referencing nodes', () => createGraphWithNodes()) + .when('a failed transaction added a reference, then the referenced node is deleted', ({ controller, graph }) => { try { controller.edit(transaction => { transaction.upsert('_:m-3', () => ({ @@ -410,22 +410,22 @@ scenario( // Intentionally left blank. } - const snapshot = store.getSnapshot(); + const snapshot = graph.getSnapshot(); controller.edit(transaction => transaction.delete('_:m-1')); return snapshot; }) - .then('should not have the node from the failed transaction', ({ store }) => - expect(store.getSnapshot().has('_:m-3')).toBe(false) + .then('should not have the node from the failed transaction', ({ graph }) => + expect(graph.getSnapshot().has('_:m-3')).toBe(false) ) - .and('should only modify nodes which referenced it', ({ store }, snapshot) => - expect(getChangedNodeIds(snapshot, store.getSnapshot())).toEqual(['_:conversation', '_:m-2']) + .and('should only modify nodes which referenced it', ({ graph }, snapshot) => + expect(getChangedNodeIds(snapshot, graph.getSnapshot())).toEqual(['_:conversation', '_:m-2']) ); bdd - .given('a store with a self-referencing node', () => { - const { controller, store } = createStore(); + .given('a graph with a self-referencing node', () => { + const { controller, graph } = createGraph(); controller.edit(transaction => transaction.upsert('_:m-1', () => ({ @@ -435,10 +435,10 @@ scenario( })) ); - return { controller, store }; + return { controller, graph }; }) .when('the node is deleted', ({ controller }) => controller.edit(transaction => transaction.delete('_:m-1'))) - .then('should remove the node', ({ store }) => expect(store.getSnapshot().size).toBe(0)); + .then('should remove the node', ({ graph }) => expect(graph.getSnapshot().size).toBe(0)); }, NodeTest ); @@ -447,9 +447,9 @@ scenario( 'deep freeze', bdd => { bdd - .given('a store with cross-referencing nodes', () => createStoreWithNodes()) - .when('a Set in a node is modified', ({ store }) => { - const types = store.getSnapshot().get('_:m-2')?.['@type']; + .given('a graph with cross-referencing nodes', () => createGraphWithNodes()) + .when('a Set in a node is modified', ({ graph }) => { + const types = graph.getSnapshot().get('_:m-2')?.['@type']; return [ () => (types as Set).add('Thing'), @@ -462,28 +462,28 @@ scenario( expect(modify).toThrow(TypeError); } }) - .and('should not be modified', ({ store }) => - expect(store.getSnapshot().get('_:m-2')?.['@type']).toEqual(new Set(['Message'])) + .and('should not be modified', ({ graph }) => + expect(graph.getSnapshot().get('_:m-2')?.['@type']).toEqual(new Set(['Message'])) ); bdd .given('a mutable Set', () => new Set(['Message'])) .when('a node with the Set is upserted and the Set is modified afterward', types => { - const { controller, store } = createStore(); + const { controller, graph } = createGraph(); controller.edit(transaction => transaction.upsert('_:m-1', () => ({ '@id': '_:m-1', '@type': types }))); types.add('Thing'); - return store; + return graph; }) - .then('should not affect the node', (_, store) => - expect(store.getSnapshot().get('_:m-1')?.['@type']).toEqual(new Set(['Message'])) + .then('should not affect the node', (_, graph) => + expect(graph.getSnapshot().get('_:m-1')?.['@type']).toEqual(new Set(['Message'])) ); bdd - .given('a store with cross-referencing nodes', () => createStoreWithNodes()) - .when('a node is retrieved', ({ store }) => store.getSnapshot().get('_:m-2')) + .given('a graph with cross-referencing nodes', () => createGraphWithNodes()) + .when('a node is retrieved', ({ graph }) => graph.getSnapshot().get('_:m-2')) .then('should freeze the node', (_, node) => expect(Object.isFrozen(node)).toBe(true)) .and('should freeze property values', (_, node) => { expect(Object.isFrozen(getPath(node, '@type'))).toBe(true); @@ -496,11 +496,11 @@ scenario( }); bdd - .given('a store with cross-referencing nodes', () => createStoreWithNodes()) - .when('a referenced node is deleted', ({ controller, store }) => { + .given('a graph with cross-referencing nodes', () => createGraphWithNodes()) + .when('a referenced node is deleted', ({ controller, graph }) => { controller.edit(transaction => transaction.delete('_:m-1')); - return store.getSnapshot().get('_:m-2'); + return graph.getSnapshot().get('_:m-2'); }) .then('should freeze the node with reference removed', (_, node) => { expect(Object.isFrozen(node)).toBe(true); @@ -510,13 +510,13 @@ scenario( bdd .given('a JSON literal', () => ({ '@type': '@json' as const, '@value': { nested: { value: [1, 2, 3] } } })) .when('a node with the JSON literal is upserted', jsonLiteral => { - const { controller, store } = createStore(); + const { controller, graph } = createGraph(); controller.edit(transaction => transaction.upsert('_:m-1', () => ({ '@id': '_:m-1', '@type': new Set(['Message']), data: [jsonLiteral] })) ); - return getPath(store.getSnapshot().get('_:m-1'), 'data', 0); + return getPath(graph.getSnapshot().get('_:m-1'), 'data', 0); }) .then('should freeze deeply into the JSON literal', (_, jsonLiteral) => { expect(Object.isFrozen(jsonLiteral)).toBe(true); @@ -528,12 +528,12 @@ scenario( }); bdd - .given('a store and a recursive JSON value', () => { + .given('a graph and a recursive JSON value', () => { const value: { self?: unknown } = {}; value.self = value; - return { ...createStore(), value }; + return { ...createGraph(), value }; }) .when('a node with the recursive JSON value is upserted', ({ controller, value }) => catchError(() => @@ -548,15 +548,15 @@ scenario( ) ) .then('should throw', (_, error) => expect(error).toEqual(expect.any(Error))) - .and('should not change the store', ({ store }) => expect(store.getSnapshot().size).toBe(0)); + .and('should not change the graph', ({ graph }) => expect(graph.getSnapshot().size).toBe(0)); bdd - .given('a store and a JSON value with the same object appearing twice but not recursive', () => { + .given('a graph and a JSON value with the same object appearing twice but not recursive', () => { const shared = { value: 1 }; - return { ...createStore(), value: { array: [shared, shared], one: shared, two: shared } }; + return { ...createGraph(), value: { array: [shared, shared], one: shared, two: shared } }; }) - .when('a node with the JSON value is upserted', ({ controller, store, value }) => { + .when('a node with the JSON value is upserted', ({ controller, graph, value }) => { controller.edit(transaction => transaction.upsert('_:m-1', () => ({ '@id': '_:m-1', @@ -565,7 +565,7 @@ scenario( })) ); - return getPath(store.getSnapshot().get('_:m-1'), 'data', 0, '@value'); + return getPath(graph.getSnapshot().get('_:m-1'), 'data', 0, '@value'); }) .then('should keep every occurrence', (_, value) => expect(value).toEqual({ array: [{ value: 1 }, { value: 1 }], one: { value: 1 }, two: { value: 1 } }) @@ -577,13 +577,13 @@ scenario( }); bdd - .given('a store and a JSON value with dangerous properties', () => ({ - ...createStore(), + .given('a graph and a JSON value with dangerous properties', () => ({ + ...createGraph(), value: JSON.parse( '{"__proto__":{"polluted":true},"constructor":{"polluted":true},"nested":{"prototype":{"polluted":true},"value":1}}' ) })) - .when('a node with the JSON value is upserted', ({ controller, store, value }) => { + .when('a node with the JSON value is upserted', ({ controller, graph, value }) => { controller.edit(transaction => transaction.upsert('_:m-1', () => ({ '@id': '_:m-1', @@ -592,7 +592,7 @@ scenario( })) ); - return getPath(store.getSnapshot().get('_:m-1'), 'data', 0, '@value'); + return getPath(graph.getSnapshot().get('_:m-1'), 'data', 0, '@value'); }) .then('should skip dangerous properties', (_, value) => expect(value).toEqual({ nested: { value: 1 } })) .and('should be plain objects', (_, value) => { @@ -603,8 +603,8 @@ scenario( .and('should not pollute Object.prototype', () => expect({}).not.toHaveProperty('polluted')); bdd - .given('a store', () => createStore()) - .when('a node with dangerous properties is upserted', ({ controller, store }) => { + .given('a graph', () => createGraph()) + .when('a node with dangerous properties is upserted', ({ controller, graph }) => { controller.edit(transaction => transaction.upsert('_:m-1', () => Object.assign( @@ -618,7 +618,7 @@ scenario( ) ); - return store.getSnapshot().get('_:m-1'); + return graph.getSnapshot().get('_:m-1'); }) .then('should skip dangerous properties', (_, node) => { expect(Object.hasOwn(node ?? {}, '__proto__')).toBe(false); @@ -636,10 +636,10 @@ scenario( 'reference integrity', bdd => { bdd - .given('a store with cross-referencing nodes', () => { - const { controller, store } = createStoreWithNodes(); + .given('a graph with cross-referencing nodes', () => { + const { controller, graph } = createGraphWithNodes(); - return { controller, snapshot: store.getSnapshot(), store }; + return { controller, snapshot: graph.getSnapshot(), graph }; }) .when('a node referencing a non-existing blank node is upserted', ({ controller }) => { try { @@ -657,10 +657,10 @@ scenario( return undefined; }) .then('should throw', (_, error) => expect(error).toEqual(expect.any(Error))) - .and('should not change the snapshot', ({ snapshot, store }) => expect(store.getSnapshot()).toBe(snapshot)); + .and('should not change the snapshot', ({ snapshot, graph }) => expect(graph.getSnapshot()).toBe(snapshot)); bdd - .given('an empty store', () => createStore()) + .given('an empty graph', () => createGraph()) .when('nodes referencing each other are upserted over multiple calls in the same transaction', ({ controller }) => controller.edit(transaction => { transaction.upsert('_:m-1', () => ({ @@ -676,12 +676,12 @@ scenario( })); }) ) - .then('should add both nodes', ({ store }) => - expect(Array.from(store.getSnapshot().keys()).sort()).toEqual(['_:m-1', '_:p-1']) + .then('should add both nodes', ({ graph }) => + expect(Array.from(graph.getSnapshot().keys()).sort()).toEqual(['_:m-1', '_:p-1']) ); bdd - .given('an empty store', () => createStore()) + .given('an empty graph', () => createGraph()) .when('a node referencing a non-existing IRI node is upserted', ({ controller }) => { try { controller.edit(transaction => @@ -698,10 +698,10 @@ scenario( return undefined; }) .then('should throw', (_, error) => expect(error).toEqual(expect.any(Error))) - .and('should not add the node', ({ store }) => expect(store.getSnapshot().has('_:m-1')).toBe(false)); + .and('should not add the node', ({ graph }) => expect(graph.getSnapshot().has('_:m-1')).toBe(false)); bdd - .given('an empty store', () => createStore()) + .given('an empty graph', () => createGraph()) .when('a referenced IRI node is upserted in the same transaction', ({ controller }) => controller.edit(transaction => { transaction.upsert('_:m-1', () => ({ @@ -716,12 +716,12 @@ scenario( })); }) ) - .then('should add both nodes', ({ store }) => - expect(Array.from(store.getSnapshot().keys()).sort()).toEqual(['_:m-1', 'https://example.com/p-1']) + .then('should add both nodes', ({ graph }) => + expect(Array.from(graph.getSnapshot().keys()).sort()).toEqual(['_:m-1', 'https://example.com/p-1']) ); bdd - .given('an empty store', () => createStore()) + .given('an empty graph', () => createGraph()) .when('a referenced blank node is upserted then deleted in the same transaction', ({ controller }) => controller.edit(transaction => { transaction.upsert('_:p-1', () => ({ '@id': '_:p-1', '@type': new Set(['Person']) })); @@ -733,19 +733,19 @@ scenario( transaction.delete('_:p-1'); }) ) - .then('should remove the reference', ({ store }) => - expect(store.getSnapshot().get('_:m-1')).not.toHaveProperty('mentions') + .then('should remove the reference', ({ graph }) => + expect(graph.getSnapshot().get('_:m-1')).not.toHaveProperty('mentions') ); }, NodeTest ); function getPartIdsOf( - storeOrSnapshot: Store | ReadonlyMap, + storeOrSnapshot: Graph | ReadonlyMap, id: string, property: 'hasPart' | 'isPartOf' ): string[] { - const node = (storeOrSnapshot instanceof Store ? storeOrSnapshot.getSnapshot() : storeOrSnapshot).get(id); + const node = (storeOrSnapshot instanceof Graph ? storeOrSnapshot.getSnapshot() : storeOrSnapshot).get(id); return Array.from((property === 'hasPart' ? node?.hasPart : node?.isPartOf) ?? [], ({ '@id': id }) => id).sort(); } @@ -754,14 +754,14 @@ scenario( 'part membership', bdd => { bdd - .given('a store with a conversation', () => { - const { controller, store } = createStore(); + .given('a graph with a conversation', () => { + const { controller, graph } = createGraph(); controller.edit(transaction => transaction.upsert('_:conversation', () => ({ '@id': '_:conversation', '@type': new Set(['Conversation']) })) ); - return { controller, store }; + return { controller, graph }; }) .when('a child node with isPartOf is upserted', ({ controller }) => controller.edit(transaction => @@ -772,25 +772,25 @@ scenario( })) ) ) - .then('should add the child to hasPart of the parent', ({ store }) => - expect(getPartIdsOf(store, '_:conversation', 'hasPart')).toEqual(['_:m-1']) + .then('should add the child to hasPart of the parent', ({ graph }) => + expect(getPartIdsOf(graph, '_:conversation', 'hasPart')).toEqual(['_:m-1']) ) - .and('should keep hasPart as an immutable Set', ({ store }) => { - const hasPart = store.getSnapshot().get('_:conversation')?.hasPart; + .and('should keep hasPart as an immutable Set', ({ graph }) => { + const hasPart = graph.getSnapshot().get('_:conversation')?.hasPart; expect(hasPart).toBeInstanceOf(Set); expect(() => Set.prototype.clear.call(hasPart)).toThrow(TypeError); }); bdd - .given('a store with a message', () => { - const { controller, store } = createStore(); + .given('a graph with a message', () => { + const { controller, graph } = createGraph(); controller.edit(transaction => transaction.upsert('_:m-1', () => ({ '@id': '_:m-1', '@type': new Set(['Message']) })) ); - return { controller, store }; + return { controller, graph }; }) .when('a parent node with hasPart is upserted', ({ controller }) => controller.edit(transaction => @@ -801,12 +801,12 @@ scenario( })) ) ) - .then('should add the parent to isPartOf of the child', ({ store }) => - expect(getPartIdsOf(store, '_:m-1', 'isPartOf')).toEqual(['_:conversation']) + .then('should add the parent to isPartOf of the child', ({ graph }) => + expect(getPartIdsOf(graph, '_:m-1', 'isPartOf')).toEqual(['_:conversation']) ); bdd - .given('an empty store', () => createStore()) + .given('an empty graph', () => createGraph()) .when( 'a child joins a new parent via isPartOf, then the parent is upserted with hasPart without the child', ({ controller }) => @@ -833,10 +833,10 @@ scenario( expect.objectContaining({ message: expect.stringContaining('must have part "_:m-1" in hasPart') }) ) ) - .and('should not commit', ({ store }) => expect(store.getSnapshot().size).toBe(0)); + .and('should not commit', ({ graph }) => expect(graph.getSnapshot().size).toBe(0)); bdd - .given('an empty store', () => createStore()) + .given('an empty graph', () => createGraph()) .when( 'a parent has a new child via hasPart, then the child is upserted with isPartOf without the parent', ({ controller }) => @@ -863,10 +863,10 @@ scenario( expect.objectContaining({ message: expect.stringContaining('must be part of "_:conversation" in isPartOf') }) ) ) - .and('should not commit', ({ store }) => expect(store.getSnapshot().size).toBe(0)); + .and('should not commit', ({ graph }) => expect(graph.getSnapshot().size).toBe(0)); bdd - .given('an empty store', () => createStore()) + .given('an empty graph', () => createGraph()) .when( 'a child joins a new parent via isPartOf, then the parent is upserted with hasPart including the child', ({ controller }) => @@ -890,23 +890,23 @@ scenario( })); }) ) - .then('should have both children in hasPart of the parent', ({ store }) => - expect(getPartIdsOf(store, '_:conversation', 'hasPart')).toEqual(['_:m-1', '_:m-2']) + .then('should have both children in hasPart of the parent', ({ graph }) => + expect(getPartIdsOf(graph, '_:conversation', 'hasPart')).toEqual(['_:m-1', '_:m-2']) ) - .and('should mirror hasPart to isPartOf of children', ({ store }) => { - expect(getPartIdsOf(store, '_:m-1', 'isPartOf')).toEqual(['_:conversation']); - expect(getPartIdsOf(store, '_:m-2', 'isPartOf')).toEqual(['_:conversation']); + .and('should mirror hasPart to isPartOf of children', ({ graph }) => { + expect(getPartIdsOf(graph, '_:m-1', 'isPartOf')).toEqual(['_:conversation']); + expect(getPartIdsOf(graph, '_:m-2', 'isPartOf')).toEqual(['_:conversation']); }); bdd - .given('a store with a parent', () => { - const { controller, store } = createStore(); + .given('a graph with a parent', () => { + const { controller, graph } = createGraph(); controller.edit(transaction => transaction.upsert('_:conversation', () => ({ '@id': '_:conversation', '@type': new Set(['Conversation']) })) ); - return { controller, store }; + return { controller, graph }; }) .when( 'a child joins the parent via isPartOf, then the parent is upserted with hasPart without the child', @@ -934,10 +934,10 @@ scenario( 'Node "_:conversation" must have part "_:m-1" in hasPart, as "_:m-1" is part of it in the same transaction' ) ) - .and('should not commit the child', ({ store }) => expect(store.getSnapshot().has('_:m-1')).toBe(false)); + .and('should not commit the child', ({ graph }) => expect(graph.getSnapshot().has('_:m-1')).toBe(false)); bdd - .given('an empty store', () => createStore()) + .given('an empty graph', () => createGraph()) .when('nested parts are upserted', ({ controller }) => controller.edit(transaction => { transaction.upsert('_:conversation', () => ({ @@ -960,15 +960,15 @@ scenario( })); }) ) - .then('should allow a node to be both parent and child', ({ store }) => { - expect(getPartIdsOf(store, '_:conversation', 'hasPart')).toEqual(['_:thread']); - expect(getPartIdsOf(store, '_:thread', 'isPartOf')).toEqual(['_:conversation']); - expect(getPartIdsOf(store, '_:thread', 'hasPart')).toEqual(['_:m-1']); - expect(getPartIdsOf(store, '_:m-1', 'isPartOf')).toEqual(['_:thread']); + .then('should allow a node to be both parent and child', ({ graph }) => { + expect(getPartIdsOf(graph, '_:conversation', 'hasPart')).toEqual(['_:thread']); + expect(getPartIdsOf(graph, '_:thread', 'isPartOf')).toEqual(['_:conversation']); + expect(getPartIdsOf(graph, '_:thread', 'hasPart')).toEqual(['_:m-1']); + expect(getPartIdsOf(graph, '_:m-1', 'isPartOf')).toEqual(['_:thread']); }); bdd - .given('a store with cross-referencing nodes', () => createStoreWithNodes()) + .given('a graph with cross-referencing nodes', () => createGraphWithNodes()) .when('a child removes isPartOf', ({ controller }) => controller.edit(transaction => transaction.upsert('_:m-1', node => { @@ -978,12 +978,12 @@ scenario( }) ) ) - .then('should remove the child from hasPart of the parent', ({ store }) => - expect(getPartIdsOf(store, '_:conversation', 'hasPart')).toEqual(['_:m-2']) + .then('should remove the child from hasPart of the parent', ({ graph }) => + expect(getPartIdsOf(graph, '_:conversation', 'hasPart')).toEqual(['_:m-2']) ); bdd - .given('a store with cross-referencing nodes', () => createStoreWithNodes()) + .given('a graph with cross-referencing nodes', () => createGraphWithNodes()) .when('a parent removes all hasPart', ({ controller }) => controller.edit(transaction => transaction.upsert('_:conversation', node => { @@ -993,13 +993,13 @@ scenario( }) ) ) - .then('should remove isPartOf from all children', ({ store }) => { - expect(store.getSnapshot().get('_:m-1')).not.toHaveProperty('isPartOf'); - expect(store.getSnapshot().get('_:m-2')).not.toHaveProperty('isPartOf'); + .then('should remove isPartOf from all children', ({ graph }) => { + expect(graph.getSnapshot().get('_:m-1')).not.toHaveProperty('isPartOf'); + expect(graph.getSnapshot().get('_:m-2')).not.toHaveProperty('isPartOf'); }); bdd - .given('a store with cross-referencing nodes', () => createStoreWithNodes()) + .given('a graph with cross-referencing nodes', () => createGraphWithNodes()) .when('parent removes a child while the child re-declares the parent in the same transaction', ({ controller }) => catchError(() => controller.edit(transaction => { @@ -1019,21 +1019,21 @@ scenario( 'Node "_:conversation" must have part "_:m-1" in hasPart, as "_:m-1" is part of it in the same transaction' ) ) - .and('should not commit', ({ store }) => { - expect(getPartIdsOf(store, '_:conversation', 'hasPart')).toEqual(['_:m-1', '_:m-2']); - expect(store.getSnapshot().get('_:m-1')).toHaveProperty('text', ['Hello']); + .and('should not commit', ({ graph }) => { + expect(getPartIdsOf(graph, '_:conversation', 'hasPart')).toEqual(['_:m-1', '_:m-2']); + expect(graph.getSnapshot().get('_:m-1')).toHaveProperty('text', ['Hello']); }); bdd - .given('a store with a conversation and an orphan message', () => { - const { controller, store } = createStore(); + .given('a graph with a conversation and an orphan message', () => { + const { controller, graph } = createGraph(); controller.edit(transaction => { transaction.upsert('_:conversation', () => ({ '@id': '_:conversation', '@type': new Set(['Conversation']) })); transaction.upsert('_:m-1', () => ({ '@id': '_:m-1', '@type': new Set(['Message']) })); }); - return { controller, store }; + return { controller, graph }; }) .when('the child adds isPartOf while the parent removes hasPart in the same transaction', ({ controller }) => catchError(() => @@ -1060,30 +1060,30 @@ scenario( 'Node "_:conversation" must have part "_:m-1" in hasPart, as "_:m-1" is part of it in the same transaction' ) ) - .and('should not commit', ({ store }) => - expect(store.getSnapshot().get('_:conversation')).not.toHaveProperty('hasPart') + .and('should not commit', ({ graph }) => + expect(graph.getSnapshot().get('_:conversation')).not.toHaveProperty('hasPart') ); bdd - .given('a store with cross-referencing nodes', () => createStoreWithNodes()) + .given('a graph with cross-referencing nodes', () => createGraphWithNodes()) .when('the parent is deleted', ({ controller }) => controller.edit(transaction => transaction.delete('_:conversation')) ) - .then('should remove isPartOf from all children', ({ store }) => { - expect(store.getSnapshot().get('_:m-1')).not.toHaveProperty('isPartOf'); - expect(store.getSnapshot().get('_:m-2')).not.toHaveProperty('isPartOf'); + .then('should remove isPartOf from all children', ({ graph }) => { + expect(graph.getSnapshot().get('_:m-1')).not.toHaveProperty('isPartOf'); + expect(graph.getSnapshot().get('_:m-2')).not.toHaveProperty('isPartOf'); }); bdd - .given('a store with cross-referencing nodes', () => createStoreWithNodes()) + .given('a graph with cross-referencing nodes', () => createGraphWithNodes()) .when('a child is deleted', ({ controller }) => controller.edit(transaction => transaction.delete('_:m-1'))) - .then('should remove the child from hasPart of the parent', ({ store }) => - expect(getPartIdsOf(store, '_:conversation', 'hasPart')).toEqual(['_:m-2']) + .then('should remove the child from hasPart of the parent', ({ graph }) => + expect(getPartIdsOf(graph, '_:conversation', 'hasPart')).toEqual(['_:m-2']) ); bdd - .given('a store with nested parts', () => { - const { controller, store } = createStore(); + .given('a graph with nested parts', () => { + const { controller, graph } = createGraph(); controller.edit(transaction => { transaction.upsert('_:conversation', () => ({ @@ -1104,16 +1104,16 @@ scenario( })); }); - return { controller, store }; + return { controller, graph }; }) .when('the middle node is deleted', ({ controller }) => controller.edit(transaction => transaction.delete('_:thread')) ) - .then('should remove hasPart from the parent', ({ store }) => - expect(store.getSnapshot().get('_:conversation')).not.toHaveProperty('hasPart') + .then('should remove hasPart from the parent', ({ graph }) => + expect(graph.getSnapshot().get('_:conversation')).not.toHaveProperty('hasPart') ) - .and('should remove isPartOf from the child', ({ store }) => - expect(store.getSnapshot().get('_:m-1')).not.toHaveProperty('isPartOf') + .and('should remove isPartOf from the child', ({ graph }) => + expect(graph.getSnapshot().get('_:m-1')).not.toHaveProperty('isPartOf') ); }, NodeTest @@ -1123,7 +1123,7 @@ scenario( 'self part', bdd => { bdd - .given('an empty store', () => createStore()) + .given('an empty graph', () => createGraph()) .when('a node with itself in isPartOf is upserted', ({ controller }) => catchError(() => controller.edit(transaction => @@ -1136,10 +1136,10 @@ scenario( ) ) .then('should throw', (_, error) => expect(error).toEqual(expect.any(Error))) - .and('should not add the node', ({ store }) => expect(store.getSnapshot().size).toBe(0)); + .and('should not add the node', ({ graph }) => expect(graph.getSnapshot().size).toBe(0)); bdd - .given('an empty store', () => createStore()) + .given('an empty graph', () => createGraph()) .when('a node with itself in hasPart is upserted', ({ controller }) => catchError(() => controller.edit(transaction => @@ -1152,7 +1152,7 @@ scenario( ) ) .then('should throw', (_, error) => expect(error).toEqual(expect.any(Error))) - .and('should not add the node', ({ store }) => expect(store.getSnapshot().size).toBe(0)); + .and('should not add the node', ({ graph }) => expect(graph.getSnapshot().size).toBe(0)); }, NodeTest ); @@ -1161,23 +1161,23 @@ scenario( 'position', bdd => { bdd - .given('a store with cross-referencing nodes', () => createStoreWithNodes()) + .given('a graph with cross-referencing nodes', () => createGraphWithNodes()) .when('members of the same part have the same position as a regular property', ({ controller }) => controller.edit(transaction => { transaction.upsert('_:m-1', node => ({ ...node!, position: [1] })); transaction.upsert('_:m-2', node => ({ ...node!, position: [1] })); }) ) - .then('should store as-is', ({ store }) => { - expect(store.getSnapshot().get('_:m-1')).toHaveProperty('position', [1]); - expect(store.getSnapshot().get('_:m-2')).toHaveProperty('position', [1]); + .then('should store as-is', ({ graph }) => { + expect(graph.getSnapshot().get('_:m-1')).toHaveProperty('position', [1]); + expect(graph.getSnapshot().get('_:m-2')).toHaveProperty('position', [1]); }); bdd - .given('a store with cross-referencing nodes', () => { - const { controller, store } = createStoreWithNodes(); + .given('a graph with cross-referencing nodes', () => { + const { controller, graph } = createGraphWithNodes(); - return { controller, snapshot: store.getSnapshot(), store }; + return { controller, snapshot: graph.getSnapshot(), graph }; }) .when('a node with position of a single number is upserted', ({ controller }) => catchError(() => @@ -1190,13 +1190,13 @@ scenario( .then('should throw as any other property value must be an array', (_, error) => expect(error).toEqual(expect.any(Error)) ) - .and('should not change the snapshot', ({ snapshot, store }) => expect(store.getSnapshot()).toBe(snapshot)); + .and('should not change the snapshot', ({ snapshot, graph }) => expect(graph.getSnapshot()).toBe(snapshot)); bdd - .given('an empty store', () => { - const { controller, store } = createStore(); + .given('an empty graph', () => { + const { controller, graph } = createGraph(); - return { controller, snapshot: store.getSnapshot(), store }; + return { controller, snapshot: graph.getSnapshot(), graph }; }) .when('a new node with text of a single string is upserted', ({ controller }) => catchError(() => @@ -1213,7 +1213,7 @@ scenario( .then('should throw as any other property value must be an array', (_, error) => expect(error).toEqual(expect.any(Error)) ) - .and('should not change the snapshot', ({ snapshot, store }) => expect(store.getSnapshot()).toBe(snapshot)); + .and('should not change the snapshot', ({ snapshot, graph }) => expect(graph.getSnapshot()).toBe(snapshot)); }, NodeTest ); @@ -1222,14 +1222,14 @@ scenario( 'part membership in a single transaction', bdd => { bdd - .given('a store with a conversation', () => { - const { controller, store } = createStore(); + .given('a graph with a conversation', () => { + const { controller, graph } = createGraph(); controller.edit(transaction => transaction.upsert('_:conversation', () => ({ '@id': '_:conversation', '@type': new Set(['Conversation']) })) ); - return { controller, store }; + return { controller, graph }; }) .when('a child is upserted and the parent is read in the same transaction', ({ controller }) => { let parentInTransaction: unknown; @@ -1253,20 +1253,20 @@ scenario( .then('should not show the pending child inside the transaction', (_, parentInTransaction) => expect(parentInTransaction).not.toHaveProperty('hasPart') ) - .and('should commit', ({ store }) => expect(getPartIdsOf(store, '_:conversation', 'hasPart')).toEqual(['_:m-1'])); + .and('should commit', ({ graph }) => expect(getPartIdsOf(graph, '_:conversation', 'hasPart')).toEqual(['_:m-1'])); bdd - .given('a store with a conversation and a listener', () => { - const { controller, store } = createStore(); + .given('a graph with a conversation and a listener', () => { + const { controller, graph } = createGraph(); const changedNodeIds: string[][] = []; controller.edit(transaction => transaction.upsert('_:conversation', () => ({ '@id': '_:conversation', '@type': new Set(['Conversation']) })) ); - store.subscribe(affectedNodeIds => changedNodeIds.push(Array.from(affectedNodeIds).sort())); + graph.subscribe(affectedNodeIds => changedNodeIds.push(Array.from(affectedNodeIds).sort())); - return { changedNodeIds, controller, store }; + return { changedNodeIds, controller, graph }; }) .when('many children are upserted in a single transaction', ({ controller }) => controller.edit(transaction => { @@ -1279,18 +1279,18 @@ scenario( } }) ) - .then('should add all children to the parent', ({ store }) => - expect(getPartIdsOf(store, '_:conversation', 'hasPart')).toEqual(['_:m-1', '_:m-2', '_:m-3']) + .then('should add all children to the parent', ({ graph }) => + expect(getPartIdsOf(graph, '_:conversation', 'hasPart')).toEqual(['_:m-1', '_:m-2', '_:m-3']) ) .and('should notify once with all affected nodes', ({ changedNodeIds }) => expect(changedNodeIds).toEqual([['_:conversation', '_:m-1', '_:m-2', '_:m-3']]) ); bdd - .given('a store with cross-referencing nodes', () => { - const { controller, store } = createStoreWithNodes(); + .given('a graph with cross-referencing nodes', () => { + const { controller, graph } = createGraphWithNodes(); - return { controller, store }; + return { controller, graph }; }) .when('a child is moved to a new parent in a single transaction', ({ controller }) => controller.edit(transaction => { @@ -1303,17 +1303,17 @@ scenario( transaction.upsert('_:m-2', node => ({ ...node!, isPartOf: new Set([{ '@id': '_:thread' }]) })); }) ) - .then('should remove all children from the old parent', ({ store }) => - expect(store.getSnapshot().get('_:conversation')).not.toHaveProperty('hasPart') + .then('should remove all children from the old parent', ({ graph }) => + expect(graph.getSnapshot().get('_:conversation')).not.toHaveProperty('hasPart') ) - .and('should add all children to the new parent', ({ store }) => - expect(getPartIdsOf(store, '_:thread', 'hasPart')).toEqual(['_:m-1', '_:m-2']) + .and('should add all children to the new parent', ({ graph }) => + expect(getPartIdsOf(graph, '_:thread', 'hasPart')).toEqual(['_:m-1', '_:m-2']) ); }, NodeTest ); -function upsertMessage(transaction: StoreTransaction, id: `_:${string}`, parentId?: `_:${string}` | undefined): void { +function upsertMessage(transaction: GraphTransaction, id: `_:${string}`, parentId?: `_:${string}` | undefined): void { transaction.upsert(id, () => ({ '@id': id, '@type': new Set(['Message']), @@ -1325,32 +1325,32 @@ scenario( 'part membership mirroring', bdd => { bdd - .given('a store with a conversation', () => { - const { controller, store } = createStore(); + .given('a graph with a conversation', () => { + const { controller, graph } = createGraph(); controller.edit(transaction => transaction.upsert('_:conversation', () => ({ '@id': '_:conversation', '@type': new Set(['Conversation']) })) ); - return { controller, snapshot: store.getSnapshot(), store }; + return { controller, snapshot: graph.getSnapshot(), graph }; }) .when('a child node with isPartOf only is upserted', ({ controller }) => controller.edit(transaction => upsertMessage(transaction, '_:m-1', '_:conversation')) ) - .then('should generate hasPart of the parent', ({ store }) => - expect(getPartIdsOf(store, '_:conversation', 'hasPart')).toEqual(['_:m-1']) + .then('should generate hasPart of the parent', ({ graph }) => + expect(getPartIdsOf(graph, '_:conversation', 'hasPart')).toEqual(['_:m-1']) ) .and('should keep the parent in the previous snapshot unchanged', ({ snapshot }) => expect(snapshot.get('_:conversation')).not.toHaveProperty('hasPart') ); bdd - .given('a store with a message', () => { - const { controller, store } = createStore(); + .given('a graph with a message', () => { + const { controller, graph } = createGraph(); controller.edit(transaction => upsertMessage(transaction, '_:m-1')); - return { controller, snapshot: store.getSnapshot(), store }; + return { controller, snapshot: graph.getSnapshot(), graph }; }) .when('a parent node with hasPart only is upserted', ({ controller }) => controller.edit(transaction => @@ -1361,18 +1361,18 @@ scenario( })) ) ) - .then('should mirror hasPart to isPartOf of the child', ({ store }) => - expect(getPartIdsOf(store, '_:m-1', 'isPartOf')).toEqual(['_:conversation']) + .then('should mirror hasPart to isPartOf of the child', ({ graph }) => + expect(getPartIdsOf(graph, '_:m-1', 'isPartOf')).toEqual(['_:conversation']) ) - .and('should keep hasPart of the parent', ({ store }) => - expect(getPartIdsOf(store, '_:conversation', 'hasPart')).toEqual(['_:m-1']) + .and('should keep hasPart of the parent', ({ graph }) => + expect(getPartIdsOf(graph, '_:conversation', 'hasPart')).toEqual(['_:m-1']) ) .and('should keep the child in the previous snapshot unchanged', ({ snapshot }) => expect(snapshot.get('_:m-1')).not.toHaveProperty('isPartOf') ); bdd - .given('an empty store', () => createStore()) + .given('an empty graph', () => createGraph()) .when( 'a parent with hasPart is upserted before its child in the same transaction, and the child agrees in isPartOf', ({ controller }) => @@ -1386,13 +1386,13 @@ scenario( upsertMessage(transaction, '_:m-1', '_:conversation'); }) ) - .then('should have the membership on both sides', ({ store }) => { - expect(getPartIdsOf(store, '_:conversation', 'hasPart')).toEqual(['_:m-1']); - expect(getPartIdsOf(store, '_:m-1', 'isPartOf')).toEqual(['_:conversation']); + .then('should have the membership on both sides', ({ graph }) => { + expect(getPartIdsOf(graph, '_:conversation', 'hasPart')).toEqual(['_:m-1']); + expect(getPartIdsOf(graph, '_:m-1', 'isPartOf')).toEqual(['_:conversation']); }); bdd - .given('an empty store', () => createStore()) + .given('an empty graph', () => createGraph()) .when('a parent with hasPart is upserted before its child, and the child omits isPartOf', ({ controller }) => catchError(() => controller.edit(transaction => { @@ -1411,10 +1411,10 @@ scenario( expect.objectContaining({ message: expect.stringContaining('must be part of "_:conversation" in isPartOf') }) ) ) - .and('should not commit', ({ store }) => expect(store.getSnapshot().size).toBe(0)); + .and('should not commit', ({ graph }) => expect(graph.getSnapshot().size).toBe(0)); bdd - .given('an empty store', () => createStore()) + .given('an empty graph', () => createGraph()) .when('a child with empty isPartOf is upserted before its parent with hasPart', ({ controller }) => catchError(() => controller.edit(transaction => { @@ -1434,17 +1434,17 @@ scenario( 'Node "_:m-1" must be part of "_:conversation" in isPartOf, as "_:conversation" has part of it in the same transaction' ) ) - .and('should not commit', ({ store }) => expect(store.getSnapshot().size).toBe(0)); + .and('should not commit', ({ graph }) => expect(graph.getSnapshot().size).toBe(0)); bdd - .given('a store with a conversation', () => { - const { controller, store } = createStore(); + .given('a graph with a conversation', () => { + const { controller, graph } = createGraph(); controller.edit(transaction => transaction.upsert('_:conversation', () => ({ '@id': '_:conversation', '@type': new Set(['Conversation']) })) ); - return { controller, snapshot: store.getSnapshot(), store }; + return { controller, snapshot: graph.getSnapshot(), graph }; }) .when('a parent has a child which is never upserted', ({ controller }) => catchError(() => @@ -1456,17 +1456,17 @@ scenario( .then('should throw', (_, error) => expect(error).toHaveProperty('message', 'Node "_:conversation" has part "_:m-1" which is not in the graph') ) - .and('should not change the snapshot', ({ snapshot, store }) => expect(store.getSnapshot()).toBe(snapshot)); + .and('should not change the snapshot', ({ snapshot, graph }) => expect(graph.getSnapshot()).toBe(snapshot)); bdd - .given('a store with a conversation', () => { - const { controller, store } = createStore(); + .given('a graph with a conversation', () => { + const { controller, graph } = createGraph(); controller.edit(transaction => transaction.upsert('_:conversation', () => ({ '@id': '_:conversation', '@type': new Set(['Conversation']) })) ); - return { controller, store }; + return { controller, graph }; }) .when('a parent has a child which is never upserted, then deleted', ({ controller }) => controller.edit(transaction => { @@ -1474,24 +1474,24 @@ scenario( transaction.delete('_:m-1'); }) ) - .then('should remove the child from hasPart', ({ store }) => - expect(store.getSnapshot().get('_:conversation')).not.toHaveProperty('hasPart') + .then('should remove the child from hasPart', ({ graph }) => + expect(graph.getSnapshot().get('_:conversation')).not.toHaveProperty('hasPart') ); bdd - .given('a store with cross-referencing nodes', () => { - const { controller, store } = createStoreWithNodes(); + .given('a graph with cross-referencing nodes', () => { + const { controller, graph } = createGraphWithNodes(); - return { controller, snapshot: store.getSnapshot(), store }; + return { controller, snapshot: graph.getSnapshot(), graph }; }) .when('a parent removes a child from hasPart', ({ controller }) => controller.edit(transaction => transaction.upsert('_:conversation', node => ({ ...node!, hasPart: new Set([{ '@id': '_:m-2' }]) })) ) ) - .then('should remove isPartOf from the child', ({ store }) => { - expect(getPartIdsOf(store, '_:conversation', 'hasPart')).toEqual(['_:m-2']); - expect(store.getSnapshot().get('_:m-1')).not.toHaveProperty('isPartOf'); + .then('should remove isPartOf from the child', ({ graph }) => { + expect(getPartIdsOf(graph, '_:conversation', 'hasPart')).toEqual(['_:m-2']); + expect(graph.getSnapshot().get('_:m-1')).not.toHaveProperty('isPartOf'); }) .and('should keep the previous snapshot unchanged', ({ snapshot }) => { expect(getPartIdsOf(snapshot, '_:conversation', 'hasPart')).toEqual(['_:m-1', '_:m-2']); @@ -1499,7 +1499,7 @@ scenario( }); bdd - .given('a store with cross-referencing nodes', () => createStoreWithNodes()) + .given('a graph with cross-referencing nodes', () => createGraphWithNodes()) .when('a child removes isPartOf', ({ controller }) => controller.edit(transaction => transaction.upsert('_:m-1', node => { @@ -1509,12 +1509,12 @@ scenario( }) ) ) - .then('should remove the child from hasPart of the parent', ({ store }) => - expect(getPartIdsOf(store, '_:conversation', 'hasPart')).toEqual(['_:m-2']) + .then('should remove the child from hasPart of the parent', ({ graph }) => + expect(getPartIdsOf(graph, '_:conversation', 'hasPart')).toEqual(['_:m-2']) ); bdd - .given('a store with cross-referencing nodes', () => createStoreWithNodes()) + .given('a graph with cross-referencing nodes', () => createGraphWithNodes()) .when('a child is moved to another parent', ({ controller }) => controller.edit(transaction => { transaction.upsert('_:thread', () => ({ @@ -1525,14 +1525,14 @@ scenario( transaction.upsert('_:m-1', node => ({ ...node!, isPartOf: new Set([{ '@id': '_:thread' }]) })); }) ) - .then('should move the child', ({ store }) => { - expect(getPartIdsOf(store, '_:conversation', 'hasPart')).toEqual(['_:m-2']); - expect(getPartIdsOf(store, '_:thread', 'hasPart')).toEqual(['_:m-1']); - expect(getPartIdsOf(store, '_:m-1', 'isPartOf')).toEqual(['_:thread']); + .then('should move the child', ({ graph }) => { + expect(getPartIdsOf(graph, '_:conversation', 'hasPart')).toEqual(['_:m-2']); + expect(getPartIdsOf(graph, '_:thread', 'hasPart')).toEqual(['_:m-1']); + expect(getPartIdsOf(graph, '_:m-1', 'isPartOf')).toEqual(['_:thread']); }); bdd - .given('a store with cross-referencing nodes', () => createStoreWithNodes()) + .given('a graph with cross-referencing nodes', () => createGraphWithNodes()) .when('a child is part of two parents', ({ controller }) => controller.edit(transaction => { transaction.upsert('_:thread', () => ({ @@ -1546,83 +1546,83 @@ scenario( })); }) ) - .then('should be in hasPart of both parents', ({ store }) => { - expect(getPartIdsOf(store, '_:conversation', 'hasPart')).toEqual(['_:m-1', '_:m-2']); - expect(getPartIdsOf(store, '_:thread', 'hasPart')).toEqual(['_:m-1']); + .then('should be in hasPart of both parents', ({ graph }) => { + expect(getPartIdsOf(graph, '_:conversation', 'hasPart')).toEqual(['_:m-1', '_:m-2']); + expect(getPartIdsOf(graph, '_:thread', 'hasPart')).toEqual(['_:m-1']); }); bdd - .given('a store with cross-referencing nodes', () => { - const { controller, store } = createStoreWithNodes(); + .given('a graph with cross-referencing nodes', () => { + const { controller, graph } = createGraphWithNodes(); - return { controller, snapshot: store.getSnapshot(), store }; + return { controller, snapshot: graph.getSnapshot(), graph }; }) .when('a parent is updated with spread', ({ controller }) => controller.edit(transaction => transaction.upsert('_:conversation', node => ({ ...node!, name: ['Conversation'] })) ) ) - .then('should keep hasPart', ({ store }) => - expect(getPartIdsOf(store, '_:conversation', 'hasPart')).toEqual(['_:m-1', '_:m-2']) + .then('should keep hasPart', ({ graph }) => + expect(getPartIdsOf(graph, '_:conversation', 'hasPart')).toEqual(['_:m-1', '_:m-2']) ) - .and('should not change children', ({ snapshot, store }) => - expect(getChangedNodeIds(snapshot, store.getSnapshot())).toEqual(['_:conversation']) + .and('should not change children', ({ snapshot, graph }) => + expect(getChangedNodeIds(snapshot, graph.getSnapshot())).toEqual(['_:conversation']) ); bdd - .given('a store with cross-referencing nodes', () => { - const { controller, store } = createStoreWithNodes(); + .given('a graph with cross-referencing nodes', () => { + const { controller, graph } = createGraphWithNodes(); - return { controller, snapshot: store.getSnapshot(), store }; + return { controller, snapshot: graph.getSnapshot(), graph }; }) .when('a child is updated without touching part relationships', ({ controller }) => controller.edit(transaction => transaction.upsert('_:m-2', node => ({ ...node!, text: ['Updated'] }))) ) - .then('should update the child', ({ store }) => - expect(store.getSnapshot().get('_:m-2')).toHaveProperty('text', ['Updated']) + .then('should update the child', ({ graph }) => + expect(graph.getSnapshot().get('_:m-2')).toHaveProperty('text', ['Updated']) ) - .and('should keep part membership', ({ store }) => { - expect(getPartIdsOf(store, '_:conversation', 'hasPart')).toEqual(['_:m-1', '_:m-2']); - expect(getPartIdsOf(store, '_:m-2', 'isPartOf')).toEqual(['_:conversation']); + .and('should keep part membership', ({ graph }) => { + expect(getPartIdsOf(graph, '_:conversation', 'hasPart')).toEqual(['_:m-1', '_:m-2']); + expect(getPartIdsOf(graph, '_:m-2', 'isPartOf')).toEqual(['_:conversation']); }) - .and('should not change the parent', ({ snapshot, store }) => - expect(getChangedNodeIds(snapshot, store.getSnapshot())).toEqual(['_:m-2']) + .and('should not change the parent', ({ snapshot, graph }) => + expect(getChangedNodeIds(snapshot, graph.getSnapshot())).toEqual(['_:m-2']) ); bdd - .given('a store with cross-referencing nodes', () => createStoreWithNodes()) + .given('a graph with cross-referencing nodes', () => createGraphWithNodes()) .when('the parent is deleted', ({ controller }) => controller.edit(transaction => transaction.delete('_:conversation')) ) - .then('should remove isPartOf from all children', ({ store }) => { - expect(store.getSnapshot().get('_:m-1')).not.toHaveProperty('isPartOf'); - expect(store.getSnapshot().get('_:m-2')).not.toHaveProperty('isPartOf'); + .then('should remove isPartOf from all children', ({ graph }) => { + expect(graph.getSnapshot().get('_:m-1')).not.toHaveProperty('isPartOf'); + expect(graph.getSnapshot().get('_:m-2')).not.toHaveProperty('isPartOf'); }); bdd - .given('a store with cross-referencing nodes', () => createStoreWithNodes()) + .given('a graph with cross-referencing nodes', () => createGraphWithNodes()) .when('a child is deleted', ({ controller }) => controller.edit(transaction => transaction.delete('_:m-1'))) - .then('should remove the child from hasPart of the parent', ({ store }) => - expect(getPartIdsOf(store, '_:conversation', 'hasPart')).toEqual(['_:m-2']) + .then('should remove the child from hasPart of the parent', ({ graph }) => + expect(getPartIdsOf(graph, '_:conversation', 'hasPart')).toEqual(['_:m-2']) ); bdd - .given('an empty store', () => createStore()) + .given('an empty graph', () => createGraph()) .when('edit is called with a transactor which is not a function', ({ controller }) => - catchError(() => controller.edit('transactor' as unknown as Parameters[0])) + catchError(() => controller.edit('transactor' as unknown as Parameters[0])) ) .then('should throw', (_, error) => expect(error).toEqual(expect.any(TypeError))) - .and('should not change the store', ({ store }) => expect(store.getSnapshot().size).toBe(0)); + .and('should not change the graph', ({ graph }) => expect(graph.getSnapshot().size).toBe(0)); bdd - .given('a store with a message and a listener', () => { - const { controller, store } = createStore(); + .given('a graph with a message and a listener', () => { + const { controller, graph } = createGraph(); const numCalls = { value: 0 }; controller.edit(transaction => upsertMessage(transaction, '_:m-1')); - store.subscribe(() => numCalls.value++); + graph.subscribe(() => numCalls.value++); - return { controller, numCalls, snapshot: store.getSnapshot(), store }; + return { controller, numCalls, snapshot: graph.getSnapshot(), graph }; }) .when.oneOf<{ readonly error: unknown; readonly pending: Promise | undefined }>([ [ @@ -1664,7 +1664,7 @@ scenario( expect(error).toEqual(expect.objectContaining({ message: expect.stringContaining('then-able') })) ) .and('should throw TypeError', (_, { error }) => expect(error).toEqual(expect.any(TypeError))) - .and('should not change the store', ({ snapshot, store }) => expect(store.getSnapshot()).toBe(snapshot)) + .and('should not change the graph', ({ snapshot, graph }) => expect(graph.getSnapshot()).toBe(snapshot)) .and('should not notify', ({ numCalls }) => expect(numCalls.value).toBe(0)) .and('should fail the asynchronous part', async (_, { pending }) => { pending && @@ -1672,9 +1672,9 @@ scenario( }); bdd - .given('an empty store', () => createStore()) + .given('an empty graph', () => createGraph()) .when('the transaction is used after edit() returns', ({ controller }) => { - let leakedTransaction: StoreTransaction | undefined; + let leakedTransaction: GraphTransaction | undefined; controller.edit(transaction => { leakedTransaction = transaction; @@ -1688,17 +1688,17 @@ scenario( expect(call).toThrow('Transaction has ended'); } }) - .and('should not change the store', ({ store }) => - expect(Array.from(store.getSnapshot().keys())).toEqual(['_:m-1']) + .and('should not change the graph', ({ graph }) => + expect(Array.from(graph.getSnapshot().keys())).toEqual(['_:m-1']) ); bdd - .given('a store with a message', () => { - const { controller, store } = createStore(); + .given('a graph with a message', () => { + const { controller, graph } = createGraph(); controller.edit(transaction => upsertMessage(transaction, '_:m-1')); - return { controller, snapshot: store.getSnapshot(), store }; + return { controller, snapshot: graph.getSnapshot(), graph }; }) .when.oneOf([ [ @@ -1728,15 +1728,15 @@ scenario( .then('should throw', (_, error) => expect(error).toEqual(expect.objectContaining({ message: 'edit() cannot be called inside another edit()' })) ) - .and('should not change the store', ({ snapshot, store }) => expect(store.getSnapshot()).toBe(snapshot)); + .and('should not change the graph', ({ snapshot, graph }) => expect(graph.getSnapshot()).toBe(snapshot)); bdd - .given('a store with a message', () => { - const { controller, store } = createStore(); + .given('a graph with a message', () => { + const { controller, graph } = createGraph(); controller.edit(transaction => upsertMessage(transaction, '_:m-1')); - return { controller, store }; + return { controller, graph }; }) .when('edit() is called inside the transactor and the error is caught', ({ controller }) => controller.edit(transaction => { @@ -1744,27 +1744,27 @@ scenario( upsertMessage(transaction, '_:m-2'); }) ) - .then('should commit the outer edit only', ({ store }) => - expect(Array.from(store.getSnapshot().keys()).sort()).toEqual(['_:m-1', '_:m-2']) + .then('should commit the outer edit only', ({ graph }) => + expect(Array.from(graph.getSnapshot().keys()).sort()).toEqual(['_:m-1', '_:m-2']) ); bdd - .given('a store with a listener which edits once', () => { - const { controller, store } = createStore(); + .given('a graph with a listener which edits once', () => { + const { controller, graph } = createGraph(); const calls: string[][] = []; - store.subscribe(affectedNodeIds => { + graph.subscribe(affectedNodeIds => { calls.push(Array.from(affectedNodeIds).sort()); calls.length === 1 && controller.edit(transaction => upsertMessage(transaction, '_:m-2')); }); - return { calls, controller, store }; + return { calls, controller, graph }; }) .when('a message is upserted', ({ controller }) => controller.edit(transaction => upsertMessage(transaction, '_:m-1')) ) - .then('should allow edit() inside the listener', ({ store }) => - expect(Array.from(store.getSnapshot().keys()).sort()).toEqual(['_:m-1', '_:m-2']) + .then('should allow edit() inside the listener', ({ graph }) => + expect(Array.from(graph.getSnapshot().keys()).sort()).toEqual(['_:m-1', '_:m-2']) ) .and('should notify both edits', ({ calls }) => expect(calls).toEqual([['_:m-1'], ['_:m-2']])); }, @@ -1775,8 +1775,8 @@ scenario( 'dirty read', bdd => { bdd - .given('a store with a conversation and a message', () => { - const { controller, store } = createStore(); + .given('a graph with a conversation and a message', () => { + const { controller, graph } = createGraph(); controller.edit(transaction => { transaction.upsert('_:conversation', () => ({ @@ -1787,9 +1787,9 @@ scenario( upsertMessage(transaction, '_:m-1', '_:conversation'); }); - return { controller, store }; + return { controller, graph }; }) - .when('the message is upserted for the first time in a transaction', ({ controller, store }) => { + .when('the message is upserted for the first time in a transaction', ({ controller, graph }) => { let received: VettedNode | undefined; controller.edit(transaction => @@ -1800,7 +1800,7 @@ scenario( }) ); - return { prevNode: store.getSnapshot().get('_:m-1'), received }; + return { prevNode: graph.getSnapshot().get('_:m-1'), received }; }) .then('should receive a frozen node', (_, { received }) => expect(Object.isFrozen(received)).toBe(true)) .and('should receive the node with hasPart and isPartOf', (_, { received }) => @@ -1808,8 +1808,8 @@ scenario( ); bdd - .given('a store with a conversation and a message', () => { - const { controller, store } = createStore(); + .given('a graph with a conversation and a message', () => { + const { controller, graph } = createGraph(); controller.edit(transaction => { transaction.upsert('_:conversation', () => ({ @@ -1821,7 +1821,7 @@ scenario( upsertMessage(transaction, '_:m-2'); }); - return { controller, store }; + return { controller, graph }; }) .when( 'the message is upserted twice in a transaction, and the second updater modifies the node in place', @@ -1874,25 +1874,25 @@ scenario( })(); }).toThrow(TypeError); }) - .and('should commit changes made in place', ({ store }) => - expect(store.getSnapshot().get('_:m-1')).toHaveProperty( + .and('should commit changes made in place', ({ graph }) => + expect(graph.getSnapshot().get('_:m-1')).toHaveProperty( 'isPartOf', new Set([{ '@id': '_:conversation' }, { '@id': '_:m-2' }]) ) ) - .and('should mirror to hasPart of the new parent', ({ store }) => - expect(store.getSnapshot().get('_:m-2')).toHaveProperty('hasPart', new Set([{ '@id': '_:m-1' }])) + .and('should mirror to hasPart of the new parent', ({ graph }) => + expect(graph.getSnapshot().get('_:m-2')).toHaveProperty('hasPart', new Set([{ '@id': '_:m-1' }])) ); bdd - .given('a store with a conversation', () => { - const { controller, store } = createStore(); + .given('a graph with a conversation', () => { + const { controller, graph } = createGraph(); controller.edit(transaction => transaction.upsert('_:conversation', () => ({ '@id': '_:conversation', '@type': new Set(['Conversation']) })) ); - return { controller, store }; + return { controller, graph }; }) .when('a child is added, then the conversation is upserted in the same transaction', ({ controller }) => { let received: VettedNode | undefined; @@ -1911,20 +1911,20 @@ scenario( .then('should not receive the pending child in hasPart', (_, received) => expect(received).not.toHaveProperty('hasPart') ) - .and('should keep the child', ({ store }) => - expect(store.getSnapshot().get('_:conversation')).toHaveProperty('hasPart', new Set([{ '@id': '_:m-1' }])) + .and('should keep the child', ({ graph }) => + expect(graph.getSnapshot().get('_:conversation')).toHaveProperty('hasPart', new Set([{ '@id': '_:m-1' }])) ); bdd - .given('a store with a conversation and a message without parent', () => { - const { controller, store } = createStore(); + .given('a graph with a conversation and a message without parent', () => { + const { controller, graph } = createGraph(); controller.edit(transaction => { transaction.upsert('_:conversation', () => ({ '@id': '_:conversation', '@type': new Set(['Conversation']) })); upsertMessage(transaction, '_:m-1'); }); - return { controller, store }; + return { controller, graph }; }) .when('the message is adopted via hasPart, then upserted in the same transaction', ({ controller }) => { let isFrozenInsideUpdater: boolean | undefined; @@ -1951,8 +1951,8 @@ scenario( .and('should freeze the node after the updater returns', (_, { received }) => expect(Object.isFrozen(received)).toBe(true) ) - .and('should commit the message with the parent', ({ store }) => - expect(store.getSnapshot().get('_:m-1')).toEqual({ + .and('should commit the message with the parent', ({ graph }) => + expect(graph.getSnapshot().get('_:m-1')).toEqual({ '@id': '_:m-1', '@type': new Set(['Message']), isPartOf: new Set([{ '@id': '_:conversation' }]), @@ -1961,15 +1961,15 @@ scenario( ); bdd - .given('a store with a conversation and a message without parent', () => { - const { controller, store } = createStore(); + .given('a graph with a conversation and a message without parent', () => { + const { controller, graph } = createGraph(); controller.edit(transaction => { transaction.upsert('_:conversation', () => ({ '@id': '_:conversation', '@type': new Set(['Conversation']) })); upsertMessage(transaction, '_:m-1'); }); - return { controller, store }; + return { controller, graph }; }) .when( 'the message is adopted via hasPart, confirmed, then upserted again in the same transaction', @@ -1992,17 +1992,17 @@ scenario( .then('should receive the confirmed parent in isPartOf', (_, received) => expect(received).toHaveProperty('isPartOf', new Set([{ '@id': '_:conversation' }])) ) - .and('should commit the message with the parent', ({ store }) => - expect(store.getSnapshot().get('_:m-1')).toHaveProperty('isPartOf', new Set([{ '@id': '_:conversation' }])) + .and('should commit the message with the parent', ({ graph }) => + expect(graph.getSnapshot().get('_:m-1')).toHaveProperty('isPartOf', new Set([{ '@id': '_:conversation' }])) ); bdd - .given('a store with a message', () => { - const { controller, store } = createStore(); + .given('a graph with a message', () => { + const { controller, graph } = createGraph(); controller.edit(transaction => upsertMessage(transaction, '_:m-1')); - return { controller, store }; + return { controller, graph }; }) .when('the message is upserted twice in a transaction, and the second updater throws', ({ controller }) => { let received: VettedNode | undefined; @@ -2024,17 +2024,17 @@ scenario( .then('should freeze the node received by the updater', (_, received) => expect(Object.isFrozen(received)).toBe(true) ) - .and('should commit changes made by the first updater', ({ store }) => - expect(store.getSnapshot().get('_:m-1')).toHaveProperty('text', ['Hello']) + .and('should commit changes made by the first updater', ({ graph }) => + expect(graph.getSnapshot().get('_:m-1')).toHaveProperty('text', ['Hello']) ); bdd - .given('a store with a message', () => { - const { controller, store } = createStore(); + .given('a graph with a message', () => { + const { controller, graph } = createGraph(); controller.edit(transaction => upsertMessage(transaction, '_:m-1')); - return { controller, store }; + return { controller, graph }; }) .when('the node received by the updater is cloned by JSON.parse(JSON.stringify())', ({ controller }) => controller.edit(transaction => { @@ -2046,8 +2046,8 @@ scenario( })); }) ) - .then('should commit the node', ({ store }) => - expect(store.getSnapshot().get('_:m-1')).toEqual({ + .then('should commit the node', ({ graph }) => + expect(graph.getSnapshot().get('_:m-1')).toEqual({ '@id': '_:m-1', '@type': new Set(['Message']), text: ['World'] @@ -2061,7 +2061,7 @@ scenario( 'toJSON() on nodes', bdd => { bdd - .given('a store', () => createStore()) + .given('a graph', () => createGraph()) .when.oneOf([ [ 'the node has toJSON()', @@ -2138,10 +2138,10 @@ scenario( expect.objectContaining({ message: expect.stringMatching(/toJSON\(\) is not supported/u) }) ) ) - .and('should not change the store', ({ store }) => expect(store.getSnapshot().size).toBe(0)); + .and('should not change the graph', ({ graph }) => expect(graph.getSnapshot().size).toBe(0)); bdd - .given('a store', () => createStore()) + .given('a graph', () => createGraph()) .when('a JSON literal has toJSON()', ({ controller }) => controller.edit(transaction => transaction.upsert('_:m-1', () => ({ @@ -2152,8 +2152,8 @@ scenario( })) ) ) - .then('should call toJSON()', ({ store }) => - expect(store.getSnapshot().get('_:m-1')).toHaveProperty('data', [ + .then('should call toJSON()', ({ graph }) => + expect(graph.getSnapshot().get('_:m-1')).toHaveProperty('data', [ { '@type': '@json', '@value': { date: '2026-01-01T00:00:00.000Z' } } ]) ); @@ -2174,7 +2174,7 @@ scenario( } bdd - .given('a store', () => createStore()) + .given('a graph', () => createGraph()) .when.oneOf([ [ 'the node is a class instance', @@ -2224,10 +2224,10 @@ scenario( .then('should throw', (_, error) => expect(error).toEqual(expect.objectContaining({ message: expect.stringMatching(/must be plain objects/u) })) ) - .and('should not change the store', ({ store }) => expect(store.getSnapshot().size).toBe(0)); + .and('should not change the graph', ({ graph }) => expect(graph.getSnapshot().size).toBe(0)); bdd - .given('a store', () => createStore()) + .given('a graph', () => createGraph()) .when('a property value array is a subclass of Array', ({ controller }) => catchError(() => controller.edit(transaction => @@ -2242,10 +2242,10 @@ scenario( .then('should throw', (_, error) => expect(error).toEqual(expect.objectContaining({ message: expect.stringMatching(/must be plain arrays/u) })) ) - .and('should not change the store', ({ store }) => expect(store.getSnapshot().size).toBe(0)); + .and('should not change the graph', ({ graph }) => expect(graph.getSnapshot().size).toBe(0)); bdd - .given('a store', () => createStore()) + .given('a graph', () => createGraph()) .when('upserting a node and a node reference with null prototype', ({ controller }) => controller.edit(transaction => { upsertMessage(transaction, '_:m-2'); @@ -2258,8 +2258,8 @@ scenario( ); }) ) - .then('should store the node as a plain object', ({ store }) => { - const node = store.getSnapshot().get('_:m-1'); + .then('should store the node as a plain object', ({ graph }) => { + const node = graph.getSnapshot().get('_:m-1'); expect(Object.getPrototypeOf(node)).toBe(Object.prototype); expect(node).toHaveProperty('citation', [{ '@id': '_:m-2' }]); @@ -2272,8 +2272,8 @@ scenario( 'transaction methods inside an updater', bdd => { bdd - .given('a store with 2 messages and a listener', () => { - const { controller, store } = createStore(); + .given('a graph with 2 messages and a listener', () => { + const { controller, graph } = createGraph(); const numCalls = { value: 0 }; controller.edit(transaction => { @@ -2281,9 +2281,9 @@ scenario( upsertMessage(transaction, '_:m-2'); }); - store.subscribe(() => numCalls.value++); + graph.subscribe(() => numCalls.value++); - return { controller, numCalls, snapshot: store.getSnapshot(), store }; + return { controller, numCalls, snapshot: graph.getSnapshot(), graph }; }) .when.oneOf([ [ @@ -2336,19 +2336,19 @@ scenario( }) ) ) - .and('should not change the store', ({ snapshot, store }) => expect(store.getSnapshot()).toBe(snapshot)) + .and('should not change the graph', ({ snapshot, graph }) => expect(graph.getSnapshot()).toBe(snapshot)) .and('should not notify', ({ numCalls }) => expect(numCalls.value).toBe(0)); bdd - .given('a store with 2 messages', () => { - const { controller, store } = createStore(); + .given('a graph with 2 messages', () => { + const { controller, graph } = createGraph(); controller.edit(transaction => { upsertMessage(transaction, '_:m-1'); upsertMessage(transaction, '_:m-2'); }); - return { controller, m2: store.getSnapshot().get('_:m-2'), store }; + return { controller, m2: graph.getSnapshot().get('_:m-2'), graph }; }) .when.oneOf<{ readonly errors: readonly unknown[] }>([ [ @@ -2394,30 +2394,30 @@ scenario( ); } }) - .and('should commit the outer upsert', ({ store }) => - expect(store.getSnapshot().get('_:m-1')).toEqual({ + .and('should commit the outer upsert', ({ graph }) => + expect(graph.getSnapshot().get('_:m-1')).toEqual({ '@id': '_:m-1', '@type': new Set(['Message']), text: ['Hello'] }) ) - .and('should not add the node of the nested upsert', ({ store }) => - expect(store.getSnapshot().has('_:m-3')).toBe(false) + .and('should not add the node of the nested upsert', ({ graph }) => + expect(graph.getSnapshot().has('_:m-3')).toBe(false) ) - .and('should not change the node of the nested call', ({ m2, store }) => - expect(store.getSnapshot().get('_:m-2')).toBe(m2) + .and('should not change the node of the nested call', ({ m2, graph }) => + expect(graph.getSnapshot().get('_:m-2')).toBe(m2) ); bdd - .given('a store with 2 messages', () => { - const { controller, store } = createStore(); + .given('a graph with 2 messages', () => { + const { controller, graph } = createGraph(); controller.edit(transaction => { upsertMessage(transaction, '_:m-1'); upsertMessage(transaction, '_:m-2'); }); - return { controller, store }; + return { controller, graph }; }) .when.oneOf([ [ @@ -2444,11 +2444,11 @@ scenario( }) ] ]) - .then('should upsert', ({ store }) => { - expect(store.getSnapshot().get('_:m-1')).toHaveProperty('text', ['Hello']); - expect(store.getSnapshot().has('_:m-3')).toBe(true); + .then('should upsert', ({ graph }) => { + expect(graph.getSnapshot().get('_:m-1')).toHaveProperty('text', ['Hello']); + expect(graph.getSnapshot().has('_:m-3')).toBe(true); }) - .and('should delete', ({ store }) => expect(store.getSnapshot().has('_:m-2')).toBe(false)); + .and('should delete', ({ graph }) => expect(graph.getSnapshot().has('_:m-2')).toBe(false)); }, NodeTest ); @@ -2457,8 +2457,8 @@ scenario( 'generated hasPart', bdd => { bdd - .given('a store with cross-referencing nodes', () => createStoreWithNodes()) - .when('the parent is read', ({ store }) => store.getSnapshot().get('_:conversation')) + .given('a graph with cross-referencing nodes', () => createGraphWithNodes()) + .when('the parent is read', ({ graph }) => graph.getSnapshot().get('_:conversation')) .then('should have an enumerable hasPart', (_, parent) => expect(Object.keys(parent!)).toEqual(expect.arrayContaining(['hasPart'])) ) @@ -2487,13 +2487,13 @@ scenario( 'subscribe', bdd => { bdd - .given('a store with cross-referencing nodes and a listener', () => { - const { controller, store } = createStoreWithNodes(); + .given('a graph with cross-referencing nodes and a listener', () => { + const { controller, graph } = createGraphWithNodes(); const calls: ReadonlySet[] = []; - store.subscribe(affectedNodeIds => calls.push(affectedNodeIds)); + graph.subscribe(affectedNodeIds => calls.push(affectedNodeIds)); - return { calls, controller, store }; + return { calls, controller, graph }; }) .when('a node is upserted', ({ controller }) => controller.edit(transaction => transaction.upsert('_:m-1', node => ({ ...node!, text: ['Aloha'] }))) @@ -2503,13 +2503,13 @@ scenario( ); bdd - .given('a store with cross-referencing nodes and a listener', () => { - const { controller, store } = createStoreWithNodes(); + .given('a graph with cross-referencing nodes and a listener', () => { + const { controller, graph } = createGraphWithNodes(); const calls: ReadonlySet[] = []; - store.subscribe(affectedNodeIds => calls.push(affectedNodeIds)); + graph.subscribe(affectedNodeIds => calls.push(affectedNodeIds)); - return { calls, controller, store }; + return { calls, controller, graph }; }) .when('a referenced node is deleted', ({ controller }) => controller.edit(transaction => transaction.delete('_:m-1')) @@ -2519,17 +2519,17 @@ scenario( ); bdd - .given('a store with a conversation and a listener', () => { - const { controller, store } = createStore(); + .given('a graph with a conversation and a listener', () => { + const { controller, graph } = createGraph(); const calls: ReadonlySet[] = []; controller.edit(transaction => transaction.upsert('_:conversation', () => ({ '@id': '_:conversation', '@type': new Set(['Conversation']) })) ); - store.subscribe(affectedNodeIds => calls.push(affectedNodeIds)); + graph.subscribe(affectedNodeIds => calls.push(affectedNodeIds)); - return { calls, controller, store }; + return { calls, controller, graph }; }) .when('a child is upserted', ({ controller }) => controller.edit(transaction => @@ -2545,11 +2545,11 @@ scenario( ); bdd - .given('a store with cross-referencing nodes and a listener', () => { - const { controller, store } = createStoreWithNodes(); + .given('a graph with cross-referencing nodes and a listener', () => { + const { controller, graph } = createGraphWithNodes(); const calls: ReadonlySet[] = []; - store.subscribe(affectedNodeIds => calls.push(affectedNodeIds)); + graph.subscribe(affectedNodeIds => calls.push(affectedNodeIds)); controller.edit(transaction => transaction.upsert('_:m-1', node => ({ ...node!, text: ['Aloha'] }))); @@ -2567,14 +2567,14 @@ scenario( .and('should not be modified', affectedNodeIds => expect(Array.from(affectedNodeIds ?? [])).toEqual(['_:m-1'])); bdd - .given('a store with cross-referencing nodes and two listeners', () => { - const { controller, store } = createStoreWithNodes(); + .given('a graph with cross-referencing nodes and two listeners', () => { + const { controller, graph } = createGraphWithNodes(); const calls: ReadonlySet[] = []; - store.subscribe(affectedNodeIds => calls.push(affectedNodeIds)); - store.subscribe(affectedNodeIds => calls.push(affectedNodeIds)); + graph.subscribe(affectedNodeIds => calls.push(affectedNodeIds)); + graph.subscribe(affectedNodeIds => calls.push(affectedNodeIds)); - return { calls, controller, store }; + return { calls, controller, graph }; }) .when('a node is upserted', ({ controller }) => controller.edit(transaction => transaction.upsert('_:m-1', node => ({ ...node!, text: ['Aloha'] }))) @@ -2582,13 +2582,13 @@ scenario( .then('should notify both listeners', ({ calls }) => expect(calls).toHaveLength(2)); bdd - .given('a store with cross-referencing nodes and an unsubscribed listener', () => { - const { controller, store } = createStoreWithNodes(); + .given('a graph with cross-referencing nodes and an unsubscribed listener', () => { + const { controller, graph } = createGraphWithNodes(); const calls: ReadonlySet[] = []; - store.subscribe(affectedNodeIds => calls.push(affectedNodeIds))(); + graph.subscribe(affectedNodeIds => calls.push(affectedNodeIds))(); - return { calls, controller, store }; + return { calls, controller, graph }; }) .when('a node is upserted', ({ controller }) => controller.edit(transaction => transaction.upsert('_:m-1', node => ({ ...node!, text: ['Aloha'] }))) @@ -2596,9 +2596,9 @@ scenario( .then('should not notify', ({ calls }) => expect(calls).toHaveLength(0)); bdd - .given('an empty store', () => createStore()) - .when('subscribe is called with a non-function', ({ store }) => - catchError(() => store.subscribe('not a function' as unknown as () => void)) + .given('an empty graph', () => createGraph()) + .when('subscribe is called with a non-function', ({ graph }) => + catchError(() => graph.subscribe('not a function' as unknown as () => void)) ) .then('should throw', (_, error) => expect(error).toEqual(expect.any(TypeError))); }, @@ -2609,17 +2609,17 @@ scenario( 'snapshot', bdd => { bdd - .given('a store with cross-referencing nodes', () => { - const { controller, store } = createStoreWithNodes(); + .given('a graph with cross-referencing nodes', () => { + const { controller, graph } = createGraphWithNodes(); - return { controller, snapshots: [store.getSnapshot()], store }; + return { controller, snapshots: [graph.getSnapshot()], graph }; }) - .when('multiple transactions succeed', ({ controller, snapshots, store }) => { + .when('multiple transactions succeed', ({ controller, snapshots, graph }) => { controller.edit(transaction => transaction.upsert('_:m-1', node => ({ ...node!, text: ['Aloha'] }))); - snapshots.push(store.getSnapshot()); + snapshots.push(graph.getSnapshot()); controller.edit(transaction => transaction.delete('_:m-2')); - snapshots.push(store.getSnapshot()); + snapshots.push(graph.getSnapshot()); controller.edit(transaction => { transaction.upsert('_:m-3', () => ({ @@ -2628,7 +2628,7 @@ scenario( isPartOf: new Set([{ '@id': '_:conversation' }]) })); }); - snapshots.push(store.getSnapshot()); + snapshots.push(graph.getSnapshot()); return snapshots; }) @@ -2662,10 +2662,10 @@ scenario( ); bdd - .given('a store with cross-referencing nodes', () => createStoreWithNodes()) + .given('a graph with cross-referencing nodes', () => createGraphWithNodes()) .when( 'a node referenced by another node is deleted in a failed transaction, then in a successful one', - ({ controller, store }) => { + ({ controller, graph }) => { catchError(() => controller.edit(transaction => { transaction.delete('_:m-1'); @@ -2676,7 +2676,7 @@ scenario( controller.edit(transaction => transaction.delete('_:m-1')); - return store.getSnapshot(); + return graph.getSnapshot(); } ) .then('should remove references to the deleted node', (_, snapshot) => { @@ -2689,7 +2689,7 @@ scenario( ); function upsertDatedMessage( - transaction: StoreTransaction, + transaction: GraphTransaction, id: `_:${string}`, parentIds: readonly `_:${string}`[], dateCreated?: number | string | undefined @@ -2707,30 +2707,30 @@ function upsertDatedMessage( })); } -function createStoreWithConversations() { - const { controller, store } = createStore(); +function createGraphWithConversations() { + const { controller, graph } = createGraph(); controller.edit(transaction => { transaction.upsert('_:c-1', () => ({ '@id': '_:c-1', '@type': new Set(['Conversation']) })); transaction.upsert('_:c-2', () => ({ '@id': '_:c-2', '@type': new Set(['Conversation']) })); }); - return { controller, store }; + return { controller, graph }; } function getSortedPartIdsOf( - store: Store, + graph: Graph, parentId: `_:${string}`, order?: GetSortedPartsInit['order'] | undefined ): string[] { - return Array.from(store.getSortedParts(parentId, { by: 'dateCreated', order }), child => child['@id']); + return Array.from(graph.getSortedParts(parentId, { by: 'dateCreated', order }), child => child['@id']); } scenario( 'getSortedParts', bdd => { bdd - .given('a store with conversations', () => createStoreWithConversations()) + .given('a graph with conversations', () => createGraphWithConversations()) .when('children are added out of order', ({ controller }) => controller.edit(transaction => { upsertDatedMessage(transaction, '_:m-3', ['_:c-1'], 3); @@ -2738,33 +2738,33 @@ scenario( upsertDatedMessage(transaction, '_:m-2', ['_:c-1'], 2); }) ) - .then('should sort by dateCreated', ({ store }) => - expect(getSortedPartIdsOf(store, '_:c-1')).toEqual(['_:m-1', '_:m-2', '_:m-3']) + .then('should sort by dateCreated', ({ graph }) => + expect(getSortedPartIdsOf(graph, '_:c-1')).toEqual(['_:m-1', '_:m-2', '_:m-3']) ) - .and('should return the children nodes', ({ store }) => { - const snapshot = store.getSnapshot(); + .and('should return the children nodes', ({ graph }) => { + const snapshot = graph.getSnapshot(); - expect(Array.from(store.getSortedParts('_:c-1', { by: 'dateCreated' }))).toEqual([ + expect(Array.from(graph.getSortedParts('_:c-1', { by: 'dateCreated' }))).toEqual([ snapshot.get('_:m-1'), snapshot.get('_:m-2'), snapshot.get('_:m-3') ]); }) - .and('should not affect other parent', ({ store }) => expect(getSortedPartIdsOf(store, '_:c-2')).toEqual([])); + .and('should not affect other parent', ({ graph }) => expect(getSortedPartIdsOf(graph, '_:c-2')).toEqual([])); bdd - .given('a store with conversations', () => createStoreWithConversations()) + .given('a graph with conversations', () => createGraphWithConversations()) .when('children are added in multiple transactions out of order', ({ controller }) => { for (const dateCreated of [5, 1, 4, 2, 3]) { controller.edit(transaction => upsertDatedMessage(transaction, `_:m-${dateCreated}`, ['_:c-1'], dateCreated)); } }) - .then('should sort by dateCreated', ({ store }) => - expect(getSortedPartIdsOf(store, '_:c-1')).toEqual(['_:m-1', '_:m-2', '_:m-3', '_:m-4', '_:m-5']) + .then('should sort by dateCreated', ({ graph }) => + expect(getSortedPartIdsOf(graph, '_:c-1')).toEqual(['_:m-1', '_:m-2', '_:m-3', '_:m-4', '_:m-5']) ); bdd - .given('a store with conversations', () => createStoreWithConversations()) + .given('a graph with conversations', () => createGraphWithConversations()) .when('children without dateCreated are added', ({ controller }) => controller.edit(transaction => { upsertDatedMessage(transaction, '_:m-b', ['_:c-1']); @@ -2773,25 +2773,25 @@ scenario( upsertDatedMessage(transaction, '_:m-1', ['_:c-1'], 1); }) ) - .then('should put them at the end, sorted by @id', ({ store }) => - expect(getSortedPartIdsOf(store, '_:c-1')).toEqual(['_:m-1', '_:m-2', '_:m-a', '_:m-b']) + .then('should put them at the end, sorted by @id', ({ graph }) => + expect(getSortedPartIdsOf(graph, '_:c-1')).toEqual(['_:m-1', '_:m-2', '_:m-a', '_:m-b']) ); bdd - .given('a store with conversations', () => createStoreWithConversations()) + .given('a graph with conversations', () => createGraphWithConversations()) .when('children sharing same dateCreated are added', ({ controller }) => { controller.edit(transaction => upsertDatedMessage(transaction, '_:m-c', ['_:c-1'], 1)); controller.edit(transaction => upsertDatedMessage(transaction, '_:m-a', ['_:c-1'], 1)); controller.edit(transaction => upsertDatedMessage(transaction, '_:m-0', ['_:c-1'], 2)); controller.edit(transaction => upsertDatedMessage(transaction, '_:m-b', ['_:c-1'], 1)); }) - .then('should sort them by @id', ({ store }) => - expect(getSortedPartIdsOf(store, '_:c-1')).toEqual(['_:m-a', '_:m-b', '_:m-c', '_:m-0']) + .then('should sort them by @id', ({ graph }) => + expect(getSortedPartIdsOf(graph, '_:c-1')).toEqual(['_:m-a', '_:m-b', '_:m-c', '_:m-0']) ); bdd - .given('a store with 3 sorted children', () => { - const { controller, store } = createStoreWithConversations(); + .given('a graph with 3 sorted children', () => { + const { controller, graph } = createGraphWithConversations(); controller.edit(transaction => { upsertDatedMessage(transaction, '_:m-1', ['_:c-1'], 1); @@ -2799,18 +2799,18 @@ scenario( upsertDatedMessage(transaction, '_:m-3', ['_:c-1'], 3); }); - return { controller, store }; + return { controller, graph }; }) .when('dateCreated of a child is changed', ({ controller }) => controller.edit(transaction => upsertDatedMessage(transaction, '_:m-1', ['_:c-1'], 4)) ) - .then('should re-sort', ({ store }) => - expect(getSortedPartIdsOf(store, '_:c-1')).toEqual(['_:m-2', '_:m-3', '_:m-1']) + .then('should re-sort', ({ graph }) => + expect(getSortedPartIdsOf(graph, '_:c-1')).toEqual(['_:m-2', '_:m-3', '_:m-1']) ); bdd - .given('a store with 3 sorted children and a result of getSortedParts()', () => { - const { controller, store } = createStoreWithConversations(); + .given('a graph with 3 sorted children and a result of getSortedParts()', () => { + const { controller, graph } = createGraphWithConversations(); controller.edit(transaction => { upsertDatedMessage(transaction, '_:m-1', ['_:c-1'], 1); @@ -2818,9 +2818,9 @@ scenario( upsertDatedMessage(transaction, '_:m-3', ['_:c-1'], 3); }); - const snapshot = store.getSnapshot(); + const snapshot = graph.getSnapshot(); - return { children: store.getSortedParts('_:c-1', { by: 'dateCreated' }), controller, snapshot, store }; + return { children: graph.getSortedParts('_:c-1', { by: 'dateCreated' }), controller, snapshot, graph }; }) .when('a child is removed, a child is deleted, and a child is added', ({ controller }) => controller.edit(transaction => { @@ -2829,33 +2829,33 @@ scenario( upsertDatedMessage(transaction, '_:m-0', ['_:c-1'], 0); }) ) - .then('should update the sorted children', ({ store }) => - expect(getSortedPartIdsOf(store, '_:c-1')).toEqual(['_:m-0', '_:m-3']) + .then('should update the sorted children', ({ graph }) => + expect(getSortedPartIdsOf(graph, '_:c-1')).toEqual(['_:m-0', '_:m-3']) ) .and('should keep the earlier result unchanged', ({ children, snapshot }) => expect(Array.from(children)).toEqual([snapshot.get('_:m-1'), snapshot.get('_:m-2'), snapshot.get('_:m-3')]) ); bdd - .given('a store with a child in a conversation', () => { - const { controller, store } = createStoreWithConversations(); + .given('a graph with a child in a conversation', () => { + const { controller, graph } = createGraphWithConversations(); controller.edit(transaction => upsertDatedMessage(transaction, '_:m-1', ['_:c-1'], 1)); - return { controller, store }; + return { controller, graph }; }) .when('the child is moved to another conversation', ({ controller }) => controller.edit(transaction => upsertDatedMessage(transaction, '_:m-1', ['_:c-2'], 1)) ) - .then('should remove from the previous parent', ({ store }) => - expect(getSortedPartIdsOf(store, '_:c-1')).toEqual([]) + .then('should remove from the previous parent', ({ graph }) => + expect(getSortedPartIdsOf(graph, '_:c-1')).toEqual([]) ) - .and('should add to the new parent', ({ store }) => - expect(getSortedPartIdsOf(store, '_:c-2')).toEqual(['_:m-1']) + .and('should add to the new parent', ({ graph }) => + expect(getSortedPartIdsOf(graph, '_:c-2')).toEqual(['_:m-1']) ); bdd - .given('a store with conversations', () => createStoreWithConversations()) + .given('a graph with conversations', () => createGraphWithConversations()) .when('a child is part of 2 parents', ({ controller }) => controller.edit(transaction => { upsertDatedMessage(transaction, '_:m-2', ['_:c-1', '_:c-2'], 2); @@ -2863,14 +2863,14 @@ scenario( upsertDatedMessage(transaction, '_:m-3', ['_:c-2'], 3); }) ) - .then('should appear in both parents', ({ store }) => { - expect(getSortedPartIdsOf(store, '_:c-1')).toEqual(['_:m-1', '_:m-2']); - expect(getSortedPartIdsOf(store, '_:c-2')).toEqual(['_:m-2', '_:m-3']); + .then('should appear in both parents', ({ graph }) => { + expect(getSortedPartIdsOf(graph, '_:c-1')).toEqual(['_:m-1', '_:m-2']); + expect(getSortedPartIdsOf(graph, '_:c-2')).toEqual(['_:m-2', '_:m-3']); }); bdd - .given('a store with a child shared by 2 parents, first in one and last in another', () => { - const { controller, store } = createStoreWithConversations(); + .given('a graph with a child shared by 2 parents, first in one and last in another', () => { + const { controller, graph } = createGraphWithConversations(); controller.edit(transaction => { upsertDatedMessage(transaction, '_:m-1', ['_:c-1'], 1); @@ -2880,7 +2880,7 @@ scenario( upsertDatedMessage(transaction, '_:m-x', ['_:c-2'], 1); }); - return { controller, store }; + return { controller, graph }; }) .when.oneOf([ [ @@ -2932,59 +2932,59 @@ scenario( } ] ]) - .then('should sort the first parent properly', ({ store }, [expected]) => - expect(getSortedPartIdsOf(store, '_:c-1')).toEqual(expected) + .then('should sort the first parent properly', ({ graph }, [expected]) => + expect(getSortedPartIdsOf(graph, '_:c-1')).toEqual(expected) ) - .and('should sort the second parent properly', ({ store }, [, expected]) => - expect(getSortedPartIdsOf(store, '_:c-2')).toEqual(expected) + .and('should sort the second parent properly', ({ graph }, [, expected]) => + expect(getSortedPartIdsOf(graph, '_:c-2')).toEqual(expected) ); bdd - .given('a store with a child in a conversation', () => { - const { controller, store } = createStoreWithConversations(); + .given('a graph with a child in a conversation', () => { + const { controller, graph } = createGraphWithConversations(); controller.edit(transaction => upsertDatedMessage(transaction, '_:m-1', ['_:c-1'], 1)); - return { controller, parent: store.getSnapshot().get('_:c-1'), store }; + return { controller, parent: graph.getSnapshot().get('_:c-1'), graph }; }) .when('the child is updated without changing dateCreated and isPartOf', ({ controller }) => controller.edit(transaction => transaction.upsert('_:m-1', node => ({ ...node!, '@id': '_:m-1', text: ['Hello, World!'] })) ) ) - .then('should not change the parent', ({ parent, store }) => - expect(store.getSnapshot().get('_:c-1')).toBe(parent) + .then('should not change the parent', ({ parent, graph }) => + expect(graph.getSnapshot().get('_:c-1')).toBe(parent) ) - .and('should return the updated child', ({ store }) => - expect(Array.from(store.getSortedParts('_:c-1', { by: 'dateCreated' }))[0]).toBe( - store.getSnapshot().get('_:m-1') + .and('should return the updated child', ({ graph }) => + expect(Array.from(graph.getSortedParts('_:c-1', { by: 'dateCreated' }))[0]).toBe( + graph.getSnapshot().get('_:m-1') ) ); bdd - .given('a store with a child in a conversation', () => { - const { controller, store } = createStoreWithConversations(); + .given('a graph with a child in a conversation', () => { + const { controller, graph } = createGraphWithConversations(); controller.edit(transaction => upsertDatedMessage(transaction, '_:m-1', ['_:c-1'], 1)); - return { controller, store }; + return { controller, graph }; }) .when('the parent is updated', ({ controller }) => controller.edit(transaction => transaction.upsert('_:c-1', node => ({ ...node!, '@id': '_:c-1', name: ['Conversation 1'] })) ) ) - .then('should keep sorted children', ({ store }) => - expect(getSortedPartIdsOf(store, '_:c-1')).toEqual(['_:m-1']) + .then('should keep sorted children', ({ graph }) => + expect(getSortedPartIdsOf(graph, '_:c-1')).toEqual(['_:m-1']) ); bdd - .given('a store with a child in a conversation', () => { - const { controller, store } = createStoreWithConversations(); + .given('a graph with a child in a conversation', () => { + const { controller, graph } = createGraphWithConversations(); controller.edit(transaction => upsertDatedMessage(transaction, '_:m-1', ['_:c-1'], 1)); - return { controller, store }; + return { controller, graph }; }) .when('a transaction adding a child failed', ({ controller }) => catchError(() => @@ -2995,17 +2995,17 @@ scenario( }) ) ) - .then('should keep sorted children', ({ store }) => - expect(getSortedPartIdsOf(store, '_:c-1')).toEqual(['_:m-1']) + .then('should keep sorted children', ({ graph }) => + expect(getSortedPartIdsOf(graph, '_:c-1')).toEqual(['_:m-1']) ); bdd - .given('a store with a child in a conversation', () => { - const { controller, store } = createStoreWithConversations(); + .given('a graph with a child in a conversation', () => { + const { controller, graph } = createGraphWithConversations(); controller.edit(transaction => upsertDatedMessage(transaction, '_:m-1', ['_:c-1'], 1)); - return { controller, store }; + return { controller, graph }; }) .when('the parent is deleted and re-created in same transaction', ({ controller }) => controller.edit(transaction => { @@ -3018,19 +3018,19 @@ scenario( upsertDatedMessage(transaction, '_:m-2', ['_:c-1'], 2); }) ) - .then('should only have the new child', ({ store }) => - expect(getSortedPartIdsOf(store, '_:c-1')).toEqual(['_:m-2']) + .then('should only have the new child', ({ graph }) => + expect(getSortedPartIdsOf(graph, '_:c-1')).toEqual(['_:m-2']) ); bdd - .given('a store with a parent with children', () => { - const { controller, store } = createStoreWithConversations(); + .given('a graph with a parent with children', () => { + const { controller, graph } = createGraphWithConversations(); controller.edit(transaction => upsertDatedMessage(transaction, '_:m-1', ['_:c-1'], 1)); - return store; + return graph; }) - .when('getSortedParts() is called', store => store.getSortedParts('_:c-1', { by: 'dateCreated' })) + .when('getSortedParts() is called', graph => graph.getSortedParts('_:c-1', { by: 'dateCreated' })) .then('should be iterable multiple times', (_, children) => { expect(Array.from(children, child => child['@id'])).toEqual(['_:m-1']); expect(Array.from(children, child => child['@id'])).toEqual(['_:m-1']); @@ -3038,8 +3038,8 @@ scenario( .and('should be frozen', (_, children) => expect(Object.isFrozen(children)).toBe(true)); bdd - .given('a store with 3 sorted children and a started iteration', () => { - const { controller, store } = createStoreWithConversations(); + .given('a graph with 3 sorted children and a started iteration', () => { + const { controller, graph } = createGraphWithConversations(); controller.edit(transaction => { upsertDatedMessage(transaction, '_:m-1', ['_:c-1'], 1); @@ -3047,7 +3047,7 @@ scenario( upsertDatedMessage(transaction, '_:m-3', ['_:c-1'], 3); }); - const iterator = store.getSortedParts('_:c-1', { by: 'dateCreated' })[Symbol.iterator](); + const iterator = graph.getSortedParts('_:c-1', { by: 'dateCreated' })[Symbol.iterator](); return { controller, firstChild: iterator.next().value, iterator }; }) @@ -3064,52 +3064,52 @@ scenario( }); bdd - .given('a store with conversations', () => createStoreWithConversations()) - .when('getSortedParts() is called on a node without children', ({ store }) => - store.getSortedParts('_:c-1', { by: 'dateCreated' }) + .given('a graph with conversations', () => createGraphWithConversations()) + .when('getSortedParts() is called on a node without children', ({ graph }) => + graph.getSortedParts('_:c-1', { by: 'dateCreated' }) ) .then('should return an empty iterable', (_, children) => expect(Array.from(children)).toEqual([])); bdd - .given('a store with conversations', () => createStoreWithConversations()) - .when('getSortedParts() is called on a node not in the store', ({ store }) => - store.getSortedParts('_:unknown', { by: 'dateCreated' }) + .given('a graph with conversations', () => createGraphWithConversations()) + .when('getSortedParts() is called on a node not in the graph', ({ graph }) => + graph.getSortedParts('_:unknown', { by: 'dateCreated' }) ) .then('should return an empty iterable', (_, children) => expect(Array.from(children)).toEqual([])); bdd - .given('a store with conversations', () => createStoreWithConversations()) + .given('a graph with conversations', () => createGraphWithConversations()) .when.oneOf([ [ 'getSortedParts() is called with a non-string', - ({ store }) => catchError(() => store.getSortedParts({} as NodeIdentifier, { by: 'dateCreated' })) + ({ graph }) => catchError(() => graph.getSortedParts({} as NodeIdentifier, { by: 'dateCreated' })) ], [ 'getSortedParts() is called with an invalid ID', - ({ store }) => catchError(() => store.getSortedParts('m-1' as NodeIdentifier, { by: 'dateCreated' })) + ({ graph }) => catchError(() => graph.getSortedParts('m-1' as NodeIdentifier, { by: 'dateCreated' })) ], [ 'getSortedParts() is called without init', - ({ store }) => + ({ graph }) => catchError(() => - (store.getSortedParts as (id: NodeIdentifier) => Iterable).call(store, '_:c-1') + (graph.getSortedParts as (id: NodeIdentifier) => Iterable).call(graph, '_:c-1') ) ], [ 'getSortedParts() is called with unsupported by', - ({ store }) => - catchError(() => store.getSortedParts('_:c-1', { by: 'position' } as unknown as GetSortedPartsInit)) + ({ graph }) => + catchError(() => graph.getSortedParts('_:c-1', { by: 'position' } as unknown as GetSortedPartsInit)) ], [ 'getSortedParts() is called with sortBy instead of by', - ({ store }) => - catchError(() => store.getSortedParts('_:c-1', { sortBy: 'dateCreated' } as unknown as GetSortedPartsInit)) + ({ graph }) => + catchError(() => graph.getSortedParts('_:c-1', { sortBy: 'dateCreated' } as unknown as GetSortedPartsInit)) ], [ 'getSortedParts() is called with unsupported order', - ({ store }) => + ({ graph }) => catchError(() => - store.getSortedParts('_:c-1', { by: 'dateCreated', order: 'random' } as unknown as GetSortedPartsInit) + graph.getSortedParts('_:c-1', { by: 'dateCreated', order: 'random' } as unknown as GetSortedPartsInit) ) ] ]) @@ -3122,8 +3122,8 @@ scenario( 'getSortedParts with order', bdd => { bdd - .given('a store with sorted children, some without dateCreated', () => { - const { controller, store } = createStoreWithConversations(); + .given('a graph with sorted children, some without dateCreated', () => { + const { controller, graph } = createGraphWithConversations(); controller.edit(transaction => { upsertDatedMessage(transaction, '_:m-3', ['_:c-1'], 3); @@ -3133,23 +3133,23 @@ scenario( upsertDatedMessage(transaction, '_:m-2', ['_:c-1'], 2); }); - return { controller, store }; + return { controller, graph }; }) .when.oneOf([ - ['getSortedParts() is called without order', ({ store }) => getSortedPartIdsOf(store, '_:c-1')], + ['getSortedParts() is called without order', ({ graph }) => getSortedPartIdsOf(graph, '_:c-1')], [ 'getSortedParts() is called with order of undefined', - ({ store }) => getSortedPartIdsOf(store, '_:c-1', undefined) + ({ graph }) => getSortedPartIdsOf(graph, '_:c-1', undefined) ], - ['getSortedParts() is called with order of "asc"', ({ store }) => getSortedPartIdsOf(store, '_:c-1', 'asc')] + ['getSortedParts() is called with order of "asc"', ({ graph }) => getSortedPartIdsOf(graph, '_:c-1', 'asc')] ]) .then('should sort in ascending order', (_, ids) => expect(ids).toEqual(['_:m-1', '_:m-2', '_:m-3', '_:m-a', '_:m-b']) ); bdd - .given('a store with sorted children, some without dateCreated', () => { - const { controller, store } = createStoreWithConversations(); + .given('a graph with sorted children, some without dateCreated', () => { + const { controller, graph } = createGraphWithConversations(); controller.edit(transaction => { upsertDatedMessage(transaction, '_:m-3', ['_:c-1'], 3); @@ -3159,16 +3159,16 @@ scenario( upsertDatedMessage(transaction, '_:m-2', ['_:c-1'], 2); }); - return { controller, store }; + return { controller, graph }; }) - .when('getSortedParts() is called with order of "desc"', ({ store }) => - store.getSortedParts('_:c-1', { by: 'dateCreated', order: 'desc' }) + .when('getSortedParts() is called with order of "desc"', ({ graph }) => + graph.getSortedParts('_:c-1', { by: 'dateCreated', order: 'desc' }) ) .then('should sort in descending order, the exact reverse of ascending order', (_, children) => expect(Array.from(children, child => child['@id'])).toEqual(['_:m-b', '_:m-a', '_:m-3', '_:m-2', '_:m-1']) ) - .and('should return the children nodes', ({ store }, children) => { - const snapshot = store.getSnapshot(); + .and('should return the children nodes', ({ graph }, children) => { + const snapshot = graph.getSnapshot(); expect(Array.from(children)).toEqual(['_:m-b', '_:m-a', '_:m-3', '_:m-2', '_:m-1'].map(id => snapshot.get(id))); }) @@ -3177,8 +3177,8 @@ scenario( ); bdd - .given('a store with 3 sorted children and a result of getSortedParts() in descending order', () => { - const { controller, store } = createStoreWithConversations(); + .given('a graph with 3 sorted children and a result of getSortedParts() in descending order', () => { + const { controller, graph } = createGraphWithConversations(); controller.edit(transaction => { upsertDatedMessage(transaction, '_:m-1', ['_:c-1'], 1); @@ -3186,7 +3186,7 @@ scenario( upsertDatedMessage(transaction, '_:m-3', ['_:c-1'], 3); }); - return { children: store.getSortedParts('_:c-1', { by: 'dateCreated', order: 'desc' }), controller, store }; + return { children: graph.getSortedParts('_:c-1', { by: 'dateCreated', order: 'desc' }), controller, graph }; }) .when('a child is deleted and a child is added', ({ controller }) => controller.edit(transaction => { @@ -3197,14 +3197,14 @@ scenario( .then('the previous result should not be affected', ({ children }) => expect(Array.from(children, child => child['@id'])).toEqual(['_:m-3', '_:m-2', '_:m-1']) ) - .and('should return the changes in descending order', ({ store }) => - expect(getSortedPartIdsOf(store, '_:c-1', 'desc')).toEqual(['_:m-4', '_:m-3', '_:m-1']) + .and('should return the changes in descending order', ({ graph }) => + expect(getSortedPartIdsOf(graph, '_:c-1', 'desc')).toEqual(['_:m-4', '_:m-3', '_:m-1']) ); bdd - .given('a store with conversations', () => createStoreWithConversations()) - .when('getSortedParts() is called with order of "desc" on a parent without children', ({ store }) => - getSortedPartIdsOf(store, '_:c-1', 'desc') + .given('a graph with conversations', () => createGraphWithConversations()) + .when('getSortedParts() is called with order of "desc" on a parent without children', ({ graph }) => + getSortedPartIdsOf(graph, '_:c-1', 'desc') ) .then('should return an empty iterable', (_, ids) => expect(ids).toEqual([])); }, @@ -3224,7 +3224,7 @@ scenario( ['a set of strings', new Set(['2026-01-01T00:00:00Z'])] ] satisfies [string, unknown][]) { bdd - .given('a store with conversations', () => createStoreWithConversations()) + .given('a graph with conversations', () => createGraphWithConversations()) .when(`a node with dateCreated of ${name} is upserted`, ({ controller }) => catchError(() => controller.edit(transaction => @@ -3236,22 +3236,22 @@ scenario( ) ) .then('should throw', (_, error) => expect(error).toBeTruthy()) - .and('should not add the node', ({ store }) => expect(store.getSnapshot().has('_:m-1')).toBe(false)); + .and('should not add the node', ({ graph }) => expect(graph.getSnapshot().has('_:m-1')).toBe(false)); } for (const dateCreated of ['2026-01-01T00:00:00Z', '2026-01-01T00:00:00.123Z', '2026-01-01T08:00:00+08:00']) { bdd - .given('a store with conversations', () => createStoreWithConversations()) + .given('a graph with conversations', () => createGraphWithConversations()) .when(`a node with dateCreated of "${dateCreated}" is upserted`, ({ controller }) => controller.edit(transaction => upsertDatedMessage(transaction, '_:m-1', [], dateCreated)) ) - .then('should add the node as-is', ({ store }) => - expect(store.getSnapshot().get('_:m-1')).toHaveProperty('dateCreated', dateCreated) + .then('should add the node as-is', ({ graph }) => + expect(graph.getSnapshot().get('_:m-1')).toHaveProperty('dateCreated', dateCreated) ); } bdd - .given('a store with conversations', () => createStoreWithConversations()) + .given('a graph with conversations', () => createGraphWithConversations()) .when('children with dateCreated in different time zones are added', ({ controller }) => controller.edit(transaction => { // 2026-01-01T00:00:00Z @@ -3262,22 +3262,22 @@ scenario( upsertDatedMessage(transaction, '_:m-c', ['_:c-1'], '2025-12-31T19:00:00.5-05:00'); }) ) - .then('should sort as Date', ({ store }) => - expect(getSortedPartIdsOf(store, '_:c-1')).toEqual(['_:m-b', '_:m-a', '_:m-c']) + .then('should sort as Date', ({ graph }) => + expect(getSortedPartIdsOf(graph, '_:c-1')).toEqual(['_:m-b', '_:m-a', '_:m-c']) ); bdd - .given('a store with 2 children of same dateCreated in different time zones', () => { - const { controller, store } = createStoreWithConversations(); + .given('a graph with 2 children of same dateCreated in different time zones', () => { + const { controller, graph } = createGraphWithConversations(); controller.edit(transaction => { upsertDatedMessage(transaction, '_:m-2', ['_:c-1'], '2026-01-01T02:00:00+02:00'); upsertDatedMessage(transaction, '_:m-1', ['_:c-1'], '2026-01-01T00:00:00Z'); }); - return { controller, store }; + return { controller, graph }; }) - .when('getSortedParts() is called', ({ store }) => getSortedPartIdsOf(store, '_:c-1')) + .when('getSortedParts() is called', ({ graph }) => getSortedPartIdsOf(graph, '_:c-1')) .then('should sort by @id', (_, ids) => expect(ids).toEqual(['_:m-1', '_:m-2'])); }, NodeTest @@ -3287,7 +3287,7 @@ scenario( 'upsert with JSON literal', bdd => { bdd - .given('an empty store', () => createStore()) + .given('an empty graph', () => createGraph()) .when('a node with a JSON literal which is not plain JSON is upserted', ({ controller }) => { const json = { a: [undefined], date: new Date(0), nan: NaN, undefined }; @@ -3302,13 +3302,13 @@ scenario( return json; }) - .then('should store the JSON literal as JSON.parse(JSON.stringify())', ({ store }, json) => - expect(getPath(store.getSnapshot().get('_:m-1'), 'encoding', 0, '@value')).toEqual( + .then('should store the JSON literal as JSON.parse(JSON.stringify())', ({ graph }, json) => + expect(getPath(graph.getSnapshot().get('_:m-1'), 'encoding', 0, '@value')).toEqual( JSON.parse(JSON.stringify(json)) ) ) - .and('should store Date as ISO string, NaN as null, and undefined in array as null', ({ store }) => - expect(getPath(store.getSnapshot().get('_:m-1'), 'encoding', 0, '@value')).toEqual({ + .and('should store Date as ISO string, NaN as null, and undefined in array as null', ({ graph }) => + expect(getPath(graph.getSnapshot().get('_:m-1'), 'encoding', 0, '@value')).toEqual({ a: [null], date: '1970-01-01T00:00:00.000Z', nan: null @@ -3318,8 +3318,8 @@ scenario( NodeTest ); -function createStoreWithIdentifiers() { - const { controller, store } = createStore(); +function createGraphWithIdentifiers() { + const { controller, graph } = createGraph(); const calls: string[][] = []; controller.edit(transaction => { @@ -3336,42 +3336,42 @@ function createStoreWithIdentifiers() { })); }); - store.subscribe(affectedIds => calls.push(Array.from(affectedIds).sort())); + graph.subscribe(affectedIds => calls.push(Array.from(affectedIds).sort())); - return { calls, controller, store }; + return { calls, controller, graph }; } scenario( 'identifier', bdd => { bdd - .given('a store with nodes with alternate identifiers', () => createStoreWithIdentifiers()) - .when('queryByIdentifier() is called with @id, alternate identifiers, and an unknown identifier', ({ store }) => + .given('a graph with nodes with alternate identifiers', () => createGraphWithIdentifiers()) + .when('queryByIdentifier() is called with @id, alternate identifiers, and an unknown identifier', ({ graph }) => (['_:m-1', 'https://example.com/m-1', '_:client-1', '_:client-2', '_:unknown'] as const).map(identifier => - store.queryByIdentifier(identifier) + graph.queryByIdentifier(identifier) ) ) - .then('should return the node matching either @id or alternate identifier', ({ store }, nodes) => + .then('should return the node matching either @id or alternate identifier', ({ graph }, nodes) => expect(nodes).toEqual([ - store.getSnapshot().get('_:m-1'), - store.getSnapshot().get('_:m-1'), - store.getSnapshot().get('_:m-1'), - store.getSnapshot().get('_:m-2'), + graph.getSnapshot().get('_:m-1'), + graph.getSnapshot().get('_:m-1'), + graph.getSnapshot().get('_:m-1'), + graph.getSnapshot().get('_:m-2'), undefined ]) ) - .and('should return the same node object as the snapshot', ({ store }, nodes) => - expect(nodes[1]).toBe(store.getSnapshot().get('_:m-1')) + .and('should return the same node object as the snapshot', ({ graph }, nodes) => + expect(nodes[1]).toBe(graph.getSnapshot().get('_:m-1')) ) - .and('should store identifier as an immutable Set', ({ store }) => { - const identifier = store.getSnapshot().get('_:m-1')?.identifier; + .and('should store identifier as an immutable Set', ({ graph }) => { + const identifier = graph.getSnapshot().get('_:m-1')?.identifier; expect(identifier).toEqual(new Set(['https://example.com/m-1', '_:client-1'])); expect(() => Set.prototype.clear.call(identifier)).toThrow(TypeError); }); bdd - .given('a store with nodes with alternate identifiers', () => createStoreWithIdentifiers()) + .given('a graph with nodes with alternate identifiers', () => createGraphWithIdentifiers()) .when('identifier of a node is changed by upsert', ({ controller }) => controller.edit(transaction => transaction.upsert('_:m-1', node => ({ @@ -3380,21 +3380,21 @@ scenario( })) ) ) - .then('should find the node by the new identifier', ({ store }) => - expect(store.queryByIdentifier('_:server-1')).toBe(store.getSnapshot().get('_:m-1')) + .then('should find the node by the new identifier', ({ graph }) => + expect(graph.queryByIdentifier('_:server-1')).toBe(graph.getSnapshot().get('_:m-1')) ) - .and('should find the node by the unchanged identifier', ({ store }) => - expect(store.queryByIdentifier('_:client-1')).toBe(store.getSnapshot().get('_:m-1')) + .and('should find the node by the unchanged identifier', ({ graph }) => + expect(graph.queryByIdentifier('_:client-1')).toBe(graph.getSnapshot().get('_:m-1')) ) - .and('should not find the node by the removed identifier', ({ store }) => - expect(store.queryByIdentifier('https://example.com/m-1')).toBeUndefined() + .and('should not find the node by the removed identifier', ({ graph }) => + expect(graph.queryByIdentifier('https://example.com/m-1')).toBeUndefined() ) .and('should notify with @id and identifiers before and after the change', ({ calls }) => expect(calls).toEqual([['_:client-1', '_:m-1', '_:server-1', 'https://example.com/m-1']]) ); bdd - .given('a store with nodes with alternate identifiers', () => createStoreWithIdentifiers()) + .given('a graph with nodes with alternate identifiers', () => createGraphWithIdentifiers()) .when('identifier of a node is removed by upsert', ({ controller }) => controller.edit(transaction => transaction.upsert('_:m-2', node => { @@ -3404,18 +3404,18 @@ scenario( }) ) ) - .then('should not find the node by the removed identifier', ({ store }) => - expect(store.queryByIdentifier('_:client-2')).toBeUndefined() + .then('should not find the node by the removed identifier', ({ graph }) => + expect(graph.queryByIdentifier('_:client-2')).toBeUndefined() ) - .and('should still find the node by @id', ({ store }) => - expect(store.queryByIdentifier('_:m-2')).toBe(store.getSnapshot().get('_:m-2')) + .and('should still find the node by @id', ({ graph }) => + expect(graph.queryByIdentifier('_:m-2')).toBe(graph.getSnapshot().get('_:m-2')) ) .and('should notify with @id and the removed identifier', ({ calls }) => expect(calls).toEqual([['_:client-2', '_:m-2']]) ); bdd - .given('a store with nodes with alternate identifiers', () => createStoreWithIdentifiers()) + .given('a graph with nodes with alternate identifiers', () => createGraphWithIdentifiers()) .when('a node is upserted without changing identifier', ({ controller }) => controller.edit(transaction => transaction.upsert('_:m-2', node => ({ ...node!, text: ['Aloha'] }))) ) @@ -3424,19 +3424,19 @@ scenario( ); bdd - .given('a store with nodes with alternate identifiers', () => createStoreWithIdentifiers()) + .given('a graph with nodes with alternate identifiers', () => createGraphWithIdentifiers()) .when('a node is deleted', ({ controller }) => controller.edit(transaction => transaction.delete('_:m-1'))) .then('should notify with @id and identifiers of the deleted node', ({ calls }) => expect(calls).toEqual([['_:client-1', '_:m-1', 'https://example.com/m-1']]) ) - .and('should not find the deleted node by @id or identifier', ({ store }) => { - expect(store.queryByIdentifier('_:m-1')).toBeUndefined(); - expect(store.queryByIdentifier('_:client-1')).toBeUndefined(); - expect(store.queryByIdentifier('https://example.com/m-1')).toBeUndefined(); + .and('should not find the deleted node by @id or identifier', ({ graph }) => { + expect(graph.queryByIdentifier('_:m-1')).toBeUndefined(); + expect(graph.queryByIdentifier('_:client-1')).toBeUndefined(); + expect(graph.queryByIdentifier('https://example.com/m-1')).toBeUndefined(); }); bdd - .given('a store with nodes with alternate identifiers', () => createStoreWithIdentifiers()) + .given('a graph with nodes with alternate identifiers', () => createGraphWithIdentifiers()) .when( 'a node is deleted and its identifier is reused by another node in the same transaction', ({ controller }) => @@ -3445,28 +3445,28 @@ scenario( transaction.delete('_:m-1'); }) ) - .then('should find the other node by the reused identifier', ({ store }) => - expect(store.queryByIdentifier('_:client-1')).toBe(store.getSnapshot().get('_:m-2')) + .then('should find the other node by the reused identifier', ({ graph }) => + expect(graph.queryByIdentifier('_:client-1')).toBe(graph.getSnapshot().get('_:m-2')) ) - .and('should not find the other node by its removed identifier', ({ store }) => - expect(store.queryByIdentifier('_:client-2')).toBeUndefined() + .and('should not find the other node by its removed identifier', ({ graph }) => + expect(graph.queryByIdentifier('_:client-2')).toBeUndefined() ); bdd - .given('a store with nodes with alternate identifiers', () => createStoreWithIdentifiers()) + .given('a graph with nodes with alternate identifiers', () => createGraphWithIdentifiers()) .when('identifiers of two nodes are swapped in the same transaction', ({ controller }) => controller.edit(transaction => { transaction.upsert('_:m-1', node => ({ ...node!, identifier: new Set(['_:client-2'] as const) })); transaction.upsert('_:m-2', node => ({ ...node!, identifier: new Set(['_:client-1'] as const) })); }) ) - .then('should find the nodes by swapped identifiers', ({ store }) => { - expect(store.queryByIdentifier('_:client-1')).toBe(store.getSnapshot().get('_:m-2')); - expect(store.queryByIdentifier('_:client-2')).toBe(store.getSnapshot().get('_:m-1')); + .then('should find the nodes by swapped identifiers', ({ graph }) => { + expect(graph.queryByIdentifier('_:client-1')).toBe(graph.getSnapshot().get('_:m-2')); + expect(graph.queryByIdentifier('_:client-2')).toBe(graph.getSnapshot().get('_:m-1')); }); bdd - .given('a store with nodes with alternate identifiers', () => createStoreWithIdentifiers()) + .given('a graph with nodes with alternate identifiers', () => createGraphWithIdentifiers()) .when.oneOf([ [ 'identifier contains @id of the node', @@ -3537,16 +3537,16 @@ scenario( ] ]) .then('should throw', (_, error) => expect(error).toBeInstanceOf(Error)) - .and('should not change the store', ({ store }) => { - expect(store.queryByIdentifier('_:client-1')).toBe(store.getSnapshot().get('_:m-1')); - expect(store.queryByIdentifier('_:client-2')).toBe(store.getSnapshot().get('_:m-2')); - expect(store.queryByIdentifier('_:client-3')).toBeUndefined(); - expect(store.getSnapshot().size).toBe(2); + .and('should not change the graph', ({ graph }) => { + expect(graph.queryByIdentifier('_:client-1')).toBe(graph.getSnapshot().get('_:m-1')); + expect(graph.queryByIdentifier('_:client-2')).toBe(graph.getSnapshot().get('_:m-2')); + expect(graph.queryByIdentifier('_:client-3')).toBeUndefined(); + expect(graph.getSnapshot().size).toBe(2); }) .and('should not notify', ({ calls }) => expect(calls).toEqual([])); bdd - .given('a store with nodes with alternate identifiers', () => createStoreWithIdentifiers()) + .given('a graph with nodes with alternate identifiers', () => createGraphWithIdentifiers()) .when('a node references another node by its alternate identifier in isPartOf', ({ controller }) => catchError(() => controller.edit(transaction => @@ -3565,9 +3565,9 @@ scenario( ); bdd - .given('a store with nodes with alternate identifiers', () => createStoreWithIdentifiers()) - .when('queryByIdentifier() is called with an invalid identifier', ({ store }) => - catchError(() => store.queryByIdentifier('m-1' as NodeIdentifier)) + .given('a graph with nodes with alternate identifiers', () => createGraphWithIdentifiers()) + .when('queryByIdentifier() is called with an invalid identifier', ({ graph }) => + catchError(() => graph.queryByIdentifier('m-1' as NodeIdentifier)) ) .then('should throw', (_, error) => expect(error).toBeInstanceOf(Error)); }, @@ -3578,62 +3578,62 @@ scenario( 'getSortedParts with sub-millisecond dateCreated', bdd => { bdd - .given('a store with conversations', () => createStoreWithConversations()) + .given('a graph with conversations', () => createGraphWithConversations()) .when('children with dateCreated within the same millisecond are added', ({ controller }) => controller.edit(transaction => { upsertDatedMessage(transaction, '_:m-a', ['_:c-1'], '2026-01-01T00:00:00.0009000Z'); upsertDatedMessage(transaction, '_:m-b', ['_:c-1'], '2026-01-01T00:00:00.0001000Z'); }) ) - .then('should drop sub-millisecond and sort by @id', ({ store }) => - expect(getSortedPartIdsOf(store, '_:c-1')).toEqual(['_:m-a', '_:m-b']) + .then('should drop sub-millisecond and sort by @id', ({ graph }) => + expect(getSortedPartIdsOf(graph, '_:c-1')).toEqual(['_:m-a', '_:m-b']) ) - .and('should keep dateCreated as-is', ({ store }) => - expect(store.getSnapshot().get('_:m-a')).toHaveProperty('dateCreated', '2026-01-01T00:00:00.0009000Z') + .and('should keep dateCreated as-is', ({ graph }) => + expect(graph.getSnapshot().get('_:m-a')).toHaveProperty('dateCreated', '2026-01-01T00:00:00.0009000Z') ); bdd - .given('a store with conversations', () => createStoreWithConversations()) + .given('a graph with conversations', () => createGraphWithConversations()) .when('children with nanosecond precision dateCreated are added', ({ controller }) => controller.edit(transaction => { upsertDatedMessage(transaction, '_:m-a', ['_:c-1'], '2026-01-01T00:00:00.000000002Z'); upsertDatedMessage(transaction, '_:m-b', ['_:c-1'], '2026-01-01T00:00:00.000000001Z'); }) ) - .then('should drop sub-millisecond and sort by @id', ({ store }) => - expect(getSortedPartIdsOf(store, '_:c-1')).toEqual(['_:m-a', '_:m-b']) + .then('should drop sub-millisecond and sort by @id', ({ graph }) => + expect(getSortedPartIdsOf(graph, '_:c-1')).toEqual(['_:m-a', '_:m-b']) ); bdd - .given('a store with conversations', () => createStoreWithConversations()) + .given('a graph with conversations', () => createGraphWithConversations()) .when('children with dateCreated in different milliseconds are added', ({ controller }) => controller.edit(transaction => { upsertDatedMessage(transaction, '_:m-a', ['_:c-1'], '2026-01-01T00:00:00.0029999Z'); upsertDatedMessage(transaction, '_:m-b', ['_:c-1'], '2026-01-01T00:00:00.0010001Z'); }) ) - .then('should sort by dateCreated in milliseconds', ({ store }) => - expect(getSortedPartIdsOf(store, '_:c-1')).toEqual(['_:m-b', '_:m-a']) + .then('should sort by dateCreated in milliseconds', ({ graph }) => + expect(getSortedPartIdsOf(graph, '_:c-1')).toEqual(['_:m-b', '_:m-a']) ); bdd - .given('a store with 2 children within the same millisecond', () => { - const { controller, store } = createStoreWithConversations(); + .given('a graph with 2 children within the same millisecond', () => { + const { controller, graph } = createGraphWithConversations(); controller.edit(transaction => { upsertDatedMessage(transaction, '_:m-a', ['_:c-1'], '2026-01-01T00:00:00.0001000Z'); upsertDatedMessage(transaction, '_:m-b', ['_:c-1'], '2026-01-01T00:00:00.0005000Z'); }); - return { controller, store }; + return { controller, graph }; }) .when('dateCreated of a child is changed within the same millisecond', ({ controller }) => controller.edit(transaction => upsertDatedMessage(transaction, '_:m-a', ['_:c-1'], '2026-01-01T00:00:00.0009000Z') ) ) - .then('should keep sorting by @id', ({ store }) => - expect(getSortedPartIdsOf(store, '_:c-1')).toEqual(['_:m-a', '_:m-b']) + .then('should keep sorting by @id', ({ graph }) => + expect(getSortedPartIdsOf(graph, '_:c-1')).toEqual(['_:m-a', '_:m-b']) ); }, NodeTest diff --git a/packages/store/src/Store.ts b/packages/store-graph/src/Graph.ts similarity index 97% rename from packages/store/src/Store.ts rename to packages/store-graph/src/Graph.ts index 9499429cb1..65fb3e4e41 100644 --- a/packages/store/src/Store.ts +++ b/packages/store-graph/src/Graph.ts @@ -32,15 +32,15 @@ import { updateIdentifierIndex } from './private/identifierIndex.ts'; import { insertSortedChild, removeSortedChild } from './private/sortedChildIds.ts'; import type { VersionedStorageInit } from './structure/VersionedStorage.ts'; -type StoreListener = (affectedNodeIds: ReadonlySet) => void; +type GraphListener = (affectedNodeIds: ReadonlySet) => void; -interface StoreSource { - start(controller: StoreController): void; +interface GraphSource { + start(controller: GraphController): void; } -interface StoreController { +interface GraphController { /** - * Edits the store in a transaction. All changes are committed at once after the transactor returns, or discarded if + * Edits the graph in a transaction. All changes are committed at once after the transactor returns, or discarded if * the transactor throws. * * `edit()` cannot be called inside another `edit()`, i.e. inside the transactor or the updater. Listeners are called @@ -50,10 +50,10 @@ interface StoreController { * discarded. The synchronous part of the Promise could succeed while the asynchronous part would fail, the caller * will not be very aware of what's going on. */ - edit(transactor: (transaction: StoreTransaction) => void): void; + edit(transactor: (transaction: GraphTransaction) => void): void; } -interface StoreTransaction { +interface GraphTransaction { /** * Deletes a node and removes all references pointing to the deleted node from other nodes. * @@ -80,7 +80,7 @@ interface StoreTransaction { * Other than named properties (e.g. `@id`, `@type`), all property values must be an array, e.g. `text: ['Hello']`. * * The updater must be a pure function of the node it receives. Transaction methods (i.e. `upsert()` and `delete()`) - * cannot be called inside the updater, they will throw without making any changes. Reading the store (e.g. + * cannot be called inside the updater, they will throw without making any changes. Reading the graph (e.g. * `getSnapshot()`) inside the updater is allowed. */ upsert(id: NodeIdentifier, updater: (node: VettedNode | undefined) => Node): void; @@ -259,11 +259,11 @@ function updateSortedChildIds( ); } -class Store { - constructor(source: StoreSource) { +class Graph { + constructor(source: GraphSource) { let editing = false; - const controller: StoreController = { + const controller: GraphController = { edit: transactor => { if (typeof transactor !== 'function') { throw new TypeError('transactor must be a function'); @@ -299,11 +299,11 @@ class Store { #forwardReferenceIds: ReferenceIds = new FrozenMap([], UNORDERED); /** Key is an alternate identifier (in `identifier` property), value is `@id` of the node which has it. */ #identifierIndex: FrozenMap = new FrozenMap([], UNORDERED); - #listeners: Set = new Set(); + #listeners: Set = new Set(); #state: FrozenMap = new FrozenMap([], UNORDERED); /** - * Runs the transactor and commits changes to the store. + * Runs the transactor and commits changes to the graph. * * Multiple nodes can be added at once and they could cross reference each other. All nodes are added at once, part * relationships are mirrored, then the graph integrity is verified. @@ -311,7 +311,7 @@ class Store { * @returns IDs of nodes which are added, updated, or deleted, plus their alternate identifiers before and after the * change, or `undefined` if nothing is changed. */ - #commit(transactor: (transaction: StoreTransaction) => void): ReadonlySet | undefined { + #commit(transactor: (transaction: GraphTransaction) => void): ReadonlySet | undefined { const affectedNodeIds = new Set(); let ended = false; @@ -751,7 +751,7 @@ class Store { * nodes which are added, updated, or deleted, plus their alternate identifiers (`identifier` property) before and * after the change. */ - subscribe(listener: StoreListener): () => void { + subscribe(listener: GraphListener): () => void { if (typeof listener !== 'function') { throw new TypeError('listener must be a function'); } @@ -764,5 +764,5 @@ class Store { } } -export default Store; -export type { GetSortedPartsInit, StoreListener, StoreSource, StoreController, StoreTransaction }; +export default Graph; +export type { GetSortedPartsInit, GraphListener, GraphSource, GraphController, GraphTransaction }; diff --git a/packages/store/src/OPINIONS.md b/packages/store-graph/src/OPINIONS.md similarity index 98% rename from packages/store/src/OPINIONS.md rename to packages/store-graph/src/OPINIONS.md index 8b78720dae..af7f5f53ca 100644 --- a/packages/store/src/OPINIONS.md +++ b/packages/store-graph/src/OPINIONS.md @@ -31,7 +31,7 @@ The new design is distributed, transactional (consistent without locking), and h - Every node can has multiple `@types` for extensibility reason - Able to iterate a sorted list of children based on `dateCreated`, can extend upto 3 hardcoded properties -The store is a standard, plain and opinionated JSON-LD graph implementation. The only custom parts are: +The graph is a standard, plain and opinionated JSON-LD graph implementation. The only custom parts are: - Consistency: a simple and friendly way to auto-inverse `hasPart`/`isPartOf` - High performance: able to iterate children sorted by `dateCreated` @@ -104,7 +104,7 @@ We intended to keep custom parts minimal to reduce code complexity and learning - First read in a transaction: the node in the graph, it is frozen - Subsequent reads: a new plain copy with changes made in the transaction, it is writable only inside the updater and is frozen after the updater returns or throws - Unchanged `hasPart`/`isPartOf` passed back from the updater are not validated nor diffed again, `O(1)` regardless of membership size - - Nodes are plain objects, the store may attach hidden state invisible to userland, however, copies (e.g. `JSON.parse(JSON.stringify(node))`) is always accepted and hidden state is rebuilt + - Nodes are plain objects, the graph may attach hidden state invisible to userland, however, copies (e.g. `JSON.parse(JSON.stringify(node))`) is always accepted and hidden state is rebuilt - Node edges must be by-reference - Nodes are auto-converted to immutable by deep freezing - Array and objects are cloned before frozen, inputs are never modified @@ -122,7 +122,7 @@ We intended to keep custom parts minimal to reduce code complexity and learning - Read is always clean and consistent - `getSnapshot()` returns a hashmap of nodes keyed by `@id`, there is no contract on its iteration order - Iteration order is unspecified and may change between versions (e.g. a deleted node upserted again may not be the last), callers must not depend on it - - Story: the graph is a set of nodes, not a list, the only order offered by the store is sorted children via `getSortedParts()` + - Story: the graph is a set of nodes, not a list, the only order offered by the graph is sorted children via `getSortedParts()` - Query a single node by ID - Query a single node by either `@id` or one of its alternate identifiers via `queryByIdentifier()`, `O(1)` - Listeners are notified with `@id` of affected nodes (including deleted nodes), plus their alternate identifiers before and after the change diff --git a/packages/store/src/assertion/assertIdentifierNotId.ts b/packages/store-graph/src/assertion/assertIdentifierNotId.ts similarity index 100% rename from packages/store/src/assertion/assertIdentifierNotId.ts rename to packages/store-graph/src/assertion/assertIdentifierNotId.ts diff --git a/packages/store/src/assertion/assertNoPartChangeToUpserted.test.ts b/packages/store-graph/src/assertion/assertNoPartChangeToUpserted.test.ts similarity index 100% rename from packages/store/src/assertion/assertNoPartChangeToUpserted.test.ts rename to packages/store-graph/src/assertion/assertNoPartChangeToUpserted.test.ts diff --git a/packages/store/src/assertion/assertNoPartChangeToUpserted.ts b/packages/store-graph/src/assertion/assertNoPartChangeToUpserted.ts similarity index 100% rename from packages/store/src/assertion/assertNoPartChangeToUpserted.ts rename to packages/store-graph/src/assertion/assertNoPartChangeToUpserted.ts diff --git a/packages/store/src/assertion/assertNoPartConflict.ts b/packages/store-graph/src/assertion/assertNoPartConflict.ts similarity index 100% rename from packages/store/src/assertion/assertNoPartConflict.ts rename to packages/store-graph/src/assertion/assertNoPartConflict.ts diff --git a/packages/store/src/assertion/assertNoPendingParts.ts b/packages/store-graph/src/assertion/assertNoPendingParts.ts similarity index 100% rename from packages/store/src/assertion/assertNoPendingParts.ts rename to packages/store-graph/src/assertion/assertNoPendingParts.ts diff --git a/packages/store/src/assertion/assertNotSelfPart.ts b/packages/store-graph/src/assertion/assertNotSelfPart.ts similarity index 100% rename from packages/store/src/assertion/assertNotSelfPart.ts rename to packages/store-graph/src/assertion/assertNotSelfPart.ts diff --git a/packages/store/src/assertion/assertReferencedNodesInGraph.ts b/packages/store-graph/src/assertion/assertReferencedNodesInGraph.ts similarity index 100% rename from packages/store/src/assertion/assertReferencedNodesInGraph.ts rename to packages/store-graph/src/assertion/assertReferencedNodesInGraph.ts diff --git a/packages/store/src/assertion/assertUniqueIdentifier.ts b/packages/store-graph/src/assertion/assertUniqueIdentifier.ts similarity index 100% rename from packages/store/src/assertion/assertUniqueIdentifier.ts rename to packages/store-graph/src/assertion/assertUniqueIdentifier.ts diff --git a/packages/store/src/benchmark.json.ts b/packages/store-graph/src/benchmark.json.ts similarity index 100% rename from packages/store/src/benchmark.json.ts rename to packages/store-graph/src/benchmark.json.ts diff --git a/packages/store/src/benchmark.node.ts b/packages/store-graph/src/benchmark.node.ts similarity index 100% rename from packages/store/src/benchmark.node.ts rename to packages/store-graph/src/benchmark.node.ts diff --git a/packages/store/src/benchmark.ts b/packages/store-graph/src/benchmark.ts similarity index 93% rename from packages/store/src/benchmark.ts rename to packages/store-graph/src/benchmark.ts index 1089304f78..1fe278f46e 100644 --- a/packages/store/src/benchmark.ts +++ b/packages/store-graph/src/benchmark.ts @@ -3,7 +3,7 @@ /* eslint-disable no-restricted-globals */ /** - * Benchmarks the store and prints average per node as a Markdown table, one row per benchmark, one column per number + * Benchmarks the graph and prints average per node as a Markdown table, one row per benchmark, one column per number * of nodes. * * Run by `npm run benchmark`. Numbers should stay flat (or grow slowly) across a row as the number of nodes grows, linear @@ -37,7 +37,7 @@ * Benchmarks measured per operation (`µs/op`) run `NUM_OPERATIONS` operations, they should stay flat across a row. */ import type { VettedNode } from './node/Node.ts'; -import Store, { type StoreController } from './Store.ts'; +import Graph, { type GraphController } from './Graph.ts'; const NUM_NODES = Object.freeze([1_000, 10_000, 50_000, 100_000]); const NUM_OPERATIONS = 100; @@ -65,10 +65,10 @@ function gc(): void { typeof globalThis.gc === 'function' && globalThis.gc(); } -function createStore(): { readonly controller: StoreController; readonly store: Store } { - let controller: StoreController | undefined; +function createGraph(): { readonly controller: GraphController; readonly graph: Graph } { + let controller: GraphController | undefined; - const store = new Store({ + const graph = new Graph({ start(nextController) { controller = nextController; } @@ -82,7 +82,7 @@ function createStore(): { readonly controller: StoreController; readonly store: transaction.upsert('_:c-1', () => ({ '@id': '_:c-1', '@type': new Set(['Conversation']) })) ); - return Object.freeze({ controller, store }); + return Object.freeze({ controller, graph }); } /** Returns indices in order, or shuffled by Fisher-Yates. */ @@ -101,16 +101,16 @@ function createIndices(numNodes: number, order: Order): number[] { } /** - * Upserts children into a store. + * Upserts children into a graph. * - * @returns The store and time spent in milliseconds. + * @returns The graph and time spent in milliseconds. */ function upsertChildren( numNodes: number, order: Order, withIdentifier: boolean = false -): { readonly controller: StoreController; readonly duration: number; readonly store: Store } { - const { controller, store } = createStore(); +): { readonly controller: GraphController; readonly duration: number; readonly graph: Graph } { + const { controller, graph } = createGraph(); const indices = createIndices(numNodes, order); const start = performance.now(); @@ -128,7 +128,7 @@ function upsertChildren( ); } - return Object.freeze({ controller, duration: performance.now() - start, store }); + return Object.freeze({ controller, duration: performance.now() - start, graph }); } function createMessage(index: number, isPartOf?: VettedNode['isPartOf']) { @@ -144,7 +144,7 @@ function createMessage(index: number, isPartOf?: VettedNode['isPartOf']) { /** Upserts children without parent, then measures upserting the parent with `hasPart` of all children. */ function adopt(numNodes: number): number { - const { controller } = createStore(); + const { controller } = createGraph(); const ids = Array.from({ length: numNodes }, (_, index) => `_:m-${index}` as const); controller.edit(transaction => ids.forEach((id, index) => transaction.upsert(id, () => createMessage(index)))); @@ -163,7 +163,7 @@ function adopt(numNodes: number): number { /** Measures upserting a parent with `hasPart` of children not in the graph yet, then the children. */ function pending(numNodes: number): number { - const { controller } = createStore(); + const { controller } = createGraph(); const ids = Array.from({ length: numNodes }, (_, index) => `_:m-${index}` as const); const start = performance.now(); @@ -210,11 +210,11 @@ function changeIdentifier(numNodes: number): number { /** Appends children with alternate identifier, then measures querying every child by its alternate identifier. */ function queryByIdentifier(numNodes: number): number { - const { store } = upsertChildren(numNodes, 'append', true); + const { graph } = upsertChildren(numNodes, 'append', true); const start = performance.now(); for (let index = 0; index < numNodes; index++) { - if (store.queryByIdentifier(`_:client-${index}`)?.['@id'] !== `_:m-${index}`) { + if (graph.queryByIdentifier(`_:client-${index}`)?.['@id'] !== `_:m-${index}`) { throw new Error(`ASSERTION: Should find "_:m-${index}" by "_:client-${index}"`); } } @@ -300,7 +300,7 @@ function repeatUpsert(numNodes: number): number { /** Upserts children without parent, then measures adopting them and updating the parent again in one transaction. */ function adoptThenReread(numNodes: number): number { - const { controller } = createStore(); + const { controller } = createGraph(); const ids = Array.from({ length: numNodes }, (_, index) => `_:m-${index}` as const); controller.edit(transaction => ids.forEach((id, index) => transaction.upsert(id, () => createMessage(index)))); @@ -335,11 +335,11 @@ function mirrorChurn(numNodes: number): number { return performance.now() - start; } -function iterate(store: Store, numNodes: number): number { +function iterate(graph: Graph, numNodes: number): number { const start = performance.now(); let numIterated = 0; - for (const _ of store.getSortedParts('_:c-1', { by: 'dateCreated' })) { + for (const _ of graph.getSortedParts('_:c-1', { by: 'dateCreated' })) { numIterated++; } @@ -358,14 +358,14 @@ function measureMemory(numNodes: number): number | undefined { gc(); const before = process.memoryUsage().heapUsed; - const { store } = upsertChildren(numNodes, 'append'); + const { graph } = upsertChildren(numNodes, 'append'); gc(); const after = process.memoryUsage().heapUsed; - // Keeps the store alive until measured. - store.getSnapshot(); + // Keeps the graph alive until measured. + graph.getSnapshot(); return after - before; } @@ -440,7 +440,7 @@ const BENCHMARKS: readonly Benchmark[] = Object.freeze([ { fractionDigits: 3, name: 'Iterate', - run: (numNodes: number) => iterate(upsertChildren(numNodes, 'append').store, numNodes), + run: (numNodes: number) => iterate(upsertChildren(numNodes, 'append').graph, numNodes), scale: 1_000, unit: 'µs/node' }, diff --git a/packages/store/src/goldenStories/AGENTS.md b/packages/store-graph/src/goldenStories/AGENTS.md similarity index 100% rename from packages/store/src/goldenStories/AGENTS.md rename to packages/store-graph/src/goldenStories/AGENTS.md diff --git a/packages/store/src/goldenStories/alternateId/delete.test.ts b/packages/store-graph/src/goldenStories/alternateId/delete.test.ts similarity index 68% rename from packages/store/src/goldenStories/alternateId/delete.test.ts rename to packages/store-graph/src/goldenStories/alternateId/delete.test.ts index a33a8f3c34..563c0366cf 100644 --- a/packages/store/src/goldenStories/alternateId/delete.test.ts +++ b/packages/store-graph/src/goldenStories/alternateId/delete.test.ts @@ -1,13 +1,13 @@ import { scenario } from '@testduet/given-when-then'; import assert from 'node:assert'; import * as NodeTest from 'node:test'; -import createStoreAndController from '../createStoreAndController.ts'; +import createGraphAndController from '../createGraphAndController.ts'; scenario( 'Inserted nodes are sorted by "dateCreated" property', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .when('a node is inserted', precondition => { precondition.controller.edit(transaction => { transaction.upsert('_:m-00001', () => ({ @@ -19,16 +19,16 @@ scenario( return precondition; }) - .then('should be queryable by "identifier"', ({ store }) => { - assert(store.queryByIdentifier('urn:com:example:message:1')); + .then('should be queryable by "identifier"', ({ graph }) => { + assert(graph.queryByIdentifier('urn:com:example:message:1')); }) .when('the node is deleted', ({ controller }) => { controller.edit(transaction => { transaction.delete('_:m-00001'); }); }) - .then('should not be queryable by "identifier"', ({ store }) => { - assert(!store.queryByIdentifier('urn:com:example:message:1')); + .then('should not be queryable by "identifier"', ({ graph }) => { + assert(!graph.queryByIdentifier('urn:com:example:message:1')); }); }, NodeTest diff --git a/packages/store/src/goldenStories/alternateId/insert.test.ts b/packages/store-graph/src/goldenStories/alternateId/insert.test.ts similarity index 83% rename from packages/store/src/goldenStories/alternateId/insert.test.ts rename to packages/store-graph/src/goldenStories/alternateId/insert.test.ts index 83fcdf9a9f..46fbd84fb9 100644 --- a/packages/store/src/goldenStories/alternateId/insert.test.ts +++ b/packages/store-graph/src/goldenStories/alternateId/insert.test.ts @@ -1,13 +1,13 @@ import { scenario } from '@testduet/given-when-then'; import assert from 'node:assert'; import * as NodeTest from 'node:test'; -import createStoreAndController from '../createStoreAndController.ts'; +import createGraphAndController from '../createGraphAndController.ts'; scenario( 'Inserted nodes are sorted by "dateCreated" property', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .and('a node is inserted', precondition => { precondition.controller.edit(transaction => { transaction.upsert('_:m-00001', () => ({ @@ -19,9 +19,9 @@ scenario( return precondition; }) - .when('query the children nodes by both "@id" and "identifier"', ({ store }) => ({ - byAtId: store.queryByIdentifier('_:m-00001'), - byIdentifier: store.queryByIdentifier('urn:com:example:message:1') + .when('query the children nodes by both "@id" and "identifier"', ({ graph }) => ({ + byAtId: graph.queryByIdentifier('_:m-00001'), + byIdentifier: graph.queryByIdentifier('urn:com:example:message:1') })) .then('should query by "@id"', (_, outcome) => { assert(outcome.byAtId); diff --git a/packages/store/src/goldenStories/alternateId/switch.test.ts b/packages/store-graph/src/goldenStories/alternateId/switch.test.ts similarity index 87% rename from packages/store/src/goldenStories/alternateId/switch.test.ts rename to packages/store-graph/src/goldenStories/alternateId/switch.test.ts index 4a6b59734b..132492da1a 100644 --- a/packages/store/src/goldenStories/alternateId/switch.test.ts +++ b/packages/store-graph/src/goldenStories/alternateId/switch.test.ts @@ -2,13 +2,13 @@ import { scenario } from '@testduet/given-when-then'; import { expect } from 'expect'; import assert from 'node:assert'; import * as NodeTest from 'node:test'; -import createStoreAndController from '../createStoreAndController.ts'; +import createGraphAndController from '../createGraphAndController.ts'; scenario( 'Switching "identifier" property value between 2 nodes', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .and('two nodes are inserted', precondition => { precondition.controller.edit(transaction => { transaction.upsert('_:m-00001', () => ({ @@ -32,8 +32,8 @@ scenario( transaction.upsert('_:m-00002', node => ({ ...node!, identifier: new Set(['urn:com:example:message:1']) })); }); }) - .then('first node should have correct "identifier" value', ({ store }) => { - const firstNode = store.queryByIdentifier('_:m-00001')!; + .then('first node should have correct "identifier" value', ({ graph }) => { + const firstNode = graph.queryByIdentifier('_:m-00001')!; assert(firstNode); expect(firstNode).toStrictEqual({ @@ -44,8 +44,8 @@ scenario( assert.deepStrictEqual(Array.from(firstNode['@type']), ['Message']); assert.deepStrictEqual(Array.from(firstNode.identifier!), ['urn:com:example:message:2']); }) - .and('second node should have correct "identifier" value', ({ store }) => { - const secondNode = store.queryByIdentifier('_:m-00002')!; + .and('second node should have correct "identifier" value', ({ graph }) => { + const secondNode = graph.queryByIdentifier('_:m-00002')!; assert(secondNode); expect(secondNode).toStrictEqual({ diff --git a/packages/store/src/goldenStories/alternateId/unique.test.ts b/packages/store-graph/src/goldenStories/alternateId/unique.test.ts similarity index 90% rename from packages/store/src/goldenStories/alternateId/unique.test.ts rename to packages/store-graph/src/goldenStories/alternateId/unique.test.ts index a41d9419ea..2201d1bac3 100644 --- a/packages/store/src/goldenStories/alternateId/unique.test.ts +++ b/packages/store-graph/src/goldenStories/alternateId/unique.test.ts @@ -1,13 +1,13 @@ import { scenario } from '@testduet/given-when-then'; import assert from 'node:assert'; import * as NodeTest from 'node:test'; -import createStoreAndController from '../createStoreAndController.ts'; +import createGraphAndController from '../createGraphAndController.ts'; scenario( '"identifier" value should be unique across nodes', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .and('a node is inserted', precondition => { precondition.controller.edit(transaction => { transaction.upsert('_:m-00001', () => ({ diff --git a/packages/store/src/goldenStories/alternateId/update.test.ts b/packages/store-graph/src/goldenStories/alternateId/update.test.ts similarity index 86% rename from packages/store/src/goldenStories/alternateId/update.test.ts rename to packages/store-graph/src/goldenStories/alternateId/update.test.ts index f0cd5ca066..04fbd19a7f 100644 --- a/packages/store/src/goldenStories/alternateId/update.test.ts +++ b/packages/store-graph/src/goldenStories/alternateId/update.test.ts @@ -1,13 +1,13 @@ import { scenario } from '@testduet/given-when-then'; import assert from 'node:assert'; import * as NodeTest from 'node:test'; -import createStoreAndController from '../createStoreAndController.ts'; +import createGraphAndController from '../createGraphAndController.ts'; scenario( 'Inserted nodes are sorted by "dateCreated" property', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .and('a node is inserted and updated with new "identifier"', precondition => { precondition.controller.edit(transaction => { transaction.upsert('_:m-00001', () => ({ @@ -27,9 +27,9 @@ scenario( return precondition; }) - .when('query the children nodes by both "@id" and "identifier"', ({ store }) => ({ - byAtId: store.queryByIdentifier('_:m-00001'), - byIdentifier: store.queryByIdentifier('urn:com:example:message:2') + .when('query the children nodes by both "@id" and "identifier"', ({ graph }) => ({ + byAtId: graph.queryByIdentifier('_:m-00001'), + byIdentifier: graph.queryByIdentifier('urn:com:example:message:2') })) .then('should query by "@id"', (_, outcome) => { assert(outcome.byAtId); @@ -37,8 +37,8 @@ scenario( .and('should query by new "identifier"', (_, outcome) => { assert(outcome.byIdentifier); }) - .and('should not queryable by old "identifier"', ({ store }) => { - assert(!store.queryByIdentifier('urn:com:example:message:1')); + .and('should not queryable by old "identifier"', ({ graph }) => { + assert(!graph.queryByIdentifier('urn:com:example:message:1')); }) .and('should be same instance', (_, outcome) => { assert.equal(outcome.byAtId, outcome.byIdentifier); @@ -67,8 +67,8 @@ scenario( transaction.upsert('_:m-00001', node => ({ ...node!, identifier: new Set(['urn:com:example:message:3']) })); }); }) - .then('should be queryable by latest "identifier" value', ({ store }) => { - assert(store.queryByIdentifier('urn:com:example:message:3')); + .then('should be queryable by latest "identifier" value', ({ graph }) => { + assert(graph.queryByIdentifier('urn:com:example:message:3')); }) .and('should notify listener with both ID', ({ onUpdate }) => { // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain diff --git a/packages/store/src/goldenStories/autoPart/hasPart.conflict.insert.test.ts b/packages/store-graph/src/goldenStories/autoPart/hasPart.conflict.insert.test.ts similarity index 93% rename from packages/store/src/goldenStories/autoPart/hasPart.conflict.insert.test.ts rename to packages/store-graph/src/goldenStories/autoPart/hasPart.conflict.insert.test.ts index a97c4e23fe..9792f88172 100644 --- a/packages/store/src/goldenStories/autoPart/hasPart.conflict.insert.test.ts +++ b/packages/store-graph/src/goldenStories/autoPart/hasPart.conflict.insert.test.ts @@ -3,17 +3,17 @@ import assert from 'node:assert'; import * as NodeTest from 'node:test'; import { type NodeReference } from '../../index.ts'; import buildPermutatedFunction from '../buildPermutatedFunction.ts'; -import createStoreAndController from '../createStoreAndController.ts'; +import createGraphAndController from '../createGraphAndController.ts'; scenario( '"hasPart" has conflicting auto-fix as the node is being inserted', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .when.oneOf( buildPermutatedFunction< [{ hasPart: Set } | { hasPart?: never }, boolean], - (precondition: ReturnType) => void + (precondition: ReturnType) => void >( [ [ diff --git a/packages/store/src/goldenStories/autoPart/hasPart.conflict.update.test.ts b/packages/store-graph/src/goldenStories/autoPart/hasPart.conflict.update.test.ts similarity index 95% rename from packages/store/src/goldenStories/autoPart/hasPart.conflict.update.test.ts rename to packages/store-graph/src/goldenStories/autoPart/hasPart.conflict.update.test.ts index 41d41f9a7d..fc55d287e8 100644 --- a/packages/store/src/goldenStories/autoPart/hasPart.conflict.update.test.ts +++ b/packages/store-graph/src/goldenStories/autoPart/hasPart.conflict.update.test.ts @@ -3,13 +3,13 @@ import assert from 'node:assert'; import * as NodeTest from 'node:test'; import type { NodeReference } from '../../index.ts'; import buildPermutatedFunction from '../buildPermutatedFunction.ts'; -import createStoreAndController from '../createStoreAndController.ts'; +import createGraphAndController from '../createGraphAndController.ts'; scenario( '"hasPart" has conflicting auto-fix as the node is being updated', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .when.oneOf( buildPermutatedFunction( [ diff --git a/packages/store/src/goldenStories/autoPart/hasPart.happy.test.ts b/packages/store-graph/src/goldenStories/autoPart/hasPart.happy.test.ts similarity index 89% rename from packages/store/src/goldenStories/autoPart/hasPart.happy.test.ts rename to packages/store-graph/src/goldenStories/autoPart/hasPart.happy.test.ts index 322c1e672a..373a98ee4c 100644 --- a/packages/store/src/goldenStories/autoPart/hasPart.happy.test.ts +++ b/packages/store-graph/src/goldenStories/autoPart/hasPart.happy.test.ts @@ -4,13 +4,13 @@ import assert from 'node:assert'; import * as NodeTest from 'node:test'; import { type BlankNodeIdentifier, type NodeReference } from '../../index.ts'; import buildPermutatedFunction from '../buildPermutatedFunction.ts'; -import createStoreAndController from '../createStoreAndController.ts'; +import createGraphAndController from '../createGraphAndController.ts'; scenario( '"hasPart" is auto-fixed without conflict', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .when.oneOf( buildPermutatedFunction( [ @@ -44,8 +44,8 @@ scenario( } ) ) - .then('the parent node should have `hasPart`', ({ store }) => { - const parent = store.queryByIdentifier('_:conversation')!; + .then('the parent node should have `hasPart`', ({ graph }) => { + const parent = graph.queryByIdentifier('_:conversation')!; expect(parent).toStrictEqual({ '@id': '_:conversation', diff --git a/packages/store/src/goldenStories/autoPart/isPartOf.conflict.insert.test.ts b/packages/store-graph/src/goldenStories/autoPart/isPartOf.conflict.insert.test.ts similarity index 95% rename from packages/store/src/goldenStories/autoPart/isPartOf.conflict.insert.test.ts rename to packages/store-graph/src/goldenStories/autoPart/isPartOf.conflict.insert.test.ts index 0e3888d1a9..4867101ebb 100644 --- a/packages/store/src/goldenStories/autoPart/isPartOf.conflict.insert.test.ts +++ b/packages/store-graph/src/goldenStories/autoPart/isPartOf.conflict.insert.test.ts @@ -3,13 +3,13 @@ import assert from 'node:assert'; import * as NodeTest from 'node:test'; import { type NodeReference } from '../../index.ts'; import buildPermutatedFunction from '../buildPermutatedFunction.ts'; -import createStoreAndController from '../createStoreAndController.ts'; +import createGraphAndController from '../createGraphAndController.ts'; scenario( '"isPartOf" has conflicting auto-fix as the node is being inserted', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .when.oneOf( buildPermutatedFunction( [ diff --git a/packages/store/src/goldenStories/autoPart/isPartOf.conflict.update.test.ts b/packages/store-graph/src/goldenStories/autoPart/isPartOf.conflict.update.test.ts similarity index 95% rename from packages/store/src/goldenStories/autoPart/isPartOf.conflict.update.test.ts rename to packages/store-graph/src/goldenStories/autoPart/isPartOf.conflict.update.test.ts index bebfeb9963..f1e9dac301 100644 --- a/packages/store/src/goldenStories/autoPart/isPartOf.conflict.update.test.ts +++ b/packages/store-graph/src/goldenStories/autoPart/isPartOf.conflict.update.test.ts @@ -3,13 +3,13 @@ import assert from 'node:assert'; import * as NodeTest from 'node:test'; import type { NodeReference } from '../../index.ts'; import buildPermutatedFunction from '../buildPermutatedFunction.ts'; -import createStoreAndController from '../createStoreAndController.ts'; +import createGraphAndController from '../createGraphAndController.ts'; scenario( '"isPartOf" has conflicting auto-fix as the node is being updated', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .when.oneOf( buildPermutatedFunction( [ diff --git a/packages/store/src/goldenStories/autoPart/isPartOf.happy.test.ts b/packages/store-graph/src/goldenStories/autoPart/isPartOf.happy.test.ts similarity index 85% rename from packages/store/src/goldenStories/autoPart/isPartOf.happy.test.ts rename to packages/store-graph/src/goldenStories/autoPart/isPartOf.happy.test.ts index 87c8782233..3135f8f6f8 100644 --- a/packages/store/src/goldenStories/autoPart/isPartOf.happy.test.ts +++ b/packages/store-graph/src/goldenStories/autoPart/isPartOf.happy.test.ts @@ -4,13 +4,13 @@ import assert from 'node:assert'; import * as NodeTest from 'node:test'; import { type BlankNodeIdentifier, type NodeReference } from '../../index.ts'; import buildPermutatedFunction from '../buildPermutatedFunction.ts'; -import createStoreAndController from '../createStoreAndController.ts'; +import createGraphAndController from '../createGraphAndController.ts'; scenario( '"isPartOf" is auto-fixed without conflict', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .when.oneOf( buildPermutatedFunction( [ @@ -44,8 +44,8 @@ scenario( } ) ) - .then('the first child node should have `isPartOf`', ({ store }) => { - const firstChild = store.queryByIdentifier('_:m-00001')!; + .then('the first child node should have `isPartOf`', ({ graph }) => { + const firstChild = graph.queryByIdentifier('_:m-00001')!; expect(firstChild).toStrictEqual({ '@id': '_:m-00001', @@ -57,8 +57,8 @@ scenario( { '@id': '_:conversation' satisfies BlankNodeIdentifier } satisfies NodeReference ]); }) - .and('the second child node should have `isPartOf`', ({ store }) => { - const secondChild = store.queryByIdentifier('_:m-00002')!; + .and('the second child node should have `isPartOf`', ({ graph }) => { + const secondChild = graph.queryByIdentifier('_:m-00002')!; expect(secondChild).toStrictEqual({ '@id': '_:m-00002', diff --git a/packages/store/src/goldenStories/autoPart/noConflict.insert.test.ts b/packages/store-graph/src/goldenStories/autoPart/noConflict.insert.test.ts similarity index 84% rename from packages/store/src/goldenStories/autoPart/noConflict.insert.test.ts rename to packages/store-graph/src/goldenStories/autoPart/noConflict.insert.test.ts index f040c69054..2d0175372e 100644 --- a/packages/store/src/goldenStories/autoPart/noConflict.insert.test.ts +++ b/packages/store-graph/src/goldenStories/autoPart/noConflict.insert.test.ts @@ -3,13 +3,13 @@ import { expect } from 'expect'; import assert from 'node:assert'; import * as NodeTest from 'node:test'; import { type BlankNodeIdentifier, type NodeReference } from '../../index.ts'; -import createStoreAndController from '../createStoreAndController.ts'; +import createGraphAndController from '../createGraphAndController.ts'; scenario( 'Both "hasPart" and "isPartOf" does not need to be fixed on insert', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .when('both parent and children are inserted', ({ controller }) => { controller.edit(transaction => { transaction.upsert('_:m-00001', () => ({ @@ -34,8 +34,8 @@ scenario( })); }); }) - .then('the parent node should have `hasPart`', ({ store }) => { - const parent = store.queryByIdentifier('_:conversation')!; + .then('the parent node should have `hasPart`', ({ graph }) => { + const parent = graph.queryByIdentifier('_:conversation')!; expect(parent).toStrictEqual({ '@id': '_:conversation', @@ -48,8 +48,8 @@ scenario( { '@id': '_:m-00002' satisfies BlankNodeIdentifier } satisfies NodeReference ]); }) - .and('the first child node should have `isPartOf`', ({ store }) => { - const firstChild = store.queryByIdentifier('_:m-00001')!; + .and('the first child node should have `isPartOf`', ({ graph }) => { + const firstChild = graph.queryByIdentifier('_:m-00001')!; expect(firstChild).toStrictEqual({ '@id': '_:m-00001', @@ -61,8 +61,8 @@ scenario( { '@id': '_:conversation' satisfies BlankNodeIdentifier } satisfies NodeReference ]); }) - .and('the second child node should have `isPartOf`', ({ store }) => { - const secondChild = store.queryByIdentifier('_:m-00002')!; + .and('the second child node should have `isPartOf`', ({ graph }) => { + const secondChild = graph.queryByIdentifier('_:m-00002')!; expect(secondChild).toStrictEqual({ '@id': '_:m-00002', diff --git a/packages/store/src/goldenStories/autoPart/noConflict.update.test.ts b/packages/store-graph/src/goldenStories/autoPart/noConflict.update.test.ts similarity index 85% rename from packages/store/src/goldenStories/autoPart/noConflict.update.test.ts rename to packages/store-graph/src/goldenStories/autoPart/noConflict.update.test.ts index 69d92e9b77..94bbe36232 100644 --- a/packages/store/src/goldenStories/autoPart/noConflict.update.test.ts +++ b/packages/store-graph/src/goldenStories/autoPart/noConflict.update.test.ts @@ -3,13 +3,13 @@ import { expect } from 'expect'; import assert from 'node:assert'; import * as NodeTest from 'node:test'; import { type BlankNodeIdentifier, type NodeReference } from '../../index.ts'; -import createStoreAndController from '../createStoreAndController.ts'; +import createGraphAndController from '../createGraphAndController.ts'; scenario( 'Both "hasPart" and "isPartOf" does not need to be fixed on update', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .when('both parent and children are updated', ({ controller }) => { controller.edit(transaction => { transaction.upsert('_:conversation', () => ({ '@id': '_:conversation', '@type': new Set(['Conversation']) })); @@ -31,8 +31,8 @@ scenario( })); }); }) - .then('the parent node should have `hasPart`', ({ store }) => { - const parent = store.queryByIdentifier('_:conversation')!; + .then('the parent node should have `hasPart`', ({ graph }) => { + const parent = graph.queryByIdentifier('_:conversation')!; expect(parent).toStrictEqual({ '@id': '_:conversation', @@ -45,8 +45,8 @@ scenario( { '@id': '_:m-00002' satisfies BlankNodeIdentifier } satisfies NodeReference ]); }) - .and('the first child node should have `isPartOf`', ({ store }) => { - const firstChild = store.queryByIdentifier('_:m-00001')!; + .and('the first child node should have `isPartOf`', ({ graph }) => { + const firstChild = graph.queryByIdentifier('_:m-00001')!; expect(firstChild).toStrictEqual({ '@id': '_:m-00001', @@ -58,8 +58,8 @@ scenario( { '@id': '_:conversation' satisfies BlankNodeIdentifier } satisfies NodeReference ]); }) - .and('the second child node should have `isPartOf`', ({ store }) => { - const secondChild = store.queryByIdentifier('_:m-00002')!; + .and('the second child node should have `isPartOf`', ({ graph }) => { + const secondChild = graph.queryByIdentifier('_:m-00002')!; expect(secondChild).toStrictEqual({ '@id': '_:m-00002', diff --git a/packages/store/src/goldenStories/buildPermutatedFunction.ts b/packages/store-graph/src/goldenStories/buildPermutatedFunction.ts similarity index 100% rename from packages/store/src/goldenStories/buildPermutatedFunction.ts rename to packages/store-graph/src/goldenStories/buildPermutatedFunction.ts diff --git a/packages/store-graph/src/goldenStories/createGraphAndController.ts b/packages/store-graph/src/goldenStories/createGraphAndController.ts new file mode 100644 index 0000000000..2151622bc7 --- /dev/null +++ b/packages/store-graph/src/goldenStories/createGraphAndController.ts @@ -0,0 +1,26 @@ +import assert from 'node:assert'; +import { mock, type Mock } from 'node:test'; +import { Graph, type GraphController, type GraphListener } from '../index.ts'; + +function createGraphAndController(): { + readonly controller: GraphController; + readonly graph: Graph; + readonly onUpdate: Mock; +} { + let controller: GraphController | undefined; + const graph = new Graph({ + start(c) { + controller = c; + } + }); + + assert(controller); + + const onUpdate = mock.fn(); + + graph.subscribe(onUpdate); + + return Object.freeze({ controller: controller as GraphController, onUpdate, graph }); +} + +export default createGraphAndController; diff --git a/packages/store/src/goldenStories/deleteNode.reAdd.test.ts b/packages/store-graph/src/goldenStories/deleteNode.reAdd.test.ts similarity index 84% rename from packages/store/src/goldenStories/deleteNode.reAdd.test.ts rename to packages/store-graph/src/goldenStories/deleteNode.reAdd.test.ts index 7087e6a35e..54fb440ae9 100644 --- a/packages/store/src/goldenStories/deleteNode.reAdd.test.ts +++ b/packages/store-graph/src/goldenStories/deleteNode.reAdd.test.ts @@ -2,13 +2,13 @@ import { scenario } from '@testduet/given-when-then'; import { expect } from 'expect'; import assert from 'node:assert'; import * as NodeTest from 'node:test'; -import createStoreAndController from './createStoreAndController.ts'; +import createGraphAndController from './createGraphAndController.ts'; scenario( 'Deleting a node', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .and('a parent node and a child node', precondition => { precondition.controller.edit(transaction => { transaction.upsert('_:conversation', () => ({ @@ -39,8 +39,8 @@ scenario( })); }); }) - .then('should have the new child node', ({ store }) => { - const child = store.queryByIdentifier('_:m-00001'); + .then('should have the new child node', ({ graph }) => { + const child = graph.queryByIdentifier('_:m-00001'); expect(child).toStrictEqual({ '@id': '_:m-00001', @@ -51,9 +51,9 @@ scenario( // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain assert.deepStrictEqual(Array.from(child?.['@type']!), ['CreativeWork']); }) - .and('should be disconnected from parent', ({ store }) => { + .and('should be disconnected from parent', ({ graph }) => { // The newly added node does not have "isPartOf" property. - const parent = store.queryByIdentifier('_:conversation'); + const parent = graph.queryByIdentifier('_:conversation'); expect(parent).toStrictEqual({ '@id': '_:conversation', diff --git a/packages/store/src/goldenStories/deleteNode.test.ts b/packages/store-graph/src/goldenStories/deleteNode.test.ts similarity index 84% rename from packages/store/src/goldenStories/deleteNode.test.ts rename to packages/store-graph/src/goldenStories/deleteNode.test.ts index 639d867c84..09f7aceb60 100644 --- a/packages/store/src/goldenStories/deleteNode.test.ts +++ b/packages/store-graph/src/goldenStories/deleteNode.test.ts @@ -1,13 +1,13 @@ import { scenario } from '@testduet/given-when-then'; import assert from 'node:assert'; import * as NodeTest from 'node:test'; -import createStoreAndController from './createStoreAndController.ts'; +import createGraphAndController from './createGraphAndController.ts'; scenario( 'Deleting a node', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .and('a node', precondition => { precondition.controller.edit(transaction => { transaction.upsert('_:m-00001', () => ({ @@ -23,8 +23,8 @@ scenario( transaction.delete('_:m-00001'); }); }) - .then('should have no nodes', ({ store }) => { - assert.deepStrictEqual(Array.from(store.getSnapshot()), []); + .then('should have no nodes', ({ graph }) => { + assert.deepStrictEqual(Array.from(graph.getSnapshot()), []); }) .and('should call listener twice (insert and delete)', ({ onUpdate }) => { assert.equal(onUpdate.mock.callCount(), 2); diff --git a/packages/store/src/goldenStories/idMustNotChange.test.ts b/packages/store-graph/src/goldenStories/idMustNotChange.test.ts similarity index 91% rename from packages/store/src/goldenStories/idMustNotChange.test.ts rename to packages/store-graph/src/goldenStories/idMustNotChange.test.ts index 0f1cba7026..90d0b2da23 100644 --- a/packages/store/src/goldenStories/idMustNotChange.test.ts +++ b/packages/store-graph/src/goldenStories/idMustNotChange.test.ts @@ -1,13 +1,13 @@ import { scenario } from '@testduet/given-when-then'; import assert from 'node:assert'; import * as NodeTest from 'node:test'; -import createStoreAndController from './createStoreAndController.ts'; +import createGraphAndController from './createGraphAndController.ts'; scenario( '"@id" must not change', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .and('a parent node and a child node', precondition => { precondition.controller.edit(transaction => { transaction.upsert('_:conversation', () => ({ diff --git a/packages/store/src/goldenStories/sortByDateCreated/delete.test.ts b/packages/store-graph/src/goldenStories/sortByDateCreated/delete.test.ts similarity index 80% rename from packages/store/src/goldenStories/sortByDateCreated/delete.test.ts rename to packages/store-graph/src/goldenStories/sortByDateCreated/delete.test.ts index 9486863766..2c512ce4dc 100644 --- a/packages/store/src/goldenStories/sortByDateCreated/delete.test.ts +++ b/packages/store-graph/src/goldenStories/sortByDateCreated/delete.test.ts @@ -1,13 +1,13 @@ import { scenario } from '@testduet/given-when-then'; -import * as NodeTest from 'node:test'; -import createStoreAndController from '../createStoreAndController.ts'; import assert from 'node:assert'; +import * as NodeTest from 'node:test'; +import createGraphAndController from '../createGraphAndController.ts'; scenario( 'Updated nodes are sorted by "dateCreated" property', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .and('two children nodes are upserted under the same parent', precondition => { precondition.controller.edit(transaction => { transaction.upsert('_:conversation', () => ({ @@ -43,15 +43,15 @@ scenario( return precondition; }) - .when('query the children nodes with sort', ({ store }) => - Array.from(store.getSortedParts('_:conversation', { by: 'dateCreated' })) + .when('query the children nodes with sort', ({ graph }) => + Array.from(graph.getSortedParts('_:conversation', { by: 'dateCreated' })) ) - .then('should have first child removed from the sort result', ({ store }, sorted) => { + .then('should have first child removed from the sort result', ({ graph }, sorted) => { assert.equal(sorted[0]?.['@id'], '_:m-00002'); - assert.deepStrictEqual(sorted, [store.queryByIdentifier('_:m-00002')]); + assert.deepStrictEqual(sorted, [graph.queryByIdentifier('_:m-00002')]); }) - .and('should have first child removed from the "hasPart" property', ({ store }) => { - assert.deepStrictEqual(Array.from(store.queryByIdentifier('_:conversation')!.hasPart!), [ + .and('should have first child removed from the "hasPart" property', ({ graph }) => { + assert.deepStrictEqual(Array.from(graph.queryByIdentifier('_:conversation')!.hasPart!), [ { '@id': '_:m-00002' } ]); }); diff --git a/packages/store/src/goldenStories/sortByDateCreated/insert.test.ts b/packages/store-graph/src/goldenStories/sortByDateCreated/insert.test.ts similarity index 76% rename from packages/store/src/goldenStories/sortByDateCreated/insert.test.ts rename to packages/store-graph/src/goldenStories/sortByDateCreated/insert.test.ts index 02f01e28a5..88ec22d5c2 100644 --- a/packages/store/src/goldenStories/sortByDateCreated/insert.test.ts +++ b/packages/store-graph/src/goldenStories/sortByDateCreated/insert.test.ts @@ -1,13 +1,13 @@ import { scenario } from '@testduet/given-when-then'; import assert from 'node:assert'; import * as NodeTest from 'node:test'; -import createStoreAndController from '../createStoreAndController.ts'; +import createGraphAndController from '../createGraphAndController.ts'; scenario( 'Inserted nodes are sorted by "dateCreated" property', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .and('two children nodes are upserted under the same parent', precondition => { precondition.controller.edit(transaction => { transaction.upsert('_:conversation', () => ({ @@ -36,14 +36,14 @@ scenario( return precondition; }) - .when('query the children nodes with sort', ({ store }) => - Array.from(store.getSortedParts('_:conversation', { by: 'dateCreated' })) + .when('query the children nodes with sort', ({ graph }) => + Array.from(graph.getSortedParts('_:conversation', { by: 'dateCreated' })) ) - .then('should be in correct order', ({ store }, sorted) => { + .then('should be in correct order', ({ graph }, sorted) => { assert.equal(sorted[0]?.['@id'], '_:m-00001'); assert.equal(sorted[1]?.['@id'], '_:m-00002'); - assert.deepStrictEqual(sorted, [store.queryByIdentifier('_:m-00001'), store.queryByIdentifier('_:m-00002')]); + assert.deepStrictEqual(sorted, [graph.queryByIdentifier('_:m-00001'), graph.queryByIdentifier('_:m-00002')]); }); }, NodeTest diff --git a/packages/store/src/goldenStories/sortByDateCreated/update.test.ts b/packages/store-graph/src/goldenStories/sortByDateCreated/update.test.ts similarity index 78% rename from packages/store/src/goldenStories/sortByDateCreated/update.test.ts rename to packages/store-graph/src/goldenStories/sortByDateCreated/update.test.ts index fcd000f46f..df0da75ff3 100644 --- a/packages/store/src/goldenStories/sortByDateCreated/update.test.ts +++ b/packages/store-graph/src/goldenStories/sortByDateCreated/update.test.ts @@ -1,13 +1,13 @@ import { scenario } from '@testduet/given-when-then'; import assert from 'node:assert'; import * as NodeTest from 'node:test'; -import createStoreAndController from '../createStoreAndController.ts'; +import createGraphAndController from '../createGraphAndController.ts'; scenario( 'Updated nodes are sorted by "dateCreated" property', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .and('two children nodes are upserted under the same parent', precondition => { precondition.controller.edit(transaction => { transaction.upsert('_:conversation', () => ({ @@ -43,17 +43,17 @@ scenario( return precondition; }) - .when('query the children nodes with sort', ({ store }) => - Array.from(store.getSortedParts('_:conversation', { by: 'dateCreated' })) + .when('query the children nodes with sort', ({ graph }) => + Array.from(graph.getSortedParts('_:conversation', { by: 'dateCreated' })) ) - .then('should be in correct order', ({ store }, sorted) => { + .then('should be in correct order', ({ graph }, sorted) => { assert.equal(sorted[0]?.['@id'], '_:m-00002'); assert.equal(sorted[1]?.['@id'], '_:m-00001'); - assert.deepStrictEqual(sorted, [store.queryByIdentifier('_:m-00002'), store.queryByIdentifier('_:m-00001')]); + assert.deepStrictEqual(sorted, [graph.queryByIdentifier('_:m-00002'), graph.queryByIdentifier('_:m-00001')]); }) - .and('should not affect the order in "hasPart" property', ({ store }) => { - assert.deepStrictEqual(Array.from(store.queryByIdentifier('_:conversation')!.hasPart!), [ + .and('should not affect the order in "hasPart" property', ({ graph }) => { + assert.deepStrictEqual(Array.from(graph.queryByIdentifier('_:conversation')!.hasPart!), [ { '@id': '_:m-00001' }, { '@id': '_:m-00002' } ]); diff --git a/packages/store/src/goldenStories/throwOnNonArrayCustomProperties.ts b/packages/store-graph/src/goldenStories/throwOnNonArrayCustomProperties.ts similarity index 87% rename from packages/store/src/goldenStories/throwOnNonArrayCustomProperties.ts rename to packages/store-graph/src/goldenStories/throwOnNonArrayCustomProperties.ts index 3377ce78d6..d7c3766122 100644 --- a/packages/store/src/goldenStories/throwOnNonArrayCustomProperties.ts +++ b/packages/store-graph/src/goldenStories/throwOnNonArrayCustomProperties.ts @@ -1,13 +1,13 @@ import { scenario } from '@testduet/given-when-then'; import assert from 'node:assert'; import * as NodeTest from 'node:test'; -import createStoreAndController from './createStoreAndController.ts'; +import createGraphAndController from './createGraphAndController.ts'; scenario( 'Throw when some custom properties are not array', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .when('a node with property value of non-array is upserted', ({ controller }) => { try { controller.edit(transaction => { diff --git a/packages/store/src/goldenStories/transactional/failingUpsertShouldNotBeCommitted.test.ts b/packages/store-graph/src/goldenStories/transactional/failingUpsertShouldNotBeCommitted.test.ts similarity index 80% rename from packages/store/src/goldenStories/transactional/failingUpsertShouldNotBeCommitted.test.ts rename to packages/store-graph/src/goldenStories/transactional/failingUpsertShouldNotBeCommitted.test.ts index 48cc879669..ad61be5302 100644 --- a/packages/store/src/goldenStories/transactional/failingUpsertShouldNotBeCommitted.test.ts +++ b/packages/store-graph/src/goldenStories/transactional/failingUpsertShouldNotBeCommitted.test.ts @@ -2,13 +2,13 @@ import { scenario } from '@testduet/given-when-then'; import { expect } from 'expect'; import assert from 'node:assert'; import * as NodeTest from 'node:test'; -import createStoreAndController from '../createStoreAndController.ts'; +import createGraphAndController from '../createGraphAndController.ts'; scenario( 'Failing upsert should not be committed', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .when('a node is upserted properly in a single transaction', ({ controller }) => { controller.edit(transaction => { transaction.upsert('_:n-00001', () => ({ @@ -18,8 +18,8 @@ scenario( })); }); }) - .then('should be in the system', ({ store }) => { - assert(store.queryByIdentifier('_:n-00001')); + .then('should be in the system', ({ graph }) => { + assert(graph.queryByIdentifier('_:n-00001')); }) .when('the node is upserted with invalid content', ({ controller }) => { try { @@ -50,8 +50,8 @@ scenario( assert(error instanceof Error); assert((error['message'] as string).includes('must be array of')); }) - .and('should keep the original node', ({ store }) => { - const node = store.queryByIdentifier('_:n-00001')!; + .and('should keep the original node', ({ graph }) => { + const node = graph.queryByIdentifier('_:n-00001')!; expect(node).toStrictEqual({ '@id': '_:n-00001', @@ -61,8 +61,8 @@ scenario( assert.deepStrictEqual(Array.from(node['@type']), ['Message']); }) - .and('should not keep other node in the same transaction', ({ store }) => { - assert(!store.queryByIdentifier('_:n-00002')); + .and('should not keep other node in the same transaction', ({ graph }) => { + assert(!graph.queryByIdentifier('_:n-00002')); }); }, NodeTest diff --git a/packages/store/src/goldenStories/updateIsReplace.test.ts b/packages/store-graph/src/goldenStories/updateIsReplace.test.ts similarity index 82% rename from packages/store/src/goldenStories/updateIsReplace.test.ts rename to packages/store-graph/src/goldenStories/updateIsReplace.test.ts index 3eeeda4e7e..7f80e8f1c5 100644 --- a/packages/store/src/goldenStories/updateIsReplace.test.ts +++ b/packages/store-graph/src/goldenStories/updateIsReplace.test.ts @@ -2,13 +2,13 @@ import { scenario } from '@testduet/given-when-then'; import { expect } from 'expect'; import assert from 'node:assert'; import * as NodeTest from 'node:test'; -import createStoreAndController from './createStoreAndController.ts'; +import createGraphAndController from './createGraphAndController.ts'; scenario( 'Update a node will replace its content', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .when('a node is inserted', ({ controller }) => { controller.edit(transaction => { transaction.upsert('_:m-00001', () => ({ @@ -18,8 +18,8 @@ scenario( })); }); }) - .then('should be able to query the inserted node', ({ store }) => { - const node = store.queryByIdentifier('_:m-00001'); + .then('should be able to query the inserted node', ({ graph }) => { + const node = graph.queryByIdentifier('_:m-00001'); expect(node).toStrictEqual({ '@id': '_:m-00001', @@ -54,8 +54,8 @@ scenario( }); }); }) - .then('should have replaced the node', ({ store }) => { - const node = store.queryByIdentifier('_:m-00001')!; + .then('should have replaced the node', ({ graph }) => { + const node = graph.queryByIdentifier('_:m-00001')!; expect(node).toStrictEqual({ '@id': '_:m-00001', @@ -65,8 +65,8 @@ scenario( // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain assert.deepStrictEqual(Array.from(node?.['@type']!), ['CreativeWork']); }) - .and('must not keep previous properties', ({ store }) => { - assert(!('text' in store.queryByIdentifier('_:m-00001')!)); + .and('must not keep previous properties', ({ graph }) => { + assert(!('text' in graph.queryByIdentifier('_:m-00001')!)); }) .and('should have called the listener', ({ onUpdate }) => { assert.equal(onUpdate.mock.callCount(), 1); diff --git a/packages/store/src/goldenStories/upsertedNodeIsDifferentAndFrozen.test.ts b/packages/store-graph/src/goldenStories/upsertedNodeIsDifferentAndFrozen.test.ts similarity index 85% rename from packages/store/src/goldenStories/upsertedNodeIsDifferentAndFrozen.test.ts rename to packages/store-graph/src/goldenStories/upsertedNodeIsDifferentAndFrozen.test.ts index b3633b282e..25e65a439a 100644 --- a/packages/store/src/goldenStories/upsertedNodeIsDifferentAndFrozen.test.ts +++ b/packages/store-graph/src/goldenStories/upsertedNodeIsDifferentAndFrozen.test.ts @@ -4,13 +4,13 @@ import assert from 'node:assert'; import * as NodeTest from 'node:test'; import type { Node } from '../index.ts'; import type { NodeIdentifier } from '../node/NodeIdentifier.ts'; -import createStoreAndController from './createStoreAndController.ts'; +import createGraphAndController from './createGraphAndController.ts'; scenario( 'Upserted node is different and frozen', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .when('a node is upserted', ({ controller }) => { const upserted: Node<{ text: string[] }> = { '@id': '_:n-1' satisfies NodeIdentifier, @@ -24,15 +24,15 @@ scenario( return { upserted }; }) - .then('should able to query the upserted node', ({ store }) => { - assert(store.queryByIdentifier('_:n-1')); + .then('should able to query the upserted node', ({ graph }) => { + assert(graph.queryByIdentifier('_:n-1')); }) .and('should have called the listener', ({ onUpdate }) => { assert.equal(onUpdate.mock.callCount(), 1); }) - .when('the node is queried', ({ store }, outcome) => ({ + .when('the node is queried', ({ graph }, outcome) => ({ ...outcome, - queried: store.queryByIdentifier('_:n-1')! + queried: graph.queryByIdentifier('_:n-1')! })) .then('should have all properties', (_, { queried }) => { expect(queried).toStrictEqual({ diff --git a/packages/store/src/goldenStories/validNodeIdentifier.test.ts b/packages/store-graph/src/goldenStories/validNodeIdentifier.test.ts similarity index 70% rename from packages/store/src/goldenStories/validNodeIdentifier.test.ts rename to packages/store-graph/src/goldenStories/validNodeIdentifier.test.ts index 8506cae7d0..eb57a289f7 100644 --- a/packages/store/src/goldenStories/validNodeIdentifier.test.ts +++ b/packages/store-graph/src/goldenStories/validNodeIdentifier.test.ts @@ -2,13 +2,13 @@ import { scenario } from '@testduet/given-when-then'; import assert from 'node:assert'; import * as NodeTest from 'node:test'; import { type BlankNodeIdentifier, type NodeIdentifier } from '../index.ts'; -import createStoreAndController from './createStoreAndController.ts'; +import createGraphAndController from './createGraphAndController.ts'; scenario( 'Child node is automatically referenced by parent node', bdd => { bdd - .given('a store and a controller', () => createStoreAndController()) + .given('a graph and a controller', () => createGraphAndController()) .when('3 nodes with 3 different supported type of node identifier', ({ controller }) => { controller.edit(transaction => { transaction.upsert('_:node-identifier', () => ({ @@ -27,14 +27,14 @@ scenario( })); }); }) - .then('should query node with blank node identifier', ({ store }) => { - assert(store.queryByIdentifier('_:node-identifier')); + .then('should query node with blank node identifier', ({ graph }) => { + assert(graph.queryByIdentifier('_:node-identifier')); }) - .and('should query node with URN node identifier', ({ store }) => { - assert(store.queryByIdentifier('urn:node-identifier')); + .and('should query node with URN node identifier', ({ graph }) => { + assert(graph.queryByIdentifier('urn:node-identifier')); }) - .and('should query node with https:// node identifier', ({ store }) => { - assert(store.queryByIdentifier('https://node-identifier')); + .and('should query node with https:// node identifier', ({ graph }) => { + assert(graph.queryByIdentifier('https://node-identifier')); }); }, NodeTest diff --git a/packages/store/src/index.test.ts b/packages/store-graph/src/index.test.ts similarity index 100% rename from packages/store/src/index.test.ts rename to packages/store-graph/src/index.test.ts diff --git a/packages/store-graph/src/index.ts b/packages/store-graph/src/index.ts new file mode 100644 index 0000000000..2a24a96f3e --- /dev/null +++ b/packages/store-graph/src/index.ts @@ -0,0 +1,11 @@ +export { type BlankNodeIdentifier } from './node/BlankNodeIdentifier.ts'; +export { type Node, type VettedNode } from './node/Node.ts'; +export { type NodeIdentifier } from './node/NodeIdentifier.ts'; +export { type NodeReference } from './node/NodeReference.ts'; +export { + default as Graph, + type GetSortedPartsInit, + type GraphController, + type GraphListener, + type GraphSource +} from './Graph.ts'; diff --git a/packages/store/src/node/BlankNodeIdentifier.ts b/packages/store-graph/src/node/BlankNodeIdentifier.ts similarity index 100% rename from packages/store/src/node/BlankNodeIdentifier.ts rename to packages/store-graph/src/node/BlankNodeIdentifier.ts diff --git a/packages/store/src/node/JSONLiteral.ts b/packages/store-graph/src/node/JSONLiteral.ts similarity index 100% rename from packages/store/src/node/JSONLiteral.ts rename to packages/store-graph/src/node/JSONLiteral.ts diff --git a/packages/store/src/node/JSONValue.ts b/packages/store-graph/src/node/JSONValue.ts similarity index 100% rename from packages/store/src/node/JSONValue.ts rename to packages/store-graph/src/node/JSONValue.ts diff --git a/packages/store/src/node/JSONValueSchema.test.ts b/packages/store-graph/src/node/JSONValueSchema.test.ts similarity index 100% rename from packages/store/src/node/JSONValueSchema.test.ts rename to packages/store-graph/src/node/JSONValueSchema.test.ts diff --git a/packages/store/src/node/Node.ts b/packages/store-graph/src/node/Node.ts similarity index 100% rename from packages/store/src/node/Node.ts rename to packages/store-graph/src/node/Node.ts diff --git a/packages/store/src/node/NodeIdentifier.test.ts b/packages/store-graph/src/node/NodeIdentifier.test.ts similarity index 100% rename from packages/store/src/node/NodeIdentifier.test.ts rename to packages/store-graph/src/node/NodeIdentifier.test.ts diff --git a/packages/store/src/node/NodeIdentifier.ts b/packages/store-graph/src/node/NodeIdentifier.ts similarity index 100% rename from packages/store/src/node/NodeIdentifier.ts rename to packages/store-graph/src/node/NodeIdentifier.ts diff --git a/packages/store/src/node/NodeReference.test.ts b/packages/store-graph/src/node/NodeReference.test.ts similarity index 100% rename from packages/store/src/node/NodeReference.test.ts rename to packages/store-graph/src/node/NodeReference.test.ts diff --git a/packages/store/src/node/NodeReference.ts b/packages/store-graph/src/node/NodeReference.ts similarity index 100% rename from packages/store/src/node/NodeReference.ts rename to packages/store-graph/src/node/NodeReference.ts diff --git a/packages/store/src/node/PropertyValue.test.ts b/packages/store-graph/src/node/PropertyValue.test.ts similarity index 100% rename from packages/store/src/node/PropertyValue.test.ts rename to packages/store-graph/src/node/PropertyValue.test.ts diff --git a/packages/store/src/node/PropertyValue.ts b/packages/store-graph/src/node/PropertyValue.ts similarity index 100% rename from packages/store/src/node/PropertyValue.ts rename to packages/store-graph/src/node/PropertyValue.ts diff --git a/packages/store/src/private/Draft.test.ts b/packages/store-graph/src/private/Draft.test.ts similarity index 100% rename from packages/store/src/private/Draft.test.ts rename to packages/store-graph/src/private/Draft.test.ts diff --git a/packages/store/src/private/Draft.ts b/packages/store-graph/src/private/Draft.ts similarity index 99% rename from packages/store/src/private/Draft.ts rename to packages/store-graph/src/private/Draft.ts index 5da03ee825..2d885ce90c 100644 --- a/packages/store/src/private/Draft.ts +++ b/packages/store-graph/src/private/Draft.ts @@ -66,7 +66,7 @@ function buildNode( } /** - * A node being modified in a transaction, i.e. changes made by the store on top of a node (e.g. mirroring `isPartOf`, + * A node being modified in a transaction, i.e. changes made by the graph on top of a node (e.g. mirroring `isPartOf`, * removing references to a deleted node). * * - `hasPart` and `isPartOf` of the node are ignored, `hasPart` is generated from the child IDs passed in, and diff --git a/packages/store/src/private/DraftNode.ts b/packages/store-graph/src/private/DraftNode.ts similarity index 100% rename from packages/store/src/private/DraftNode.ts rename to packages/store-graph/src/private/DraftNode.ts diff --git a/packages/store/src/private/FrozenNode.ts b/packages/store-graph/src/private/FrozenNode.ts similarity index 100% rename from packages/store/src/private/FrozenNode.ts rename to packages/store-graph/src/private/FrozenNode.ts diff --git a/packages/store/src/private/createDraftNode.test.ts b/packages/store-graph/src/private/createDraftNode.test.ts similarity index 100% rename from packages/store/src/private/createDraftNode.test.ts rename to packages/store-graph/src/private/createDraftNode.test.ts diff --git a/packages/store/src/private/createDraftNode.ts b/packages/store-graph/src/private/createDraftNode.ts similarity index 98% rename from packages/store/src/private/createDraftNode.ts rename to packages/store-graph/src/private/createDraftNode.ts index c6550c2d2f..f00b30bdc1 100644 --- a/packages/store/src/private/createDraftNode.ts +++ b/packages/store-graph/src/private/createDraftNode.ts @@ -6,7 +6,7 @@ import deepFreeze from './deepFreeze.ts'; import type { DraftNode } from './DraftNode.ts'; import isDangerousKey from './isDangerousKey.ts'; -// Passed to validation in place of a `FrozenNodeSet`, it is always created by the store from validated IDs. +// Passed to validation in place of a `FrozenNodeSet`, it is always created by the graph from validated IDs. const EMPTY_SET: ReadonlySet = Object.freeze(new Set()); /** @@ -88,7 +88,7 @@ function toFrozenNodeSet(originalValue: unknown, value: ReadonlySet( * is deleted earlier, only when preserving order), or when there are more tombstones than live entries (amortized to * O(1) per deletion) * - * Why undo log: a read-only version must never change after it is handed out, e.g. `Store.getSnapshot()` returns it + * Why undo log: a read-only version must never change after it is handed out, e.g. `Graph.getSnapshot()` returns it * directly, and callers (such as `useSyncExternalStore()` or diffing previous/next snapshots) could still read it after * the next commit. When committing, the older version must keep its content, the options are: * diff --git a/packages/store/src/til.test.ts b/packages/store-graph/src/til.test.ts similarity index 100% rename from packages/store/src/til.test.ts rename to packages/store-graph/src/til.test.ts diff --git a/packages/store-graph/src/tsconfig.json b/packages/store-graph/src/tsconfig.json new file mode 100644 index 0000000000..a4027ab6d3 --- /dev/null +++ b/packages/store-graph/src/tsconfig.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "allowImportingTsExtensions": true + }, + "extends": "@msinternal/botframework-webchat-tsconfig/current" +} diff --git a/packages/store-graph/tsdown.config.mts b/packages/store-graph/tsdown.config.mts new file mode 100644 index 0000000000..727f7984cd --- /dev/null +++ b/packages/store-graph/tsdown.config.mts @@ -0,0 +1,5 @@ +import { defineConfig } from 'tsdown'; + +import { createDualFormatConfigs } from '../../tsdown.base.config.mts'; + +export default defineConfig(createDualFormatConfigs({ 'botframework-webchat-store-graph': './src/index.ts' })); diff --git a/packages/store/package.json b/packages/store/package.json index cbf0ede67e..9d3cfcb057 100644 --- a/packages/store/package.json +++ b/packages/store/package.json @@ -17,10 +17,12 @@ } }, "scripts": { - "benchmark": "node --disable-warning=MODULE_TYPELESS_PACKAGE_JSON --expose-gc ./src/benchmark.ts", - "benchmark:json": "node --disable-warning=MODULE_TYPELESS_PACKAGE_JSON ./src/benchmark.json.ts", - "benchmark:node": "node --disable-warning=MODULE_TYPELESS_PACKAGE_JSON ./src/benchmark.node.ts", - "build": "npm run build:run", + "build": "npm run --if-present build:pre && npm run build:run && npm run --if-present build:post", + "build:post": "npm run build:post:dtsroll && npm run build:post:validate:dts", + "build:post:dtsroll": "dtsroll ./dist/*.d.* && sed -E -i 's/^([[:space:]]*export[[:space:]]+)type[[:space:]]+(\\{[[:space:]]+type[[:space:]]+)/\\1\\2/' ./dist/*.d.ts", + "build:post:validate:dts": "vg ast-check dist-types ./dist/*.d.*", + "build:pre": "npm run build:pre:local-dependencies", + "build:pre:local-dependencies": "../../scripts/npm/build-local-dependencies.sh", "build:run": "tsdown", "precommit": "npm run precommit:eslint -- src && npm run precommit:typecheck", "precommit:eslint": "../../node_modules/.bin/eslint --report-unused-disable-directives --max-warnings 0", @@ -28,11 +30,14 @@ "test": "npm run test:node", "test:node": "node --test" }, - "dependencies": { - "valibot": "1.2.0" + "localDependencies": { + "@msinternal/botframework-webchat-store-graph": "development" }, "devDependencies": { - "@types/node": "^25.9.1", - "type-fest": "^5.6.0" + "@msinternal/botframework-webchat-store-graph": "0.0.0-0", + "@types/node": "^25.9.1" + }, + "dependencies": { + "valibot": "1.2.0" } } diff --git a/packages/store/src/goldenStories/createStoreAndController.ts b/packages/store/src/goldenStories/createStoreAndController.ts deleted file mode 100644 index b3c003001f..0000000000 --- a/packages/store/src/goldenStories/createStoreAndController.ts +++ /dev/null @@ -1,26 +0,0 @@ -import assert from 'node:assert'; -import { mock, type Mock } from 'node:test'; -import { Store, type StoreController, type StoreListener } from '../index.ts'; - -function createStoreAndController(): { - readonly controller: StoreController; - readonly onUpdate: Mock; - readonly store: Store; -} { - let controller: StoreController | undefined; - const store = new Store({ - start(c) { - controller = c; - } - }); - - assert(controller); - - const onUpdate = mock.fn(); - - store.subscribe(onUpdate); - - return Object.freeze({ controller: controller as StoreController, onUpdate, store }); -} - -export default createStoreAndController; diff --git a/packages/store/src/index.ts b/packages/store/src/index.ts index 21b350895f..cb0ff5c3b5 100644 --- a/packages/store/src/index.ts +++ b/packages/store/src/index.ts @@ -1,11 +1 @@ -export { type BlankNodeIdentifier } from './node/BlankNodeIdentifier.ts'; -export { type Node, type VettedNode } from './node/Node.ts'; -export { type NodeIdentifier } from './node/NodeIdentifier.ts'; -export { type NodeReference } from './node/NodeReference.ts'; -export { - default as Store, - type GetSortedPartsInit, - type StoreController, - type StoreListener, - type StoreSource -} from './Store.ts'; +export {};