arocapi 5.0.0-beta.4 → 5.0.0-beta.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.
Files changed (160) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +3 -3
  3. package/dist/app.d.ts +19 -13
  4. package/dist/app.js +13 -10
  5. package/dist/app.js.map +1 -1
  6. package/dist/routes/capabilities.d.ts +2 -2
  7. package/dist/routes/capabilities.js +2 -2
  8. package/dist/routes/capabilities.js.map +1 -1
  9. package/dist/routes/entities.js +8 -9
  10. package/dist/routes/entities.js.map +1 -1
  11. package/dist/routes/file.js +5 -1
  12. package/dist/routes/file.js.map +1 -1
  13. package/dist/routes/search.d.ts +2 -4
  14. package/dist/routes/search.js +75 -97
  15. package/dist/routes/search.js.map +1 -1
  16. package/dist/search/advanced.d.ts +8 -0
  17. package/dist/search/advanced.js +198 -0
  18. package/dist/search/advanced.js.map +1 -0
  19. package/dist/search/backend.d.ts +95 -0
  20. package/dist/search/backend.js +5 -0
  21. package/dist/search/backend.js.map +1 -0
  22. package/dist/search/fields.d.ts +63 -0
  23. package/dist/search/fields.js +203 -0
  24. package/dist/search/fields.js.map +1 -0
  25. package/dist/search/opensearch/advanced.d.ts +4 -0
  26. package/dist/search/opensearch/advanced.js +28 -0
  27. package/dist/search/opensearch/advanced.js.map +1 -0
  28. package/dist/search/opensearch/opensearch.d.ts +16 -0
  29. package/dist/search/opensearch/opensearch.js +187 -0
  30. package/dist/search/opensearch/opensearch.js.map +1 -0
  31. package/dist/search/opensearch/visibility.d.ts +3 -0
  32. package/dist/search/opensearch/visibility.js +27 -0
  33. package/dist/search/opensearch/visibility.js.map +1 -0
  34. package/dist/search/postgres/advanced.d.ts +7 -0
  35. package/dist/search/postgres/advanced.js +54 -0
  36. package/dist/search/postgres/advanced.js.map +1 -0
  37. package/dist/search/postgres/facets.d.ts +2 -0
  38. package/dist/search/postgres/facets.js +27 -0
  39. package/dist/search/postgres/facets.js.map +1 -0
  40. package/dist/search/postgres/geo.d.ts +6 -0
  41. package/dist/search/postgres/geo.js +42 -0
  42. package/dist/search/postgres/geo.js.map +1 -0
  43. package/dist/search/postgres/postgres.d.ts +19 -0
  44. package/dist/search/postgres/postgres.js +157 -0
  45. package/dist/search/postgres/postgres.js.map +1 -0
  46. package/dist/search/postgres/values.d.ts +8 -0
  47. package/dist/search/postgres/values.js +20 -0
  48. package/dist/search/postgres/values.js.map +1 -0
  49. package/dist/search/postgres/visibility.d.ts +4 -0
  50. package/dist/search/postgres/visibility.js +38 -0
  51. package/dist/search/postgres/visibility.js.map +1 -0
  52. package/dist/search/requestFilters.d.ts +39 -0
  53. package/dist/search/requestFilters.js +125 -0
  54. package/dist/search/requestFilters.js.map +1 -0
  55. package/dist/search/visibility.d.ts +51 -0
  56. package/dist/search/visibility.js +100 -0
  57. package/dist/search/visibility.js.map +1 -0
  58. package/dist/types/visibility.d.ts +3 -2
  59. package/dist/utils/visibility.js +1 -1
  60. package/dist/utils/visibility.js.map +1 -1
  61. package/package.json +46 -26
  62. package/prisma/models/entity.prisma +9 -8
  63. package/prisma/models/entitySearch.prisma +21 -0
  64. package/prisma/models/file.prisma +3 -3
  65. package/src/app.ts +53 -24
  66. package/src/routes/capabilities.ts +4 -4
  67. package/src/routes/entities.ts +10 -9
  68. package/src/routes/file.ts +5 -1
  69. package/src/routes/search.ts +104 -118
  70. package/src/search/advanced.ts +258 -0
  71. package/src/search/backend.ts +91 -0
  72. package/src/search/fields.ts +320 -0
  73. package/src/search/opensearch/advanced.ts +36 -0
  74. package/src/search/opensearch/opensearch.ts +290 -0
  75. package/src/search/opensearch/visibility.ts +38 -0
  76. package/src/search/postgres/advanced.ts +76 -0
  77. package/src/search/postgres/facets.ts +34 -0
  78. package/src/search/postgres/geo.ts +58 -0
  79. package/src/search/postgres/postgres.ts +232 -0
  80. package/src/search/postgres/values.ts +33 -0
  81. package/src/search/postgres/visibility.ts +54 -0
  82. package/src/search/requestFilters.ts +178 -0
  83. package/src/search/visibility.ts +193 -0
  84. package/src/types/visibility.ts +6 -3
  85. package/src/utils/visibility.ts +1 -1
  86. package/dist/app.test.d.ts +0 -1
  87. package/dist/app.test.js +0 -67
  88. package/dist/app.test.js.map +0 -1
  89. package/dist/errorHandling.test.d.ts +0 -1
  90. package/dist/errorHandling.test.js +0 -222
  91. package/dist/errorHandling.test.js.map +0 -1
  92. package/dist/index.dev.d.ts +0 -1
  93. package/dist/index.dev.js +0 -157
  94. package/dist/index.dev.js.map +0 -1
  95. package/dist/routes/capabilities.test.d.ts +0 -1
  96. package/dist/routes/capabilities.test.js +0 -81
  97. package/dist/routes/capabilities.test.js.map +0 -1
  98. package/dist/routes/crate.test.d.ts +0 -1
  99. package/dist/routes/crate.test.js +0 -390
  100. package/dist/routes/crate.test.js.map +0 -1
  101. package/dist/routes/entities.test.d.ts +0 -1
  102. package/dist/routes/entities.test.js +0 -277
  103. package/dist/routes/entities.test.js.map +0 -1
  104. package/dist/routes/entity.test.d.ts +0 -1
  105. package/dist/routes/entity.test.js +0 -136
  106. package/dist/routes/entity.test.js.map +0 -1
  107. package/dist/routes/file.test.d.ts +0 -1
  108. package/dist/routes/file.test.js +0 -425
  109. package/dist/routes/file.test.js.map +0 -1
  110. package/dist/routes/files.test.d.ts +0 -1
  111. package/dist/routes/files.test.js +0 -245
  112. package/dist/routes/files.test.js.map +0 -1
  113. package/dist/routes/search.test.d.ts +0 -1
  114. package/dist/routes/search.test.js +0 -833
  115. package/dist/routes/search.test.js.map +0 -1
  116. package/dist/test/helpers/fastify.d.ts +0 -9
  117. package/dist/test/helpers/fastify.js +0 -17
  118. package/dist/test/helpers/fastify.js.map +0 -1
  119. package/dist/test/integration.setup.d.ts +0 -47
  120. package/dist/test/integration.setup.js +0 -195
  121. package/dist/test/integration.setup.js.map +0 -1
  122. package/dist/test/integration.test.d.ts +0 -1
  123. package/dist/test/integration.test.js +0 -762
  124. package/dist/test/integration.test.js.map +0 -1
  125. package/dist/transformers/default.test.d.ts +0 -1
  126. package/dist/transformers/default.test.js +0 -225
  127. package/dist/transformers/default.test.js.map +0 -1
  128. package/dist/transformers/transformer.test.d.ts +0 -1
  129. package/dist/transformers/transformer.test.js +0 -165
  130. package/dist/transformers/transformer.test.js.map +0 -1
  131. package/dist/utils/errors.test.d.ts +0 -1
  132. package/dist/utils/errors.test.js +0 -79
  133. package/dist/utils/errors.test.js.map +0 -1
  134. package/dist/utils/queryBuilder.d.ts +0 -47
  135. package/dist/utils/queryBuilder.js +0 -107
  136. package/dist/utils/queryBuilder.js.map +0 -1
  137. package/prisma.config.ts +0 -9
  138. package/src/app.test.ts +0 -91
  139. package/src/errorHandling.test.ts +0 -288
  140. package/src/index.dev.ts +0 -202
  141. package/src/reset.d.ts +0 -1
  142. package/src/routes/__snapshots__/capabilities.test.ts.snap +0 -34
  143. package/src/routes/__snapshots__/entities.test.ts.snap +0 -96
  144. package/src/routes/__snapshots__/entity.test.ts.snap +0 -74
  145. package/src/routes/__snapshots__/search.test.ts.snap +0 -165
  146. package/src/routes/capabilities.test.ts +0 -102
  147. package/src/routes/crate.test.ts +0 -484
  148. package/src/routes/entities.test.ts +0 -324
  149. package/src/routes/entity.test.ts +0 -166
  150. package/src/routes/file.test.ts +0 -526
  151. package/src/routes/files.test.ts +0 -304
  152. package/src/routes/search.test.ts +0 -959
  153. package/src/test/__snapshots__/integration.test.ts.snap +0 -18
  154. package/src/test/helpers/fastify.ts +0 -26
  155. package/src/test/integration.setup.ts +0 -220
  156. package/src/test/integration.test.ts +0 -953
  157. package/src/transformers/default.test.ts +0 -276
  158. package/src/transformers/transformer.test.ts +0 -226
  159. package/src/utils/errors.test.ts +0 -90
  160. package/src/utils/queryBuilder.ts +0 -157
