Skip to content

Commit 509687d

Browse files
committed
fix(jazz-tools/inspector): comap's transactions guessed as group's txs
1 parent 87254a2 commit 509687d

File tree

6 files changed

+162
-25
lines changed

6 files changed

+162
-25
lines changed

.changeset/fast-cycles-ask.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"jazz-tools": patch
3+
---
4+
5+
Fix the inspector to guess correctly Group's transactions
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { assert, describe, expect, it } from "vitest";
2+
import { setActiveAccount, setupJazzTestSync } from "jazz-tools/testing";
3+
import { co, z } from "jazz-tools";
4+
import * as TransactionsChanges from "../../utils/transactions-changes";
5+
6+
describe("transactions changes", async () => {
7+
const account = await setupJazzTestSync();
8+
setActiveAccount(account);
9+
10+
describe("ambiguous values in Group's transactions", () => {
11+
it("isGroupExtension should return false for a CoMap", () => {
12+
const value = co.map({ test: z.string() }).create({ test: "extend" })
13+
.$jazz.raw;
14+
15+
const transactions = value.core.verifiedTransactions;
16+
expect(
17+
TransactionsChanges.isGroupExtension(
18+
value,
19+
transactions[0]?.changes?.[0],
20+
),
21+
).toBe(false);
22+
});
23+
24+
it("isGroupExtendRevocation should return false for a CoMap", () => {
25+
const value = co.map({ test: z.string() }).create({ test: "revoked" })
26+
.$jazz.raw;
27+
28+
const transactions = value.core.verifiedTransactions;
29+
expect(
30+
TransactionsChanges.isGroupExtendRevocation(
31+
value,
32+
transactions[0]?.changes?.[0],
33+
),
34+
).toBe(false);
35+
});
36+
37+
it("isGroupPromotion should return false for a CoMap", () => {
38+
const value = co
39+
.map({ parent_co_test: z.string() })
40+
.create({ parent_co_test: "foo" }).$jazz.raw;
41+
42+
const transactions = value.core.verifiedTransactions;
43+
expect(
44+
TransactionsChanges.isGroupPromotion(
45+
value,
46+
transactions[0]?.changes?.[0],
47+
),
48+
).toBe(false);
49+
});
50+
51+
it("isUserPromotion should return false for a CoMap", () => {
52+
const value = co.map({ everyone: z.string() }).create({ everyone: "foo" })
53+
.$jazz.raw;
54+
55+
const transactions = value.core.verifiedTransactions;
56+
expect(
57+
TransactionsChanges.isUserPromotion(
58+
value,
59+
transactions[0]?.changes?.[0],
60+
),
61+
).toBe(false);
62+
});
63+
64+
it("isUserPromotion should return false for a CoMap", () => {
65+
const value = co.map({ everyone: z.string() }).create({ everyone: "foo" })
66+
.$jazz.raw;
67+
68+
const transactions = value.core.verifiedTransactions;
69+
expect(
70+
TransactionsChanges.isUserPromotion(
71+
value,
72+
transactions[0]?.changes?.[0],
73+
),
74+
).toBe(false);
75+
76+
const value2 = co.map({ co_z123: z.string() }).create({ co_z123: "foo" })
77+
.$jazz.raw;
78+
79+
const transactions2 = value2.core.verifiedTransactions;
80+
expect(
81+
TransactionsChanges.isUserPromotion(
82+
value2,
83+
transactions2[0]?.changes?.[0],
84+
),
85+
).toBe(false);
86+
});
87+
88+
it("isKeyRevelation should return false for a CoMap", () => {
89+
const value = co
90+
.map({ "123_for_test": z.string() })
91+
.create({ "123_for_test": "foo" }).$jazz.raw;
92+
93+
const transactions = value.core.verifiedTransactions;
94+
expect(
95+
TransactionsChanges.isKeyRevelation(
96+
value,
97+
transactions[0]?.changes?.[0],
98+
),
99+
).toBe(false);
100+
});
101+
});
102+
});

packages/jazz-tools/src/inspector/ui/icons/add-icon.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ export function AddIcon(props: React.SVGProps<SVGSVGElement>) {
1212
>
1313
<path
1414
d="M4 12H20M12 4V20"
15-
stroke-width="2"
16-
stroke-linecap="round"
17-
stroke-linejoin="round"
15+
strokeWidth="2"
16+
strokeLinecap="round"
17+
strokeLinejoin="round"
1818
/>
1919
</svg>
2020
);

