jazz-tools 0.17.5 → 0.17.6

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.
@@ -347,7 +347,6 @@ describe("loadImageBySize", async () => {
347
347
 
348
348
  it("returns the image already loaded", async () => {
349
349
  const account = await setupJazzTestSync({ asyncPeers: true });
350
- const account2 = await createJazzTestAccount();
351
350
 
352
351
  setActiveAccount(account);
353
352
 
@@ -363,9 +362,11 @@ describe("loadImageBySize", async () => {
363
362
  group,
364
363
  );
365
364
 
365
+ const account2 = await createJazzTestAccount();
366
366
  setActiveAccount(account2);
367
367
 
368
- const result = await loadImageBySize(imageDef, 1024, 1024);
368
+ const result = await loadImageBySize(imageDef.id, 1024, 1024);
369
+ expect(result).not.toBeNull();
369
370
  expect(result?.image.id).toBe(imageDef["1024x1024"]!.id);
370
371
  expect(result?.image.isBinaryStreamEnded()).toBe(true);
371
372
  expect(result?.image.asBase64()).toStrictEqual(expect.any(String));
@@ -185,7 +185,11 @@ export async function loadImageBySize(
185
185
  const bestTarget =
186
186
  sortedSizes.find((el) => el.match > 0.95) || sortedSizes.at(-1)!;
187
187
 
188
- const file = image[bestTarget.size[2]];
188
+ // The image's `wxh` keys reference FileStream.
189
+ // image[bestTarget.size[2]] returns undefined if FileStream hasn't loaded yet.
190
+ // Since we only need the file's ID to fetch it later, we check the raw _refs
191
+ // which contain only the linked covalue's ID.
192
+ const file = image._refs[bestTarget.size[2]];
189
193
 
190
194
  if (!file) {
191
195
  return null;
@@ -171,7 +171,9 @@ export function instantiateRefEncodedWithInit<V extends CoValue>(
171
171
  `Cannot automatically create CoValue from value: ${JSON.stringify(init)}. Use the CoValue schema's create() method instead.`,
172
172
  );
173
173
  }
174
- const owner = Group.create();
174
+ const node = parentOwner._raw.core.node;
175
+ const rawGroup = node.createGroup();
176
+ const owner = new Group({ fromRaw: rawGroup });
175
177
  owner.addMember(parentOwner.castAs(Group));
176
178
  // @ts-expect-error - create is a static method in all CoValue classes
177
179
  return schema.ref.create(init, owner);
@@ -154,6 +154,24 @@ export function setActiveAccount(account: Account) {
154
154
  activeAccountContext.set(account);
155
155
  }
156
156
 
157
+ /**
158
+ * Run a callback without an active account.
159
+ *
160
+ * Takes care of restoring the active account after the callback is run.
161
+ *
162
+ * @param callback - The callback to run.
163
+ * @returns The result of the callback.
164
+ */
165
+ export function runWithoutActiveAccount<Result>(
166
+ callback: () => Result,
167
+ ): Result {
168
+ const me = Account.getMe();
169
+ activeAccountContext.set(null);
170
+ const result = callback();
171
+ activeAccountContext.set(me);
172
+ return result;
173
+ }
174
+
157
175
  export async function createJazzTestGuest() {
158
176
  const ctx = await createAnonymousJazzContext({
159
177
  crypto: await PureJSCrypto.create(),
@@ -12,10 +12,15 @@ import {
12
12
  } from "vitest";
13
13
  import { Group, co, subscribeToCoValue, z } from "../exports.js";
14
14
  import { Account } from "../index.js";
15
- import { Loaded, coValueClassFromCoValueClassOrSchema } from "../internal.js";
15
+ import {
16
+ Loaded,
17
+ activeAccountContext,
18
+ coValueClassFromCoValueClassOrSchema,
19
+ } from "../internal.js";
16
20
  import {
17
21
  createJazzTestAccount,
18
22
  getPeerConnectedToTestSyncServer,
23
+ runWithoutActiveAccount,
19
24
  setupJazzTestSync,
20
25
  } from "../testing.js";
21
26
  import { setupTwoNodes, waitFor } from "./utils.js";
@@ -214,6 +219,19 @@ describe("CoMap", async () => {
214
219
  const map = Schema.create({ text: "" });
215
220
  expect(map.text.toString()).toBe("");
216
221
  });
222
+
223
+ it("creates a group for the new CoValue when there is no active account", () => {
224
+ const Schema = co.map({ text: co.plainText() });
225
+
226
+ const parentGroup = Group.create();
227
+ runWithoutActiveAccount(() => {
228
+ const map = Schema.create({ text: "Hello" }, parentGroup);
229
+
230
+ expect(
231
+ map.text._owner.getParentGroups().map((group: Group) => group.id),
232
+ ).toContain(parentGroup.id);
233
+ });
234
+ });
217
235
  });
218
236
 
219
237
  test("CoMap with self reference", () => {