package/src/index.dev.ts DELETED
@@ -1,202 +0,0 @@
1
- import { createReadStream, existsSync, readFileSync, statSync } from 'node:fs';
2
- import { Readable } from 'node:stream';
3
- import { Client } from '@opensearch-project/opensearch';
4
- import { PrismaMariaDb } from '@prisma/adapter-mariadb';
5
- import Fastify from 'fastify';
6
-
7
- import arocapi, {
8
- AllPublicAccess,
9
- AllVisibleEntityFilter,
10
- AllVisibleSearchFilter,
11
- type EntityTransformer,
12
- type FileHandler,
13
- type RoCrateHandler,
14
- } from './app.js';
15
- import type { Entity, File } from './generated/prisma/client.js';
16
- import { PrismaClient } from './generated/prisma/client.js';
17
-
18
- const adapter = new PrismaMariaDb({
19
- host: 'localhost',
20
- user: 'root',
21
- password: 'password',
22
- database: 'catalog',
23
- port: 3306,
24
- connectionLimit: 5,
25
- // NOTE: This is required when using the default MariaDB server configuration with SSL disabled. In production, you should properly configure SSL and remove this option.
26
- allowPublicKeyRetrieval: true,
27
- });
28
- const prisma = new PrismaClient({ adapter });
29
-
30
- if (!process.env.OPENSEARCH_URL) {
31
- throw new Error('OPENSEARCH_URL environment variable is not set');
32
- }
33
- const opensearchUrl = process.env.OPENSEARCH_URL;
34
- const opensearch = new Client({ node: opensearchUrl });
35
-
36
- const fileHandler: FileHandler = {
37
- head: async (file: File) => {
38
- const filePath = (file.meta as { storagePath: string }).storagePath;
39
-
40
- if (!existsSync(filePath)) {
41
- return false;
42
- }
43
-
44
- const stats = statSync(filePath);
45
-
46
- return {
47
- contentType: file.mediaType,
48
- contentLength: Number(file.size),
49
- lastModified: stats.mtime,
50
- };
51
- },
52
- get: async (file: File) => {
53
- const filePath = (file.meta as { storagePath: string }).storagePath;
54
-
55
- if (!existsSync(filePath)) {
56
- return false;
57
- }
58
-
59
- const stats = statSync(filePath);
60
-
61
- return {
62
- type: 'stream',
63
- stream: createReadStream(filePath),
64
- metadata: {
65
- contentType: file.mediaType,
66
- contentLength: Number(file.size),
67
- lastModified: stats.mtime,
68
- },
69
- };
70
- },
71
- };
72
-
73
- const transformRoCrate = (filePath: string, remapRootTo: string): { rocrate: unknown; jsonString: string } | false => {
74
- const fileContents = readFileSync(filePath, 'utf-8');
75
- const rocrate = JSON.parse(fileContents) as {
76
- '@context'?: unknown;
77
- '@graph'?: Array<{ '@id': string; [key: string]: unknown }>;
78
- [key: string]: unknown;
79
- };
80
-
81
- const graph = rocrate['@graph'] || [];
82
- const rootNode = graph.find((node) => node['@id'] === 'ro-crate-metadata.json') as
83
- | { about: { '@id': string } }
84
- | undefined;
85
-
86
- if (!rootNode) {
87
- return false;
88
- }
89
-
90
- rootNode.about['@id'] = remapRootTo;
91
-
92
- const jsonString = JSON.stringify(rocrate);
93
-
94
- return { rocrate, jsonString };
95
- };
96
-
97
- const roCrateHandler: RoCrateHandler = {
98
- head: async (entity: Entity) => {
99
- const meta = entity.meta as { storagePath: string; remapRootTo: string };
100
- const filePath = meta.storagePath;
101
-
102
- if (!existsSync(filePath)) {
103
- return false;
104
- }
105
-
106
- if (meta.remapRootTo) {
107
- const result = transformRoCrate(filePath, meta.remapRootTo);
108
-
109
- if (!result) {
110
- return false;
111
- }
112
-
113
- return {
114
- contentType: 'application/ld+json',
115
- contentLength: Buffer.byteLength(result.jsonString),
116
- lastModified: statSync(filePath).mtime,
117
- };
118
- }
119
-
120
- const stats = statSync(filePath);
121
-
122
- return {
123
- contentType: 'application/ld+json',
124
- contentLength: stats.size,
125
- lastModified: stats.mtime,
126
- };
127
- },
128
- get: async (entity: Entity) => {
129
- const meta = entity.meta as { storagePath: string; remapRootTo: string };
130
- const filePath = meta.storagePath;
131
-
132
- if (!existsSync(filePath)) {
133
- return false;
134
- }
135
-
136
- if (meta.remapRootTo) {
137
- const result = transformRoCrate(filePath, meta.remapRootTo);
138
-
139
- if (!result) {
140
- return false;
141
- }
142
-
143
- return {
144
- type: 'stream',
145
- stream: Readable.from(result.jsonString),
146
- metadata: {
147
- contentType: 'application/ld+json',
148
- contentLength: Buffer.byteLength(result.jsonString),
149
- lastModified: statSync(filePath).mtime,
150
- },
151
- };
152
- }
153
-
154
- const stats = statSync(filePath);
155
-
156
- return {
157
- type: 'stream',
158
- stream: createReadStream(filePath),
159
- metadata: {
160
- contentType: 'application/ld+json',
161
- contentLength: stats.size,
162
- lastModified: stats.mtime,
163
- },
164
- };
165
- },
166
- };
167
-
168
- const oniTransformer: EntityTransformer = (entity) => {
169
- return {
170
- ...entity,
171
- accessControl: 'Public',
172
- counts: {
173
- collections: 0,
174
- objects: 0,
175
- files: 0,
176
- },
177
- };
178
- };
179
-
180
- const fastify = Fastify({
181
- logger: true,
182
- });
183
- fastify.register(arocapi, {
184
- prisma,
185
- opensearch,
186
- fileHandler,
187
- roCrateHandler,
188
- access: AllPublicAccess,
189
- entityTransformers: [oniTransformer],
190
- entityVisibilityFilter: AllVisibleEntityFilter,
191
- searchVisibilityFilter: AllVisibleSearchFilter,
192
- });
193
-
194
- const start = async () => {
195
- try {
196
- await fastify.listen({ port: 9000 });
197
- } catch (err) {
198
- fastify.log.error(err);
199
- process.exit(1);
200
- }
201
- };
202
- start();
package/src/reset.d.ts DELETED
@@ -1 +0,0 @@
1
- import '@total-typescript/ts-reset';
@@ -1,34 +0,0 @@
1
- // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
-
3
- exports[`Capabilities Route > GET /capabilities > should declare the default capabilities 1`] = `
4
- {
5
- "apiVersion": "0.4.0",
6
- "deposit": {
7
- "supported": false,
8
- },
9
- "extensions": {},
10
- "search": {
11
- "facets": {
12
- "communicationMode": {},
13
- "entityType": {},
14
- "inLanguage": {},
15
- "mediaType": {},
16
- },
17
- "filters": {
18
- "communicationMode": {
19
- "type": "string",
20
- },
21
- "entityType": {
22
- "type": "string",
23
- },
24
- "inLanguage": {
25
- "type": "string",
26
- },
27
- "mediaType": {
28
- "type": "string",
29
- },
30
- },
31
- },
32
- "tombstonePolicy": "404",
33
- }
34
- `;
@@ -1,96 +0,0 @@
1
- // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
-
3
- exports[`Entities Route > GET /entities > should apply custom entity transformers 1`] = `
4
- {
5
- "entities": [
6
- {
7
- "access": {
8
- "content": true,
9
- "metadata": true,
10
- },
11
- "contentLicenseId": "https://creativecommons.org/licenses/by/4.0/",
12
- "description": "First test entity",
13
- "entityType": "http://pcdm.org/models#Collection",
14
- "id": "http://example.com/entity/1",
15
- "memberOf": null,
16
- "metadataLicenseId": "https://creativecommons.org/licenses/by/4.0/",
17
- "name": "Test Entity 1",
18
- "rootCollection": null,
19
- "tested": true,
20
- },
21
- ],
22
- }
23
- `;
24
-
25
- exports[`Entities Route > GET /entities > should return 500 when database error occurs 1`] = `
26
- {
27
- "error": {
28
- "code": "INTERNAL_ERROR",
29
- "message": "Internal server error",
30
- },
31
- }
32
- `;
33
-
34
- exports[`Entities Route > GET /entities > should return entities with default pagination 1`] = `
35
- {
36
- "entities": [
37
- {
38
- "access": {
39
- "content": true,
40
- "metadata": true,
41
- },
42
- "contentLicenseId": "https://creativecommons.org/licenses/by/4.0/",
43
- "description": "First test entity",
44
- "entityType": "http://pcdm.org/models#Collection",
45
- "id": "http://example.com/entity/1",
46
- "memberOf": null,
47
- "metadataLicenseId": "https://creativecommons.org/licenses/by/4.0/",
48
- "name": "Test Entity 1",
49
- "rootCollection": null,
50
- },
51
- {
52
- "access": {
53
- "content": true,
54
- "metadata": true,
55
- },
56
- "contentLicenseId": "https://creativecommons.org/licenses/by/4.0/",
57
- "description": "Second test entity",
58
- "entityType": "http://pcdm.org/models#Object",
59
- "id": "http://example.com/entity/2",
60
- "memberOf": {
61
- "id": "http://example.com/entity/1",
62
- "name": "Test Entity 1",
63
- },
64
- "metadataLicenseId": "https://creativecommons.org/licenses/by/4.0/",
65
- "name": "Test Entity 2",
66
- "rootCollection": {
67
- "id": "http://example.com/entity/1",
68
- "name": "Test Entity 1",
69
- },
70
- },
71
- ],
72
- "total": 2,
73
- }
74
- `;
75
-
76
- exports[`Entities Route > GET /entities > should return null for memberOf/rootCollection when parent entity not found 1`] = `
77
- {
78
- "entities": [
79
- {
80
- "access": {
81
- "content": true,
82
- "metadata": true,
83
- },
84
- "contentLicenseId": "https://creativecommons.org/licenses/by/4.0/",
85
- "description": "Entity with missing parent",
86
- "entityType": "http://pcdm.org/models#Object",
87
- "id": "http://example.com/entity/1",
88
- "memberOf": null,
89
- "metadataLicenseId": "https://creativecommons.org/licenses/by/4.0/",
90
- "name": "Test Entity 1",
91
- "rootCollection": null,
92
- },
93
- ],
94
- "total": 1,
95
- }
96
- `;
@@ -1,74 +0,0 @@
1
- // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
-
3
- exports[`Entity Route > GET /entity/:id > should apply custom entity transformers 1`] = `
4
- {
5
- "access": {
6
- "content": true,
7
- "metadata": true,
8
- },
9
- "contentLicenseId": "https://creativecommons.org/licenses/by/4.0/",
10
- "description": "A test entity",
11
- "entityType": "http://pcdm.org/models#Collection",
12
- "id": "http://example.com/entity/123",
13
- "memberOf": null,
14
- "metadataLicenseId": "https://creativecommons.org/licenses/by/4.0/",
15
- "name": "Test Entity",
16
- "rootCollection": null,
17
- "tested": true,
18
- }
19
- `;
20
-
21
- exports[`Entity Route > GET /entity/:id > should return 404 when entity not found 1`] = `
22
- {
23
- "error": {
24
- "code": "NOT_FOUND",
25
- "details": {
26
- "entityId": "http://example.com/entity/nonexistent",
27
- },
28
- "message": "The requested entity was not found",
29
- },
30
- }
31
- `;
32
-
33
- exports[`Entity Route > GET /entity/:id > should return 500 when database error occurs 1`] = `
34
- {
35
- "error": {
36
- "code": "INTERNAL_ERROR",
37
- "message": "Internal server error",
38
- },
39
- }
40
- `;
41
-
42
- exports[`Entity Route > GET /entity/:id > should return entity when found 1`] = `
43
- {
44
- "access": {
45
- "content": true,
46
- "metadata": true,
47
- },
48
- "contentLicenseId": "https://creativecommons.org/licenses/by/4.0/",
49
- "description": "A test entity",
50
- "entityType": "http://schema.org/Person",
51
- "id": "http://example.com/entity/123",
52
- "memberOf": null,
53
- "metadataLicenseId": "https://creativecommons.org/licenses/by/4.0/",
54
- "name": "Test Entity",
55
- "rootCollection": null,
56
- }
57
- `;
58
-
59
- exports[`Entity Route > GET /entity/:id > should return null for memberOf/rootCollection when parent entity not found 1`] = `
60
- {
61
- "access": {
62
- "content": true,
63
- "metadata": true,
64
- },
65
- "contentLicenseId": "https://creativecommons.org/licenses/by/4.0/",
66
- "description": "A test entity",
67
- "entityType": "http://pcdm.org/models#Object",
68
- "id": "http://example.com/entity/123",
69
- "memberOf": null,
70
- "metadataLicenseId": "https://creativecommons.org/licenses/by/4.0/",
71
- "name": "Test Entity",
72
- "rootCollection": null,
73
- }
74
- `;
@@ -1,165 +0,0 @@
1
- // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
-
3
- exports[`Search Route > POST /search > should apply custom entity transformers 1`] = `
4
- {
5
- "entities": [
6
- {
7
- "access": {
8
- "content": true,
9
- "metadata": true,
10
- },
11
- "contentLicenseId": null,
12
- "description": "A test entity",
13
- "entityType": "http://pcdm.org/models#Collection",
14
- "id": "http://example.com/entity/1",
15
- "memberOf": null,
16
- "metadataLicenseId": null,
17
- "name": "Test Entity 1",
18
- "rootCollection": null,
19
- "searchExtra": {
20
- "highlight": {},
21
- "score": 1.5,
22
- },
23
- "tested": true,
24
- },
25
- ],
26
- "searchTime": 10,
27
- "total": 1,
28
- }
29
- `;
30
-
31
- exports[`Search Route > POST /search > should handle id explicitly set to undefined 1`] = `
32
- {
33
- "error": {
34
- "code": "INTERNAL_ERROR",
35
- "message": "Internal server error",
36
- },
37
- }
38
- `;
39
-
40
- exports[`Search Route > POST /search > should handle opensearch errors 1`] = `
41
- {
42
- "error": {
43
- "code": "INTERNAL_ERROR",
44
- "message": "Internal server error",
45
- },
46
- }
47
- `;
48
-
49
- exports[`Search Route > POST /search > should perform basic search successfully 1`] = `
50
- {
51
- "entities": [
52
- {
53
- "access": {
54
- "content": true,
55
- "metadata": true,
56
- },
57
- "contentLicenseId": null,
58
- "description": "A test entity",
59
- "entityType": "http://pcdm.org/models#Collection",
60
- "id": "http://example.com/entity/1",
61
- "memberOf": null,
62
- "metadataLicenseId": null,
63
- "name": "Test Entity 1",
64
- "rootCollection": null,
65
- "searchExtra": {
66
- "highlight": {
67
- "name": [
68
- "<em>Test</em> Entity 1",
69
- ],
70
- },
71
- "score": 1.5,
72
- },
73
- },
74
- {
75
- "access": {
76
- "content": true,
77
- "metadata": true,
78
- },
79
- "contentLicenseId": null,
80
- "description": "Another test entity",
81
- "entityType": "http://pcdm.org/models#Object",
82
- "id": "http://example.com/entity/2",
83
- "memberOf": null,
84
- "metadataLicenseId": null,
85
- "name": "Test Entity 2",
86
- "rootCollection": null,
87
- "searchExtra": {
88
- "highlight": {
89
- "description": [
90
- "Another <em>test</em> entity",
91
- ],
92
- },
93
- "score": 1.2,
94
- },
95
- },
96
- ],
97
- "facets": {
98
- "entityType": [
99
- {
100
- "count": 1,
101
- "name": "http://pcdm.org/models#Collection",
102
- },
103
- {
104
- "count": 1,
105
- "name": "http://pcdm.org/models#Object",
106
- },
107
- ],
108
- },
109
- "searchTime": 10,
110
- "total": 2,
111
- }
112
- `;
113
-
114
- exports[`Search Route > POST /search > should return null for memberOf/rootCollection when parent entity not found 1`] = `
115
- {
116
- "entities": [
117
- {
118
- "access": {
119
- "content": true,
120
- "metadata": true,
121
- },
122
- "contentLicenseId": "https://creativecommons.org/licenses/by/4.0/",
123
- "description": "Entity with missing parent",
124
- "entityType": "http://pcdm.org/models#Object",
125
- "id": "http://example.com/entity/1",
126
- "memberOf": null,
127
- "metadataLicenseId": "https://creativecommons.org/licenses/by/4.0/",
128
- "name": "Test Entity 1",
129
- "rootCollection": null,
130
- "searchExtra": {
131
- "score": 1.5,
132
- },
133
- },
134
- ],
135
- "searchTime": 10,
136
- "total": 1,
137
- }
138
- `;
139
-
140
- exports[`Search Route > POST /search > should skip entities not found in database and log warning 1`] = `
141
- {
142
- "entities": [
143
- {
144
- "access": {
145
- "content": true,
146
- "metadata": true,
147
- },
148
- "contentLicenseId": null,
149
- "description": "A test entity",
150
- "entityType": "http://pcdm.org/models#Collection",
151
- "id": "http://example.com/entity/1",
152
- "memberOf": null,
153
- "metadataLicenseId": null,
154
- "name": "Test Entity 1",
155
- "rootCollection": null,
156
- "searchExtra": {
157
- "highlight": {},
158
- "score": 1.5,
159
- },
160
- },
161
- ],
162
- "searchTime": 10,
163
- "total": 2,
164
- }
165
- `;
@@ -1,102 +0,0 @@
1
- import { afterEach, describe, expect, it, vi } from 'vitest';
2
-
3
- import app, { AllPublicAccess, AllVisibleEntityFilter, AllVisibleSearchFilter, type Options } from '../app.js';
4
- import { fastify, fastifyAfter, fastifyBefore, opensearch, prisma } from '../test/helpers/fastify.js';
5
- import { OpensearchQueryBuilder, type SearchCapability } from '../utils/queryBuilder.js';
6
-
7
- const registerApp = async (options: Partial<Options> = {}) => {
8
- await fastifyBefore();
9
- await fastify.register(app, {
10
- prisma,
11
- opensearch,
12
- access: AllPublicAccess,
13
- fileHandler: { get: vi.fn(), head: vi.fn() },
14
- roCrateHandler: { get: vi.fn(), head: vi.fn() },
15
- entityVisibilityFilter: AllVisibleEntityFilter,
16
- searchVisibilityFilter: AllVisibleSearchFilter,
17
- ...options,
18
- });
19
- };
20
-
21
- const getCapabilities = async () => {
22
- const response = await fastify.inject({ method: 'GET', url: '/capabilities' });
23
-
24
- return { statusCode: response.statusCode, body: response.json<{ search: SearchCapability }>() };
25
- };
26
-
27
- describe('Capabilities Route', () => {
28
- afterEach(async () => {
29
- await fastifyAfter();
30
- });
31
-
32
- describe('GET /capabilities', () => {
33
- it('should declare the default capabilities', async () => {
34
- await registerApp();
35
-
36
- const { statusCode, body } = await getCapabilities();
37
-
38
- expect(statusCode).toBe(200);
39
- expect(body).toMatchSnapshot();
40
- });
41
-
42
- it('should declare every facet as a filter', async () => {
43
- await registerApp();
44
-
45
- const { body } = await getCapabilities();
46
-
47
- expect(Object.keys(body.search.filters)).toEqual(expect.arrayContaining(Object.keys(body.search.facets)));
48
- });
49
-
50
- it('should declare only terms aggregations', async () => {
51
- await registerApp({
52
- queryBuilderOptions: {
53
- aggregations: {
54
- inLanguage: { terms: { size: 20 } },
55
- dateCreated: { date_histogram: { field: 'dateCreated', calendar_interval: 'year' } },
56
- },
57
- },
58
- });
59
-
60
- const { body } = await getCapabilities();
61
-
62
- expect(body.search).toEqual({
63
- filters: { inLanguage: { type: 'string' } },
64
- facets: { inLanguage: {} },
65
- });
66
- });
67
-
68
- it('should use an overridden searchCapabilities', async () => {
69
- const labelled: SearchCapability = {
70
- filters: {
71
- inLanguage: { type: 'string', label: 'Language' },
72
- dateCreated: { type: 'date', label: 'Date created' },
73
- },
74
- facets: { inLanguage: { label: 'Language' } },
75
- };
76
- class LabelledQueryBuilder extends OpensearchQueryBuilder {
77
- searchCapabilities() {
78
- return labelled;
79
- }
80
- }
81
-
82
- await registerApp({ queryBuilderClass: LabelledQueryBuilder });
83
-
84
- const { body } = await getCapabilities();
85
-
86
- expect(body.search).toEqual(labelled);
87
- });
88
-
89
- it('should not run the access function', async () => {
90
- const access = vi.fn(() => {
91
- throw new Error('Unauthorised');
92
- });
93
-
94
- await registerApp({ access });
95
-
96
- const { statusCode } = await getCapabilities();
97
-
98
- expect(statusCode).toBe(200);
99
- expect(access).not.toHaveBeenCalled();
100
- });
101
- });
102
- });