cojson 0.20.2 → 0.20.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -140,7 +140,7 @@ describe("Parent Group Cache", () => {
140
140
  value: "revoked",
141
141
  },
142
142
  ],
143
- "private",
143
+ "trusting",
144
144
  undefined,
145
145
  t2,
146
146
  );
@@ -153,7 +153,7 @@ describe("Parent Group Cache", () => {
153
153
  value: "extend",
154
154
  },
155
155
  ],
156
- "private",
156
+ "trusting",
157
157
  undefined,
158
158
  t1,
159
159
  );
@@ -1,4 +1,4 @@
1
- import { expect, test, vi } from "vitest";
1
+ import { beforeEach, describe, expect, test, vi } from "vitest";
2
2
  import { expectMap } from "../coValue.js";
3
3
  import { ControlledAgent } from "../coValues/account.js";
4
4
  import { WasmCrypto } from "../crypto/WasmCrypto.js";
@@ -13,6 +13,8 @@ import {
13
13
  loadCoValueOrFail,
14
14
  newGroup,
15
15
  newGroupHighLevel,
16
+ setupTestAccount,
17
+ setupTestNode,
16
18
  waitFor,
17
19
  } from "./testUtils.js";
18
20
  import { Role } from "../permissions.js";
@@ -2727,3 +2729,118 @@ test("Can revoke read permission from 'everyone'", async () => {
2727
2729
  // Verify the new account cannot read after revocation
2728
2730
  expect(childContent2.get("foo")).toEqual("bar");
2729
2731
  });
2732
+
2733
+ describe("Private transactions in groups", () => {
2734
+ beforeEach(async () => {
2735
+ setupTestNode({ isSyncServer: true });
2736
+ });
2737
+
2738
+ test("Admins can make private transactions directly in accounts", async () => {
2739
+ const alice = await setupTestAccount();
2740
+
2741
+ const group = alice.node.createGroup();
2742
+ const map = group.createMap();
2743
+
2744
+ // Count valid transactions before attempting private transaction
2745
+ const validTransactionsBefore = alice.account.core.getValidTransactions();
2746
+ const countBefore = validTransactionsBefore.length;
2747
+
2748
+ alice.account.set("root", map.id, "private");
2749
+
2750
+ const validTransactions = alice.account.core.getValidTransactions();
2751
+ expect(validTransactions).toHaveLength(countBefore + 1);
2752
+
2753
+ expect(alice.account.get("root")).toEqual(map.id);
2754
+ expect(alice.account.core.getCurrentReadKey()).not.toBeUndefined();
2755
+ });
2756
+
2757
+ test("Admins cannot make private transactions directly in groups", async () => {
2758
+ const alice = await setupTestAccount();
2759
+
2760
+ const group = alice.node.createGroup();
2761
+ const map = group.createMap();
2762
+
2763
+ // Count valid transactions before attempting private transaction
2764
+ const validTransactionsBefore = group.core.getValidTransactions();
2765
+ const countBefore = validTransactionsBefore.length;
2766
+
2767
+ // Attempt to make a private transaction directly on the group
2768
+ group.set("root", map.id, "private");
2769
+
2770
+ // Verify the transaction is marked invalid (count should not increase)
2771
+ const validTransactions = group.core.getValidTransactions();
2772
+ expect(validTransactions).toHaveLength(countBefore);
2773
+
2774
+ // Verify the change didn't take effect
2775
+ expect(group.get("root")).toBeUndefined();
2776
+ });
2777
+
2778
+ test("Writers cannot make private transactions directly in groups", async () => {
2779
+ const alice = await setupTestAccount({ connected: true });
2780
+ const bob = await setupTestAccount({ connected: true });
2781
+
2782
+ const group = alice.node.createGroup();
2783
+ const map = group.createMap();
2784
+
2785
+ group.addMember(bob.account, "writer");
2786
+
2787
+ // Count valid transactions before attempting private transaction
2788
+ const validTransactionsBefore = group.core.getValidTransactions();
2789
+ const countBefore = validTransactionsBefore.length;
2790
+
2791
+ const groupAsBob = await loadCoValueOrFail(bob.node, group.id);
2792
+
2793
+ // Attempt to make a private transaction directly on the group
2794
+ groupAsBob.set("root", map.id, "private");
2795
+
2796
+ // Verify the transaction is marked invalid (count should not increase)
2797
+ const validTransactions = groupAsBob.core.getValidTransactions();
2798
+ expect(validTransactions).toHaveLength(countBefore);
2799
+
2800
+ // Verify the change didn't take effect
2801
+ expect(groupAsBob.get("root")).toBeUndefined();
2802
+ });
2803
+
2804
+ test("Managers cannot make private transactions directly in groups", async () => {
2805
+ const alice = await setupTestAccount({ connected: true });
2806
+ const bob = await setupTestAccount({ connected: true });
2807
+
2808
+ const group = alice.node.createGroup();
2809
+ const map = group.createMap();
2810
+
2811
+ group.addMember(bob.account, "manager");
2812
+
2813
+ // Count valid transactions before attempting private transaction
2814
+ const validTransactionsBefore = group.core.getValidTransactions();
2815
+ const countBefore = validTransactionsBefore.length;
2816
+
2817
+ const groupAsBob = await loadCoValueOrFail(bob.node, group.id);
2818
+
2819
+ // Attempt to make a private transaction directly on the group
2820
+ groupAsBob.set("root", map.id, "private");
2821
+
2822
+ // Verify the transaction is marked invalid (count should not increase)
2823
+ const validTransactions = groupAsBob.core.getValidTransactions();
2824
+ expect(validTransactions).toHaveLength(countBefore);
2825
+
2826
+ // Verify the change didn't take effect
2827
+ expect(groupAsBob.get("root")).toBeUndefined();
2828
+ });
2829
+
2830
+ test("A Group with a private transaction can be loaded without issues", async () => {
2831
+ const alice = await setupTestAccount({ connected: true });
2832
+ const bob = await setupTestAccount({ connected: true });
2833
+
2834
+ const group = alice.node.createGroup();
2835
+ const map = group.createMap();
2836
+
2837
+ // Attempt to make a private transaction directly on the group
2838
+ group.set("root", map.id, "private");
2839
+ group.addMember(bob.account, "reader");
2840
+
2841
+ const groupAsBob = await loadCoValueOrFail(bob.node, group.id);
2842
+
2843
+ expect(groupAsBob.myRole()).toBe("reader");
2844
+ expect(groupAsBob.core.getCurrentReadKey()).not.toBeUndefined();
2845
+ });
2846
+ });
@@ -92,6 +92,14 @@ class LibSQLSqliteSyncDriver implements SQLiteDatabaseDriver {
92
92
  }
