@api-client/core 0.11.2 → 0.11.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.
@@ -1,5 +1,13 @@
1
1
  import { test } from '@japa/runner'
2
- import { DataEntity, DataModel, DataNamespace, DataNamespaceKind, IDataNamespace, Thing } from '../../../src/index.js'
2
+ import {
3
+ DataEntity,
4
+ DataItem,
5
+ DataModel,
6
+ DataNamespace,
7
+ DataNamespaceKind,
8
+ IDataNamespace,
9
+ Thing,
10
+ } from '../../../src/index.js'
3
11
 
4
12
  test.group('constructor() defaults', () => {
5
13
  test('sets the kind', ({ assert }) => {
@@ -821,3 +829,377 @@ test.group('computeForeignNamespaceKeys()', () => {
821
829
  assert.deepEqual(result, [n2.key])
822
830
  })
823
831
  })
832
+
833
+ test.group('findAssociatedEntity()', (group) => {
834
+ let root: DataNamespace
835
+ let foreign: DataNamespace
836
+ let e1: DataEntity
837
+ let e2: DataEntity
838
+
839
+ group.each.setup(() => {
840
+ root = new DataNamespace()
841
+ foreign = new DataNamespace()
842
+ root.addForeign(foreign)
843
+ const model1 = root.addDataModel('m1')
844
+ const model2 = foreign.addDataModel('m2')
845
+ e1 = model1.addEntity('e1')
846
+ e2 = model2.addEntity('e2')
847
+ })
848
+
849
+ test('finds an entity in the current namespace', ({ assert }) => {
850
+ const result = root.findAssociatedEntity(e1.key)
851
+ assert.deepEqual(result, e1)
852
+ })
853
+
854
+ test('finds an entity in a foreign namespace', ({ assert }) => {
855
+ const result = root.findAssociatedEntity(e2.key, foreign.key)
856
+ assert.deepEqual(result, e2)
857
+ })
858
+
859
+ test('returns undefined when the entity is not found in the current namespace', ({ assert }) => {
860
+ const result = root.findAssociatedEntity('non-existent-key')
861
+ assert.isUndefined(result)
862
+ })
863
+
864
+ test('returns undefined when the entity is not found in a foreign namespace', ({ assert }) => {
865
+ const result = root.findAssociatedEntity('non-existent-key', foreign.key)
866
+ assert.isUndefined(result)
867
+ })
868
+
869
+ test('returns undefined when the foreign namespace does not exist', ({ assert }) => {
870
+ const result = root.findAssociatedEntity(e2.key, 'non-existent-namespace')
871
+ assert.isUndefined(result)
872
+ })
873
+
874
+ test('returns undefined when no namespace is provided and the entity is in a foreign namespace', ({ assert }) => {
875
+ const result = root.findAssociatedEntity(e2.key)
876
+ assert.isUndefined(result)
877
+ })
878
+ })
879
+
880
+ test.group('addForeign()', (group) => {
881
+ let root: DataNamespace
882
+ let foreign: DataNamespace
883
+
884
+ group.each.setup(() => {
885
+ root = new DataNamespace()
886
+ foreign = new DataNamespace()
887
+ })
888
+
889
+ test('adds a foreign namespace', ({ assert }) => {
890
+ root.addForeign(foreign)
891
+ assert.deepEqual(root.foreign, [foreign])
892
+ })
893
+
894
+ test('does not add the same foreign namespace twice', ({ assert }) => {
895
+ root.addForeign(foreign)
896
+ root.addForeign(foreign)
897
+ assert.deepEqual(root.foreign, [foreign])
898
+ })
899
+ })
900
+
901
+ test.group('removeForeign()', (group) => {
902
+ let root: DataNamespace
903
+ let foreign: DataNamespace
904
+ let foreign2: DataNamespace
905
+
906
+ group.each.setup(() => {
907
+ root = new DataNamespace()
908
+ foreign = new DataNamespace()
909
+ foreign2 = new DataNamespace()
910
+ root.addForeign(foreign)
911
+ root.addForeign(foreign2)
912
+ })
913
+
914
+ test('removes a foreign namespace', ({ assert }) => {
915
+ root.removeForeign(foreign)
916
+ assert.deepEqual(root.foreign, [foreign2])
917
+ })
918
+
919
+ test('does nothing when the foreign namespace does not exist', ({ assert }) => {
920
+ const other = new DataNamespace()
921
+ root.removeForeign(other)
922
+ assert.deepEqual(root.foreign, [foreign, foreign2])
923
+ })
924
+ })
925
+
926
+ test.group('hasForeignNamespace()', (group) => {
927
+ let root: DataNamespace
928
+ let foreign: DataNamespace
929
+
930
+ group.each.setup(() => {
931
+ root = new DataNamespace()
932
+ foreign = new DataNamespace()
933
+ root.addForeign(foreign)
934
+ })
935
+
936
+ test('returns true when the foreign namespace exists', ({ assert }) => {
937
+ const result = root.hasForeignNamespace(foreign.key)
938
+ assert.isTrue(result)
939
+ })
940
+
941
+ test('returns false when the foreign namespace does not exist', ({ assert }) => {
942
+ const result = root.hasForeignNamespace('non-existent-key')
943
+ assert.isFalse(result)
944
+ })
945
+ })
946
+
947
+ test.group('listNamespaces()', (group) => {
948
+ let root: DataNamespace
949
+ let n1: DataNamespace
950
+ let n2: DataNamespace
951
+ let m1: DataModel
952
+
953
+ group.each.setup(() => {
954
+ root = new DataNamespace()
955
+ n1 = root.addNamespace('n1')
956
+ n2 = root.addNamespace('n2')
957
+ m1 = root.addDataModel('m1')
958
+ })
959
+
960
+ test('lists all namespaces', ({ assert }) => {
961
+ const result = root.listNamespaces()
962
+ assert.deepEqual(result, [n1, n2])
963
+ })
964
+
965
+ test('lists namespaces in a sub-namespace', ({ assert }) => {
966
+ const n3 = n1.addNamespace('n3')
967
+ const result = n1.listNamespaces()
968
+ assert.deepEqual(result, [n3])
969
+ })
970
+
971
+ test('does not list data models', ({ assert }) => {
972
+ const result = root.listNamespaces()
973
+ assert.notDeepInclude(result, m1)
974
+ })
975
+ })
976
+
977
+ test.group('listDataModels()', (group) => {
978
+ let root: DataNamespace
979
+ let n1: DataNamespace
980
+ let m1: DataModel
981
+ let m2: DataModel
982
+ let e1: DataEntity
983
+
984
+ group.each.setup(() => {
985
+ root = new DataNamespace()
986
+ n1 = root.addNamespace('n1')
987
+ m1 = root.addDataModel('m1')
988
+ m2 = n1.addDataModel('m2')
989
+ e1 = m2.addEntity('e1')
990
+ })
991
+
992
+ test('lists all data models', ({ assert }) => {
993
+ const result = root.listDataModels()
994
+ assert.deepEqual(result, [m1])
995
+ })
996
+
997
+ test('lists data models in a sub-namespace', ({ assert }) => {
998
+ const m3 = n1.addDataModel('m3')
999
+ const result = n1.listDataModels()
1000
+ assert.deepEqual(result, [m2, m3])
1001
+ })
1002
+
1003
+ test('does not list entities', ({ assert }) => {
1004
+ const result = root.listDataModels()
1005
+ assert.notDeepInclude(result, e1)
1006
+ })
1007
+ })
1008
+
1009
+ test.group('getRoot()', (group) => {
1010
+ let root: DataNamespace
1011
+ let n1: DataNamespace
1012
+ let n2: DataNamespace
1013
+
1014
+ group.each.setup(() => {
1015
+ root = new DataNamespace()
1016
+ n1 = root.addNamespace('n1')
1017
+ n2 = n1.addNamespace('n2')
1018
+ })
1019
+
1020
+ test('returns self when called on the root namespace', ({ assert }) => {
1021
+ const result = root.getRoot()
1022
+ assert.deepEqual(result, root)
1023
+ })
1024
+
1025
+ test('returns the root namespace when called on a sub-namespace', ({ assert }) => {
1026
+ const result = n1.getRoot()
1027
+ assert.deepEqual(result, root)
1028
+ })
1029
+
1030
+ test('returns the root namespace when called on a sub-sub-namespace', ({ assert }) => {
1031
+ const result = n2.getRoot()
1032
+ assert.deepEqual(result, root)
1033
+ })
1034
+ })
1035
+
1036
+ test.group('getParent()', (group) => {
1037
+ let root: DataNamespace
1038
+ let n1: DataNamespace
1039
+ let n2: DataNamespace
1040
+
1041
+ group.each.setup(() => {
1042
+ root = new DataNamespace()
1043
+ n1 = root.addNamespace('n1')
1044
+ n2 = n1.addNamespace('n2')
1045
+ })
1046
+
1047
+ test('returns undefined when called on the root namespace', ({ assert }) => {
1048
+ const result = root.getParent()
1049
+ assert.isUndefined(result)
1050
+ })
1051
+
1052
+ test('returns the parent namespace when called on a sub-namespace', ({ assert }) => {
1053
+ const result = n2.getParent()
1054
+ assert.deepEqual(result, n1)
1055
+ })
1056
+ })
1057
+
1058
+ test.group('isRoot()', (group) => {
1059
+ let root: DataNamespace
1060
+ let n1: DataNamespace
1061
+
1062
+ group.each.setup(() => {
1063
+ root = new DataNamespace()
1064
+ n1 = root.addNamespace('n1')
1065
+ })
1066
+
1067
+ test('returns true when called on the root namespace', ({ assert }) => {
1068
+ const result = root.isRoot()
1069
+ assert.isTrue(result)
1070
+ })
1071
+
1072
+ test('returns false when called on a sub-namespace', ({ assert }) => {
1073
+ const result = n1.isRoot()
1074
+ assert.isFalse(result)
1075
+ })
1076
+ })
1077
+
1078
+ test.group('adaptNamespace()', (group) => {
1079
+ let root: DataNamespace
1080
+ let n1: DataNamespace
1081
+ let n2: DataNamespace
1082
+
1083
+ group.each.setup(() => {
1084
+ root = new DataNamespace()
1085
+ n1 = root.addNamespace('n1')
1086
+ n2 = root.addNamespace('n2')
1087
+ })
1088
+
1089
+ test('moves a namespace to a new parent', ({ assert }) => {
1090
+ const n3 = n1.addNamespace('n3')
1091
+ n2.adaptNamespace(n3)
1092
+ assert.deepEqual(n1.items, [], 'removes from the old parent')
1093
+ const item = DataItem.dataNamespace(root, n3.key)
1094
+ assert.deepEqual(n2.items, [item], 'adds to the new parent')
1095
+ assert.deepEqual(root.definitions.namespaces, [n1, n2, n3], 'keeps the namespace in the root definitions')
1096
+ })
1097
+
1098
+ test('moves a namespace to a new index', ({ assert }) => {
1099
+ const n3 = n1.addNamespace('n3')
1100
+ const n4 = n1.addNamespace('n4')
1101
+ const n5 = n2.addNamespace('n5')
1102
+ n2.adaptNamespace(n3, { index: 0 })
1103
+ const item3 = DataItem.dataNamespace(root, n3.key)
1104
+ const item4 = DataItem.dataNamespace(root, n4.key)
1105
+ const item5 = DataItem.dataNamespace(root, n5.key)
1106
+ assert.deepEqual(n1.items, [item4], 'removes from the old parent')
1107
+ assert.deepEqual(n2.items, [item3, item5], 'adds to the new parent')
1108
+ assert.deepEqual(root.definitions.namespaces, [n1, n2, n3, n4, n5], 'keeps the namespace in the root definitions')
1109
+ })
1110
+
1111
+ test('throws when adapting self', ({ assert }) => {
1112
+ assert.throws(() => n1.adaptNamespace(n1), 'Unable to adapt a namespace that is self.')
1113
+ })
1114
+
1115
+ test('throws when adapting a namespace from another root', ({ assert }) => {
1116
+ const otherRoot = new DataNamespace()
1117
+ const otherNs = otherRoot.addNamespace('other')
1118
+ assert.throws(
1119
+ () => n1.adaptNamespace(otherNs),
1120
+ `The namespace ${otherNs.key} is not in the same namespace as this data namespace.`
1121
+ )
1122
+ })
1123
+
1124
+ test('throws when adapting a namespace that is already adapted', ({ assert }) => {
1125
+ const n3 = n1.addNamespace('n3')
1126
+ n2.adaptNamespace(n3)
1127
+ assert.throws(() => n2.adaptNamespace(n3), `The namespace ${n3.key} is already adapted by this data namespace.`)
1128
+ })
1129
+
1130
+ test('throws when index is out of range (minimum)', ({ assert }) => {
1131
+ assert.throws(() => n1.adaptNamespace(n2, { index: -1 }), `The index -1 cannot be below 0.`)
1132
+ })
1133
+
1134
+ test('throws when index is out of range (maximum)', ({ assert }) => {
1135
+ assert.throws(() => n1.adaptNamespace(n2, { index: 1 }), `The index 1 is not valid.`)
1136
+ })
1137
+ })
1138
+
1139
+ test.group('adaptNamespace() - Circular Dependency', (group) => {
1140
+ let root: DataNamespace
1141
+ let n1: DataNamespace
1142
+ let n2: DataNamespace
1143
+ let n3: DataNamespace
1144
+ let n4: DataNamespace
1145
+
1146
+ // Root
1147
+ // ├── n1
1148
+ // │ ├── n3
1149
+ // │ │ └── n4
1150
+ // └── n2
1151
+
1152
+ group.each.setup(() => {
1153
+ root = new DataNamespace()
1154
+ n1 = root.addNamespace('n1')
1155
+ n2 = root.addNamespace('n2')
1156
+ n3 = n1.addNamespace('n3')
1157
+ n4 = n3.addNamespace('n4')
1158
+ })
1159
+
1160
+ test('throws when adapting a namespace under itself', ({ assert }) => {
1161
+ assert.throws(() => n1.adaptNamespace(n1), 'Unable to adapt a namespace that is self.')
1162
+ })
1163
+
1164
+ test('throws when adapting a namespace under its child', ({ assert }) => {
1165
+ assert.throws(
1166
+ () => n3.adaptNamespace(n1),
1167
+ `Unable to adapt namespace ${n1.key} under itself or one of its children.`
1168
+ )
1169
+ })
1170
+
1171
+ test('throws when adapting a namespace under its grandchild', ({ assert }) => {
1172
+ assert.throws(
1173
+ () => n4.adaptNamespace(n1),
1174
+ `Unable to adapt namespace ${n1.key} under itself or one of its children.`
1175
+ )
1176
+ })
1177
+
1178
+ test('does not throw when adapting a namespace under its sibling', ({ assert }) => {
1179
+ const n5 = n1.addNamespace('n5')
1180
+ assert.doesNotThrow(() => n2.adaptNamespace(n5))
1181
+ })
1182
+
1183
+ test("does not throw when adapting a namespace under its sibling's child", ({ assert }) => {
1184
+ const n5 = n1.addNamespace('n5')
1185
+ const n6 = n5.addNamespace('n6')
1186
+ assert.doesNotThrow(() => n2.adaptNamespace(n6))
1187
+ })
1188
+
1189
+ test("does not throw when adapting a namespace under its sibling's parent", ({ assert }) => {
1190
+ const n5 = n1.addNamespace('n5')
1191
+ // Root (before)
1192
+ // ├── n1
1193
+ // │ ├── n3
1194
+ // │ │ └── n4
1195
+ // │ └── n5
1196
+ // └── n2
1197
+ // Root (after)
1198
+ // └── n1
1199
+ // ├── n3
1200
+ // │ └── n4
1201
+ // └── n5
1202
+ // └── n2
1203
+ assert.doesNotThrow(() => n5.adaptNamespace(n2))
1204
+ })
1205
+ })
@@ -1,33 +0,0 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <title>Oauth2 callback window</title>
6
- <style>
7
- *[hidden] {
8
- display: none;
9
- }
10
- </style>
11
- </head>
12
- <body>
13
- <h1>Sending the authorization data to the application</h1>
14
- <p id="general-error" hidden>
15
- The window wasn't opened as a popup and therefore it can't pass the authorization information.<br />
16
- This is an error.
17
- </p>
18
- <script>
19
- const messageTarget = window.opener || window.parent || window.top
20
- if (!messageTarget || messageTarget === window || !messageTarget.postMessage) {
21
- const elm = document.getElementById('general-error')
22
- elm.removeAttribute('hidden')
23
- } else {
24
- const search = window.location.search.substr(1)
25
- if (search) {
26
- messageTarget.postMessage(search, '*')
27
- } else {
28
- messageTarget.postMessage(window.location.hash.substr(1), '*')
29
- }
30
- }
31
- </script>
32
- </body>
33
- </html>