cojson 0.11.8 → 0.12.1

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.
@@ -5,6 +5,7 @@ import { RawCoMap } from "../coValues/coMap.js";
5
5
  import { RawCoStream } from "../coValues/coStream.js";
6
6
  import { RawBinaryCoStream } from "../coValues/coStream.js";
7
7
  import { WasmCrypto } from "../crypto/WasmCrypto.js";
8
+ import { RawAccountID } from "../exports.js";
8
9
  import { LocalNode } from "../localNode.js";
9
10
  import {
10
11
  createThreeConnectedNodes,
@@ -915,117 +916,215 @@ describe("extend with role mapping", () => {
915
916
  expect(mapOnNode2.get("test")).toEqual("Written from the admin");
916
917
  });
917
918
  });
918
- test("roleOf should prioritize explicit account role over everyone role in same group", async () => {
919
- const { node1, node2 } = await createTwoConnectedNodes("server", "server");
920
919
 
921
- const group = node1.node.createGroup();
922
- const account2 = await loadCoValueOrFail(node1.node, node2.accountID);
920
+ describe("roleOf", () => {
921
+ test("returns direct role assignments", () => {
922
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
923
+ const group = node.createGroup();
924
+ const account = new LocalNode(
925
+ ...randomAnonymousAccountAndSessionID(),
926
+ Crypto,
927
+ ).account;
928
+
929
+ group.addMember(account, "writer");
930
+ expect(group.roleOf(account.id as RawAccountID)).toEqual("writer");
931
+ });
923
932
 
924
- // Add both everyone and specific account
925
- group.addMember("everyone", "reader");
926
- group.addMember(account2, "writer");
933
+ test("returns undefined for non-members", () => {
934
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
935
+ const group = node.createGroup();
936
+ const account = new LocalNode(
937
+ ...randomAnonymousAccountAndSessionID(),
938
+ Crypto,
939
+ ).account;
927
940
 
928
- // Should return the explicit role, not everyone's role
929
- expect(group.roleOf(node2.accountID)).toEqual("writer");
941
+ expect(group.roleOf(account.id as RawAccountID)).toEqual(undefined);
942
+ });
930
943
 
931
- // Change everyone's role
932
- group.addMember("everyone", "writer");
944
+ test("revoked roles return undefined", () => {
945
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
946
+ const group = node.createGroup();
947
+ const account = new LocalNode(
948
+ ...randomAnonymousAccountAndSessionID(),
949
+ Crypto,
950
+ ).account;
951
+
952
+ group.addMember(account, "writer");
953
+ group.removeMemberInternal(account);
954
+ expect(group.roleOf(account.id as RawAccountID)).toEqual(undefined);
955
+ });
933
956
 
934
- // Should still return the explicit role
935
- expect(group.roleOf(node2.accountID)).toEqual("writer");
936
- });
957
+ test("everyone role applies to all accounts", () => {
958
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
959
+ const group = node.createGroup();
960
+ const account = new LocalNode(
961
+ ...randomAnonymousAccountAndSessionID(),
962
+ Crypto,
963
+ ).account;
937
964
 
938
- test("roleOf should prioritize inherited everyone role over explicit account role", async () => {
939
- const { node1, node2 } = await createTwoConnectedNodes("server", "server");
965
+ group.addMemberInternal("everyone", "reader");
966
+ expect(group.roleOf(account.id as RawAccountID)).toEqual("reader");
967
+ });
940
968
 
941
- const parentGroup = node1.node.createGroup();
942
- const childGroup = node1.node.createGroup();
943
- const account2 = await loadCoValueOrFail(node1.node, node2.accountID);
969
+ test("account role overrides everyone role", () => {
970
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
971
+ const group = node.createGroup();
972
+ const account = new LocalNode(
973
+ ...randomAnonymousAccountAndSessionID(),
974
+ Crypto,
975
+ ).account;
976
+
977
+ group.addMemberInternal("everyone", "writer");
978
+ group.addMember(account, "reader");
979
+ expect(group.roleOf(account.id as RawAccountID)).toEqual("reader");
980
+ });
944
981
 
945
- // Set up inheritance
946
- childGroup.extend(parentGroup);
982
+ test("Revoking access on everyone role should not affect existing members", () => {
983
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
984
+ const group = node.createGroup();
985
+ const account = new LocalNode(
986
+ ...randomAnonymousAccountAndSessionID(),
987
+ Crypto,
988
+ ).account;
989
+
990
+ group.addMemberInternal("everyone", "reader");
991
+ group.addMember(account, "writer");
992
+ group.removeMemberInternal("everyone");
993
+ expect(group.roleOf(account.id as RawAccountID)).toEqual("writer");
994
+ expect(group.roleOf("123" as RawAccountID)).toEqual(undefined);
995
+ });
947
996
 
948
- // Add everyone to parent and account to child
949
- parentGroup.addMember("everyone", "writer");
950
- childGroup.addMember(account2, "reader");
997
+ test("Everyone role is inherited following the most permissive algorithm", () => {
998
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
999
+ const group = node.createGroup();
1000
+ const account = new LocalNode(
1001
+ ...randomAnonymousAccountAndSessionID(),
1002
+ Crypto,
1003
+ ).account;
951
1004
 
952
- // Should return the explicit role from child, not inherited everyone role
953
- expect(childGroup.roleOf(node2.accountID)).toEqual("writer");
954
- });
1005
+ const parentGroup = node.createGroup();
1006
+ parentGroup.addMemberInternal("everyone", "writer");
955
1007
 
956
- test("roleOf should use everyone role when no explicit role exists", async () => {
957
- const { node1, node2 } = await createTwoConnectedNodes("server", "server");
1008
+ group.extend(parentGroup);
1009
+ group.addMember(account, "reader");
958
1010
 
959
- const group = node1.node.createGroup();
1011
+ expect(group.roleOf(account.id as RawAccountID)).toEqual("writer");
1012
+ });
1013
+ test("roleOf should prioritize explicit account role over everyone role in same group", async () => {
1014
+ const { node1, node2 } = await createTwoConnectedNodes("server", "server");
960
1015
 
961
- // Add only everyone role
962
- group.addMember("everyone", "reader");
1016
+ const group = node1.node.createGroup();
1017
+ const account2 = await loadCoValueOrFail(node1.node, node2.accountID);
963
1018
 
964
- // Should return everyone's role when no explicit role exists
965
- expect(group.roleOf(node2.accountID)).toEqual("reader");
966
- });
1019
+ // Add both everyone and specific account
1020
+ group.addMember("everyone", "reader");
1021
+ group.addMember(account2, "writer");
967
1022
 
968
- test("roleOf should inherit everyone role from parent when no explicit roles exist", async () => {
969
- const { node1, node2 } = await createTwoConnectedNodes("server", "server");
1023
+ // Should return the explicit role, not everyone's role
1024
+ expect(group.roleOf(node2.accountID)).toEqual("writer");
970
1025
 
971
- const parentGroup = node1.node.createGroup();
972
- const childGroup = node1.node.createGroup();
1026
+ // Change everyone's role
1027
+ group.addMember("everyone", "writer");
973
1028
 
974
- // Set up inheritance
975
- childGroup.extend(parentGroup);
1029
+ // Should still return the explicit role
1030
+ expect(group.roleOf(node2.accountID)).toEqual("writer");
1031
+ });
976
1032
 
977
- // Add everyone to parent only
978
- parentGroup.addMember("everyone", "reader");
1033
+ test("roleOf should prioritize inherited everyone role over explicit account role", async () => {
1034
+ const { node1, node2 } = await createTwoConnectedNodes("server", "server");
979
1035
 
980
- // Should inherit everyone's role from parent
981
- expect(childGroup.roleOf(node2.accountID)).toEqual("reader");
982
- });
1036
+ const parentGroup = node1.node.createGroup();
1037
+ const childGroup = node1.node.createGroup();
1038
+ const account2 = await loadCoValueOrFail(node1.node, node2.accountID);
983
1039
 
984
- test("roleOf should handle everyone role inheritance through multiple levels", async () => {
985
- const { node1, node2 } = await createTwoConnectedNodes("server", "server");
1040
+ // Set up inheritance
1041
+ childGroup.extend(parentGroup);
986
1042
 
987
- const grandParentGroup = node1.node.createGroup();
988
- const parentGroup = node1.node.createGroup();
989
- const childGroup = node1.node.createGroup();
1043
+ // Add everyone to parent and account to child
1044
+ parentGroup.addMember("everyone", "writer");
1045
+ childGroup.addMember(account2, "reader");
990
1046
 
991
- const childGroupOnNode2 = await loadCoValueOrFail(node2.node, childGroup.id);
1047
+ // Should return the explicit role from child, not inherited everyone role
1048
+ expect(childGroup.roleOf(node2.accountID)).toEqual("writer");
1049
+ });
992
1050
 
993
- const account2 = await loadCoValueOrFail(node1.node, node2.accountID);
1051
+ test("roleOf should use everyone role when no explicit role exists", async () => {
1052
+ const { node1, node2 } = await createTwoConnectedNodes("server", "server");
994
1053
 
995
- // Set up inheritance chain
996
- parentGroup.extend(grandParentGroup);
997
- childGroup.extend(parentGroup);
1054
+ const group = node1.node.createGroup();
998
1055
 
999
- // Add everyone to grandparent
1000
- grandParentGroup.addMember("everyone", "writer");
1056
+ // Add only everyone role
1057
+ group.addMember("everyone", "reader");
1001
1058
 
1002
- // Should inherit everyone's role from grandparent
1003
- expect(childGroup.roleOf(node2.accountID)).toEqual("writer");
1059
+ // Should return everyone's role when no explicit role exists
1060
+ expect(group.roleOf(node2.accountID)).toEqual("reader");
1061
+ });
1004
1062
 
1005
- // Add explicit role in parent
1006
- parentGroup.addMember(account2, "reader");
1063
+ test("roleOf should inherit everyone role from parent when no explicit roles exist", async () => {
1064
+ const { node1, node2 } = await createTwoConnectedNodes("server", "server");
1007
1065
 
1008
- // Should use parent's explicit role instead of grandparent's everyone role
1009
- expect(childGroup.roleOf(node2.accountID)).toEqual("writer");
1066
+ const parentGroup = node1.node.createGroup();
1067
+ const childGroup = node1.node.createGroup();
1010
1068
 
1011
- // Add explicit role in child
1012
- childGroup.addMember(account2, "admin");
1013
- await childGroup.core.waitForSync();
1069
+ // Set up inheritance
1070
+ childGroup.extend(parentGroup);
1014
1071
 
1015
- // Should use child's explicit role
1016
- expect(childGroup.roleOf(node2.accountID)).toEqual("admin");
1072
+ // Add everyone to parent only
1073
+ parentGroup.addMember("everyone", "reader");
1017
1074
 
1018
- // Remove child's explicit role
1019
- await childGroupOnNode2.removeMember(account2);
1020
- await childGroupOnNode2.core.waitForSync();
1075
+ // Should inherit everyone's role from parent
1076
+ expect(childGroup.roleOf(node2.accountID)).toEqual("reader");
1077
+ });
1021
1078
 
1022
- // Should fall back to parent's explicit role
1023
- expect(childGroup.roleOf(node2.accountID)).toEqual("writer");
1079
+ test("roleOf should handle everyone role inheritance through multiple levels", async () => {
1080
+ const { node1, node2 } = await createTwoConnectedNodes("server", "server");
1024
1081
 
1025
- // Remove parent's explicit role
1026
- await parentGroup.removeMember(account2);
1027
- await childGroup.core.waitForSync();
1082
+ const grandParentGroup = node1.node.createGroup();
1083
+ const parentGroup = node1.node.createGroup();
1084
+ const childGroup = node1.node.createGroup();
1085
+
1086
+ const childGroupOnNode2 = await loadCoValueOrFail(
1087
+ node2.node,
1088
+ childGroup.id,
1089
+ );
1090
+
1091
+ const account2 = await loadCoValueOrFail(node1.node, node2.accountID);
1092
+
1093
+ // Set up inheritance chain
1094
+ parentGroup.extend(grandParentGroup);
1095
+ childGroup.extend(parentGroup);
1096
+
1097
+ // Add everyone to grandparent
1098
+ grandParentGroup.addMember("everyone", "writer");
1099
+
1100
+ // Should inherit everyone's role from grandparent
1101
+ expect(childGroup.roleOf(node2.accountID)).toEqual("writer");
1102
+
1103
+ // Add explicit role in parent
1104
+ parentGroup.addMember(account2, "reader");
1105
+
1106
+ // Should use parent's explicit role instead of grandparent's everyone role
1107
+ expect(childGroup.roleOf(node2.accountID)).toEqual("writer");
1108
+
1109
+ // Add explicit role in child
1110
+ childGroup.addMember(account2, "admin");
1111
+ await childGroup.core.waitForSync();
1112
+
1113
+ // Should use child's explicit role
1114
+ expect(childGroup.roleOf(node2.accountID)).toEqual("admin");
1028
1115
 
1029
- // Should fall back to grandparent's everyone role
1030
- expect(childGroup.roleOf(node2.accountID)).toEqual("writer");
1116
+ // Remove child's explicit role
1117
+ await childGroupOnNode2.removeMember(account2);
1118
+ await childGroupOnNode2.core.waitForSync();
1119
+
1120
+ // Should fall back to parent's explicit role
1121
+ expect(childGroup.roleOf(node2.accountID)).toEqual("writer");
1122
+
1123
+ // Remove parent's explicit role
1124
+ await parentGroup.removeMember(account2);
1125
+ await childGroup.core.waitForSync();
1126
+
1127
+ // Should fall back to grandparent's everyone role
1128
+ expect(childGroup.roleOf(node2.accountID)).toEqual("writer");
1129
+ });
1031
1130
  });