packages/jazz-tools/src/inspector/utils/history.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ export function getTransactionChanges(
6262
const firstChange = tx.changes[0]!;
6363

6464
if (
65-
TransactionChanges.isItemAppend(firstChange) &&
65+
TransactionChanges.isItemAppend(coValue, firstChange) &&
6666
tx.changes.every(
6767
(c) =>
68-
TransactionChanges.isItemAppend(c) &&
68+
TransactionChanges.isItemAppend(coValue, c) &&
6969
areSameOpIds(c.after, firstChange.after),
7070
)
7171
) {
@@ -84,10 +84,10 @@ export function getTransactionChanges(
8484
}
8585

8686
if (
87-
TransactionChanges.isItemPrepend(firstChange) &&
87+
TransactionChanges.isItemPrepend(coValue, firstChange) &&
8888
tx.changes.every(
8989
(c) =>
90-
TransactionChanges.isItemPrepend(c) &&
90+
TransactionChanges.isItemPrepend(coValue, c) &&
9191
areSameOpIds(c.before, firstChange.before),
9292
)
9393
) {
@@ -106,8 +106,8 @@ export function getTransactionChanges(
106106
}
107107

108108
if (
109-
TransactionChanges.isItemDeletion(firstChange) &&
110-
tx.changes.every((c) => TransactionChanges.isItemDeletion(c))
109+
TransactionChanges.isItemDeletion(coValue, firstChange) &&
110+
tx.changes.every((c) => TransactionChanges.isItemDeletion(coValue, c))
111111
) {
112112
const coValueBeforeDeletions = coValue.atTime(tx.madeAt - 1);
113113

packages/jazz-tools/src/inspector/utils/transactions-changes.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,85 +14,115 @@ import type {
1414
import { isCoId } from "../viewer/types";
1515

1616
export const isGroupExtension = (
17+
coValue: RawCoValue,
1718
change: any,
1819
): change is Extract<
1920
MapOpPayload<`child_${string}`, "extend">,
2021
{ op: "set" }
2122
> => {
23+
if (coValue.core.isGroup() === false) return false;
2224
return change?.op === "set" && change?.value === "extend";
2325
};
2426

2527
export const isGroupExtendRevocation = (
28+
coValue: RawCoValue,
2629
change: any,
2730
): change is Extract<
2831
MapOpPayload<`child_${string}`, "revoked">,
2932
{ op: "set" }
3033
> => {
34+
if (coValue.core.isGroup() === false) return false;
3135
return change?.op === "set" && change?.value === "revoked";
3236
};
3337

3438
export const isGroupPromotion = (
39+
coValue: RawCoValue,
3540
change: any,
3641
): change is Extract<
3742
MapOpPayload<`parent_co_${string}`, AccountRole>,
3843
{ op: "set" }
3944
> => {
45+
if (coValue.core.isGroup() === false) return false;
4046
return change?.op === "set" && change?.key.startsWith("parent_co_");
4147
};
4248

4349
export const isUserPromotion = (
50+
coValue: RawCoValue,
4451
change: any,
4552
): change is Extract<MapOpPayload<CoID<RawCoValue>, Role>, { op: "set" }> => {
53+
if (coValue.core.isGroup() === false) return false;
4654
return (
4755
change?.op === "set" && (isCoId(change?.key) || change?.key === "everyone")
4856
);
4957
};
5058

5159
export const isKeyRevelation = (
60+
coValue: RawCoValue,
5261
change: any,
5362
): change is Extract<
5463
MapOpPayload<`${string}_for_${string}`, string>,
5564
{ op: "set" }
5665
> => {
66+
if (coValue.core.isGroup() === false) return false;
5767
return change?.op === "set" && change?.key.includes("_for_");
5868
};
5969

6070
export const isPropertySet = (
71+
coValue: RawCoValue,
6172
change: any,
6273
): change is Extract<MapOpPayload<string, any>, { op: "set" }> => {
6374
return change?.op === "set" && "key" in change && "value" in change;
6475
};
6576
export const isPropertyDeletion = (
77+
coValue: RawCoValue,
6678
change: any,
6779
): change is Extract<MapOpPayload<string, any>, { op: "del" }> => {
6880
return change?.op === "del" && "key" in change;
6981
};
7082

7183
export const isItemAppend = (
84+
coValue: RawCoValue,
7285
change: any,
7386
): change is Extract<ListOpPayload<any>, { op: "app" }> => {
87+
if (coValue.type !== "coList") return false;
7488
return change?.op === "app" && "after" in change && "value" in change;
7589
};
7690
export const isItemPrepend = (
91+
coValue: RawCoValue,
7792
change: any,
7893
): change is Extract<ListOpPayload<any>, { op: "pre" }> => {
94+
if (coValue.type !== "coList") return false;
7995
return change?.op === "pre" && "before" in change && "value" in change;
8096
};
8197

8298
export const isItemDeletion = (
99+
coValue: RawCoValue,
83100
change: any,
84101
): change is Extract<ListOpPayload<any>, { op: "del" }> => {
102+
if (coValue.type !== "coList") return false;
85103
return change?.op === "del" && "insertion" in change;
86104
};
87105

88-
export const isStreamStart = (change: any): change is BinaryStreamStart => {
106+
export const isStreamStart = (
107+
coValue: RawCoValue,
108+
change: any,
109+
): change is BinaryStreamStart => {
110+
if (coValue.type !== "coStream") return false;
89111
return change?.type === "start" && "mimeType" in change;
90112
};
91113

92-
export const isStreamChunk = (change: any): change is BinaryStreamChunk => {
114+
export const isStreamChunk = (
115+
coValue: RawCoValue,
116+
change: any,
117+
): change is BinaryStreamChunk => {
118+
if (coValue.type !== "coStream") return false;
93119
return change?.type === "chunk" && "chunk" in change;
94120
};
95121

96-
export const isStreamEnd = (change: any): change is BinaryStreamEnd => {
122+
export const isStreamEnd = (
123+
coValue: RawCoValue,
124+
change: any,
125+
): change is BinaryStreamEnd => {
126+
if (coValue.type !== "coStream") return false;
97127
return change?.type === "end";
98128
};

packages/jazz-tools/src/inspector/viewer/history-view.tsx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -128,36 +128,36 @@ function mapTransactionToAction(
128128
coValue: RawCoValue,
129129
): string {
130130
// Group changes
131-
if (TransactionChanges.isUserPromotion(change)) {
131+
if (TransactionChanges.isUserPromotion(coValue, change)) {
132132
if (change.value === "revoked") {
133133
return `${change.key} has been revoked`;
134134
}
135135

136136
return `${change.key} has been promoted to ${change.value}`;
137137
}
138138

139-
if (TransactionChanges.isGroupExtension(change)) {
139+
if (TransactionChanges.isGroupExtension(coValue, change)) {
140140
const child = change.key.slice(6);
141141
return `Group became a member of ${child}`;
142142
}
143143

144-
if (TransactionChanges.isGroupExtendRevocation(change)) {
144+
if (TransactionChanges.isGroupExtendRevocation(coValue, change)) {
145145
const child = change.key.slice(6);
146146
return `Group's membership of ${child} has been revoked.`;
147147
}
148148

149-
if (TransactionChanges.isGroupPromotion(change)) {
149+
if (TransactionChanges.isGroupPromotion(coValue, change)) {
150150
const parent = change.key.slice(7);
151151
return `Group ${parent} has been promoted to ${change.value}`;
152152
}
153153

154-
if (TransactionChanges.isKeyRevelation(change)) {
154+
if (TransactionChanges.isKeyRevelation(coValue, change)) {
155155
const [key, target] = change.key.split("_for_");
156156
return `Key "${key}" has been revealed to "${target}"`;
157157
}
158158

159159
// coList changes
160-
if (TransactionChanges.isItemAppend(change)) {
160+
if (TransactionChanges.isItemAppend(coValue, change)) {
161161
if (change.after === "start") {
162162
return `"${change.value}" has been appended`;
163163
}
@@ -171,7 +171,7 @@ function mapTransactionToAction(
171171
return `"${change.value}" has been inserted after "${(after as any).value}"`;
172172
}
173173

174-
if (TransactionChanges.isItemPrepend(change)) {
174+
if (TransactionChanges.isItemPrepend(coValue, change)) {
175175
if (change.before === "end") {
176176
return `"${change.value}" has been prepended`;
177177
}
@@ -185,7 +185,7 @@ function mapTransactionToAction(
185185
return `"${change.value}" has been inserted before "${(before as any).value}"`;
186186
}
187187

188-
if (TransactionChanges.isItemDeletion(change)) {
188+
if (TransactionChanges.isItemDeletion(coValue, change)) {
189189
const insertion = findListChange(change.insertion, coValue);
190190
if (insertion === undefined) {
191191
return `An undefined item has been deleted`;
@@ -195,24 +195,24 @@ function mapTransactionToAction(
195195
}
196196

197197
// coStream changes
198-
if (TransactionChanges.isStreamStart(change)) {
198+
if (TransactionChanges.isStreamStart(coValue, change)) {
199199
return `Stream started with mime type "${change.mimeType}" and file name "${change.fileName}"`;
200200
}
201201

202-
if (TransactionChanges.isStreamChunk(change)) {
202+
if (TransactionChanges.isStreamChunk(coValue, change)) {
203203
return `Stream chunk added`;
204204
}
205205

206-
if (TransactionChanges.isStreamEnd(change)) {
206+
if (TransactionChanges.isStreamEnd(coValue, change)) {
207207
return `Stream ended`;
208208
}
209209

210210
// coMap changes
211-
if (TransactionChanges.isPropertySet(change)) {
211+
if (TransactionChanges.isPropertySet(coValue, change)) {
212212
return `Property "${change.key}" has been set to ${JSON.stringify(change.value)}`;
213213
}
214214

215-
if (TransactionChanges.isPropertyDeletion(change)) {
215+
if (TransactionChanges.isPropertyDeletion(coValue, change)) {
216216
return `Property "${change.key}" has been deleted`;
217217
}
218218

0 commit comments

Comments
 (0)