93
93
  }
94
94
 
95
+ function deleteDb(dbPath: string) {
96
+ try {
97
+ unlinkSync(dbPath);
98
+ } catch (error) {
99
+ console.error(error);
100
+ }
101
+ }
102
+
95
103
  export async function createAsyncStorage({
96
104
  filename,
97
105
  nodeName = "client",
@@ -101,12 +109,14 @@ export async function createAsyncStorage({
101
109
  nodeName: string;
102
110
  storageName: string;
103
111
  }) {
112
+ const dbPath = getDbPath(filename);
104
113
  const storage = await getSqliteStorageAsync(
105
- new LibSQLSqliteAsyncDriver(getDbPath(filename)),
114
+ new LibSQLSqliteAsyncDriver(dbPath),
106
115
  );
107
116
 
108
117
  onTestFinished(async () => {
109
118
  await storage.close();
119
+ deleteDb(dbPath);
110
120
  });
111
121
 
112
122
  trackStorageMessages(storage, nodeName, storageName);
@@ -123,10 +133,16 @@ export function createSyncStorage({
123
133
  nodeName: string;
124
134
  storageName: string;
125
135
  }) {
136
+ const dbPath = getDbPath(filename);
126
137
  const storage = getSqliteStorage(
127
138
  new LibSQLSqliteSyncDriver(getDbPath(filename)),
128
139
  );
129
140
 
141
+ onTestFinished(() => {
142
+ storage.close();
143
+ deleteDb(dbPath);
144
+ });
145
+
130
146
  trackStorageMessages(storage, nodeName, storageName);
131
147
 
132
148
  return storage;
@@ -159,17 +175,7 @@ export async function getCoValueStoredSessions(
159
175
  }
160
176
 
161
177
  export function getDbPath(defaultDbPath?: string) {
162
- const dbPath = defaultDbPath ?? join(tmpdir(), `test-${randomUUID()}.db`);
163
-
164
- if (!defaultDbPath) {
165
- onTestFinished(() => {
166
- setTimeout(() => {
167
- unlinkSync(dbPath);
168
- }, 100);
169
- });
170
- }
171
-
172
- return dbPath;
178
+ return defaultDbPath ?? join(tmpdir(), `test-${randomUUID()}.db`);
173
179
  }
174
180
 
175
181
  function trackStorageMessages(