@abyss-project/reflex 6.4.0 → 6.6.0
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.
- package/.eslintrc.js +32 -32
- package/.prettierrc +5 -5
- package/README.md +15 -15
- package/dist/api/function-run.api.d.ts +2 -1
- package/dist/api/function-run.api.js +3 -1
- package/dist/api/function-run.api.js.map +1 -1
- package/dist/discord/discord-command-tree.d.ts +5 -0
- package/dist/discord/discord-command-tree.js +38 -0
- package/dist/discord/discord-command-tree.js.map +1 -0
- package/dist/discord/discord-event-filter-cases.js +27 -11
- package/dist/discord/discord-event-filter-cases.js.map +1 -1
- package/dist/discord/discord-event-filters.js +7 -3
- package/dist/discord/discord-event-filters.js.map +1 -1
- package/dist/discord/discord-event-intents.d.ts +8 -2
- package/dist/discord/discord-event-intents.js +13 -2
- package/dist/discord/discord-event-intents.js.map +1 -1
- package/dist/discord/index.d.ts +1 -0
- package/dist/discord/index.js +1 -0
- package/dist/discord/index.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/types/enum/discord-command-node-type.enum.d.ts +5 -0
- package/dist/types/enum/discord-command-node-type.enum.js +10 -0
- package/dist/types/enum/discord-command-node-type.enum.js.map +1 -0
- package/dist/types/enum/discord-command-type.enum.d.ts +5 -0
- package/dist/types/enum/discord-command-type.enum.js +10 -0
- package/dist/types/enum/discord-command-type.enum.js.map +1 -0
- package/dist/types/enum/discord-event-type.enum.d.ts +3 -1
- package/dist/types/enum/discord-event-type.enum.js +2 -0
- package/dist/types/enum/discord-event-type.enum.js.map +1 -1
- package/dist/types/enum/index.d.ts +2 -0
- package/dist/types/enum/index.js +2 -0
- package/dist/types/enum/index.js.map +1 -1
- package/dist/types/enum/reflex-error-code.enum.d.ts +10 -0
- package/dist/types/enum/reflex-error-code.enum.js +10 -0
- package/dist/types/enum/reflex-error-code.enum.js.map +1 -1
- package/dist/types/interface/api/requests/function-run.request.d.ts +8 -0
- package/dist/types/interface/api/requests/service-discord.request.d.ts +7 -3
- package/dist/types/interface/api/responses/function-run.response.d.ts +8 -0
- package/dist/types/interface/models/reflex-discord.model.d.ts +18 -2
- package/dist/types/interface/models/reflex-discord.model.js +4 -0
- package/dist/types/interface/models/reflex-discord.model.js.map +1 -1
- package/dist/types/interface/models/reflex-run.model.d.ts +4 -0
- package/dist/types/interface/models/reflex-run.model.js +4 -1
- package/dist/types/interface/models/reflex-run.model.js.map +1 -1
- package/jest.config.ts +11 -11
- package/package.json +1 -1
- package/src/api/alert-rule.api.test.ts +72 -72
- package/src/api/alert-rule.api.ts +57 -57
- package/src/api/application-insights.api.test.ts +42 -42
- package/src/api/application-insights.api.ts +38 -38
- package/src/api/application-run-settings.api.test.ts +62 -62
- package/src/api/application-run-settings.api.ts +30 -30
- package/src/api/function-access.api.ts +87 -87
- package/src/api/function-insights.api.test.ts +27 -27
- package/src/api/function-insights.api.ts +18 -18
- package/src/api/function-run.api.ts +19 -0
- package/src/api/function-settings.api.ts +96 -96
- package/src/api/function-template.api.test.ts +72 -72
- package/src/api/function-template.api.ts +52 -52
- package/src/api/function-test.api.ts +34 -34
- package/src/api/function-version.api.ts +66 -66
- package/src/api/function.api.test.ts +346 -346
- package/src/api/function.api.ts +62 -62
- package/src/api/health.api.ts +10 -10
- package/src/api/route-stats.api.test.ts +45 -45
- package/src/api/route-stats.api.ts +36 -36
- package/src/api/service-completion-webhook.api.test.ts +97 -97
- package/src/api/service-completion-webhook.api.ts +39 -39
- package/src/api/service-configuration.api.ts +16 -16
- package/src/api/service-consumer.api.test.ts +104 -104
- package/src/api/service-consumer.api.ts +90 -90
- package/src/api/service-deployment.api.ts +79 -79
- package/src/api/service-discord-event.api.test.ts +106 -106
- package/src/api/service-discord-event.api.ts +78 -78
- package/src/api/service-discord.api.test.ts +178 -178
- package/src/api/service-discord.api.ts +222 -222
- package/src/api/service-insights.api.test.ts +36 -36
- package/src/api/service-insights.api.ts +42 -42
- package/src/api/service-limits.api.test.ts +100 -100
- package/src/api/service-limits.api.ts +37 -37
- package/src/api/service-openapi.api.test.ts +52 -52
- package/src/api/service-openapi.api.ts +21 -21
- package/src/api/service-route.api.ts +74 -74
- package/src/api/service-task.api.test.ts +140 -140
- package/src/api/service-task.api.ts +141 -141
- package/src/api/service-version.api.ts +54 -54
- package/src/api/service.api.test.ts +220 -220
- package/src/api/service.api.ts +55 -55
- package/src/api/storage-surplus.admin.api.test.ts +66 -66
- package/src/api/storage-surplus.admin.api.ts +45 -45
- package/src/discord/discord-command-tree.test.ts +47 -0
- package/src/discord/discord-command-tree.ts +60 -0
- package/src/discord/discord-event-filter-cases.ts +299 -261
- package/src/discord/discord-event-filters.test.ts +120 -91
- package/src/discord/discord-event-filters.ts +103 -95
- package/src/discord/discord-event-intents.test.ts +139 -112
- package/src/discord/discord-event-intents.ts +80 -55
- package/src/discord/index.ts +4 -3
- package/src/index.test.ts +104 -104
- package/src/index.ts +2 -0
- package/src/insights/index.ts +1 -1
- package/src/insights/percentiles.test.ts +75 -75
- package/src/insights/percentiles.ts +37 -37
- package/src/types/enum/discord-command-node-type.enum.ts +10 -0
- package/src/types/enum/discord-command-type.enum.ts +9 -0
- package/src/types/enum/discord-event-type.enum.ts +4 -0
- package/src/types/enum/discord-host-action.enum.ts +37 -37
- package/src/types/enum/index.ts +2 -0
- package/src/types/enum/reflex-error-code.enum.ts +112 -89
- package/src/types/interface/api/requests/application-run-settings.request.ts +13 -13
- package/src/types/interface/api/requests/function-run.request.ts +53 -42
- package/src/types/interface/api/requests/function-settings.request.ts +50 -50
- package/src/types/interface/api/requests/index.ts +25 -25
- package/src/types/interface/api/requests/service-discord.request.ts +168 -155
- package/src/types/interface/api/requests/service.request.ts +48 -48
- package/src/types/interface/api/requests/storage-surplus.admin.request.ts +12 -12
- package/src/types/interface/api/responses/function-run.response.ts +35 -23
- package/src/types/interface/api/responses/function-settings.response.ts +57 -57
- package/src/types/interface/api/responses/index.ts +25 -25
- package/src/types/interface/api/responses/storage-surplus.admin.response.ts +10 -10
- package/src/types/interface/models/index.ts +29 -29
- package/src/types/interface/models/reflex-discord.model.ts +249 -208
- package/src/types/interface/models/reflex-price-list.model.ts +22 -22
- package/src/types/interface/models/reflex-run-retention.model.ts +24 -24
- package/src/types/interface/models/reflex-run-settings.model.ts +39 -39
- package/src/types/interface/models/reflex-run.model.ts +317 -309
- package/src/types/interface/models/reflex-service-deployment.model.ts +48 -48
- package/src/types/interface/models/reflex-service.model.ts +47 -47
- package/src/types/interface/models/reflex-storage-surplus.model.ts +23 -23
- package/tsconfig.check.json +15 -15
- package/tsconfig.json +35 -35
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
import { AxiosAdapter, InternalAxiosRequestConfig } from 'axios';
|
|
2
|
-
import { ADMIN_TOKEN_HEADER, AbyssReflexCore, ReflexErrorCode } from '..';
|
|
3
|
-
|
|
4
|
-
const OWNER = { organizationId: 'org-1' };
|
|
5
|
-
const BODY = { owner: OWNER, itemIds: ['day-1'] };
|
|
6
|
-
const BASE = 'storage-surplus/admin';
|
|
7
|
-
|
|
8
|
-
const surplus = () => AbyssReflexCore.sdk.storageSurplus.admin;
|
|
9
|
-
|
|
10
|
-
describe('AbyssReflexCore storage surplus routes (day 90 of abyss-main)', () => {
|
|
11
|
-
const adapter = jest.fn<ReturnType<AxiosAdapter>, [InternalAxiosRequestConfig]>(
|
|
12
|
-
async (config) => ({
|
|
13
|
-
data: { data: { items: [] } },
|
|
14
|
-
status: 200,
|
|
15
|
-
statusText: '',
|
|
16
|
-
headers: {},
|
|
17
|
-
config,
|
|
18
|
-
}),
|
|
19
|
-
);
|
|
20
|
-
|
|
21
|
-
beforeAll(() => {
|
|
22
|
-
AbyssReflexCore.setConfig({ baseURL: 'http://reflex.test/', adminToken: 'mesh-admin-token' });
|
|
23
|
-
AbyssReflexCore.axios.defaults.adapter = adapter;
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
beforeEach(() => adapter.mockClear());
|
|
27
|
-
|
|
28
|
-
it.each([
|
|
29
|
-
[
|
|
30
|
-
'listArchivable',
|
|
31
|
-
`${BASE}/archivable`,
|
|
32
|
-
{ owner: OWNER },
|
|
33
|
-
() => surplus().listArchivable({ owner: OWNER }),
|
|
34
|
-
],
|
|
35
|
-
['archive', `${BASE}/archive`, BODY, () => surplus().archive(BODY)],
|
|
36
|
-
[
|
|
37
|
-
'listArchives',
|
|
38
|
-
`${BASE}/archives`,
|
|
39
|
-
{ owner: OWNER },
|
|
40
|
-
() => surplus().listArchives({ owner: OWNER }),
|
|
41
|
-
],
|
|
42
|
-
['deleteArchives', `${BASE}/delete-archives`, BODY, () => surplus().deleteArchives(BODY)],
|
|
43
|
-
])('%s posts to %s with the body and the admin token', async (_label, url, body, run) => {
|
|
44
|
-
const answer = await run();
|
|
45
|
-
|
|
46
|
-
const config = adapter.mock.calls[0][0];
|
|
47
|
-
expect(config.method).toBe('post');
|
|
48
|
-
expect(config.url).toBe(url);
|
|
49
|
-
expect(config.baseURL).toBe('http://reflex.test/');
|
|
50
|
-
expect(JSON.parse(config.data)).toEqual(body);
|
|
51
|
-
expect(config.headers.get(ADMIN_TOKEN_HEADER)).toBe('mesh-admin-token');
|
|
52
|
-
expect(answer).toEqual({ data: { items: [] } });
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it('names the header of the mesh admin token', () => {
|
|
56
|
-
expect(ADMIN_TOKEN_HEADER).toBe('abyss-admin-token');
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it('names the refusal of a publication past the storage quota', () => {
|
|
60
|
-
expect(ReflexErrorCode.STORAGE_PUBLISH_REFUSED).toBe('STORAGE_PUBLISH_REFUSED');
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('names the 404 of a run whose archive was deleted', () => {
|
|
64
|
-
expect(ReflexErrorCode.ARCHIVE_DELETED).toBe('ARCHIVE_DELETED');
|
|
65
|
-
});
|
|
66
|
-
});
|
|
1
|
+
import { AxiosAdapter, InternalAxiosRequestConfig } from 'axios';
|
|
2
|
+
import { ADMIN_TOKEN_HEADER, AbyssReflexCore, ReflexErrorCode } from '..';
|
|
3
|
+
|
|
4
|
+
const OWNER = { organizationId: 'org-1' };
|
|
5
|
+
const BODY = { owner: OWNER, itemIds: ['day-1'] };
|
|
6
|
+
const BASE = 'storage-surplus/admin';
|
|
7
|
+
|
|
8
|
+
const surplus = () => AbyssReflexCore.sdk.storageSurplus.admin;
|
|
9
|
+
|
|
10
|
+
describe('AbyssReflexCore storage surplus routes (day 90 of abyss-main)', () => {
|
|
11
|
+
const adapter = jest.fn<ReturnType<AxiosAdapter>, [InternalAxiosRequestConfig]>(
|
|
12
|
+
async (config) => ({
|
|
13
|
+
data: { data: { items: [] } },
|
|
14
|
+
status: 200,
|
|
15
|
+
statusText: '',
|
|
16
|
+
headers: {},
|
|
17
|
+
config,
|
|
18
|
+
}),
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
beforeAll(() => {
|
|
22
|
+
AbyssReflexCore.setConfig({ baseURL: 'http://reflex.test/', adminToken: 'mesh-admin-token' });
|
|
23
|
+
AbyssReflexCore.axios.defaults.adapter = adapter;
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
beforeEach(() => adapter.mockClear());
|
|
27
|
+
|
|
28
|
+
it.each([
|
|
29
|
+
[
|
|
30
|
+
'listArchivable',
|
|
31
|
+
`${BASE}/archivable`,
|
|
32
|
+
{ owner: OWNER },
|
|
33
|
+
() => surplus().listArchivable({ owner: OWNER }),
|
|
34
|
+
],
|
|
35
|
+
['archive', `${BASE}/archive`, BODY, () => surplus().archive(BODY)],
|
|
36
|
+
[
|
|
37
|
+
'listArchives',
|
|
38
|
+
`${BASE}/archives`,
|
|
39
|
+
{ owner: OWNER },
|
|
40
|
+
() => surplus().listArchives({ owner: OWNER }),
|
|
41
|
+
],
|
|
42
|
+
['deleteArchives', `${BASE}/delete-archives`, BODY, () => surplus().deleteArchives(BODY)],
|
|
43
|
+
])('%s posts to %s with the body and the admin token', async (_label, url, body, run) => {
|
|
44
|
+
const answer = await run();
|
|
45
|
+
|
|
46
|
+
const config = adapter.mock.calls[0][0];
|
|
47
|
+
expect(config.method).toBe('post');
|
|
48
|
+
expect(config.url).toBe(url);
|
|
49
|
+
expect(config.baseURL).toBe('http://reflex.test/');
|
|
50
|
+
expect(JSON.parse(config.data)).toEqual(body);
|
|
51
|
+
expect(config.headers.get(ADMIN_TOKEN_HEADER)).toBe('mesh-admin-token');
|
|
52
|
+
expect(answer).toEqual({ data: { items: [] } });
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('names the header of the mesh admin token', () => {
|
|
56
|
+
expect(ADMIN_TOKEN_HEADER).toBe('abyss-admin-token');
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('names the refusal of a publication past the storage quota', () => {
|
|
60
|
+
expect(ReflexErrorCode.STORAGE_PUBLISH_REFUSED).toBe('STORAGE_PUBLISH_REFUSED');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('names the 404 of a run whose archive was deleted', () => {
|
|
64
|
+
expect(ReflexErrorCode.ARCHIVE_DELETED).toBe('ARCHIVE_DELETED');
|
|
65
|
+
});
|
|
66
|
+
});
|
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
import { AbyssReflexCore } from '..';
|
|
2
|
-
import type {
|
|
3
|
-
IApplyStorageSurplusAdminBody,
|
|
4
|
-
IApplyStorageSurplusAdminResponse,
|
|
5
|
-
IListStorageSurplusAdminBody,
|
|
6
|
-
IListStorageSurplusAdminResponse,
|
|
7
|
-
} from '../types';
|
|
8
|
-
|
|
9
|
-
const BASE = 'storage-surplus/admin';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* `POST /storage-surplus/admin/archivable`: the days of runs of an organization that can move to the archive now. For
|
|
13
|
-
* abyss-main only: behind the admin token (`setConfig({ adminToken })`). AbyssReflex lists nothing for the surplus of
|
|
14
|
-
* the ONLINE quota (its runs are not in it): the answer is always empty.
|
|
15
|
-
*/
|
|
16
|
-
export const listArchivableStorageSurplusAdmin = async (
|
|
17
|
-
body: IListStorageSurplusAdminBody,
|
|
18
|
-
): Promise<IListStorageSurplusAdminResponse> =>
|
|
19
|
-
(await AbyssReflexCore.axios.post<IListStorageSurplusAdminResponse>(`${BASE}/archivable`, body))
|
|
20
|
-
.data;
|
|
21
|
-
|
|
22
|
-
/** `POST /storage-surplus/admin/archive`: moves the given days to the archive; idempotent. Admin token. */
|
|
23
|
-
export const archiveStorageSurplusAdmin = async (
|
|
24
|
-
body: IApplyStorageSurplusAdminBody,
|
|
25
|
-
): Promise<IApplyStorageSurplusAdminResponse> =>
|
|
26
|
-
(await AbyssReflexCore.axios.post<IApplyStorageSurplusAdminResponse>(`${BASE}/archive`, body))
|
|
27
|
-
.data;
|
|
28
|
-
|
|
29
|
-
/** `POST /storage-surplus/admin/archives`: the archived days of the organization, the oldest first. Admin token. */
|
|
30
|
-
export const listArchivesStorageSurplusAdmin = async (
|
|
31
|
-
body: IListStorageSurplusAdminBody,
|
|
32
|
-
): Promise<IListStorageSurplusAdminResponse> =>
|
|
33
|
-
(await AbyssReflexCore.axios.post<IListStorageSurplusAdminResponse>(`${BASE}/archives`, body))
|
|
34
|
-
.data;
|
|
35
|
-
|
|
36
|
-
/** `POST /storage-surplus/admin/delete-archives`: deletes the given archived days for good; idempotent. Admin token. */
|
|
37
|
-
export const deleteArchivesStorageSurplusAdmin = async (
|
|
38
|
-
body: IApplyStorageSurplusAdminBody,
|
|
39
|
-
): Promise<IApplyStorageSurplusAdminResponse> =>
|
|
40
|
-
(
|
|
41
|
-
await AbyssReflexCore.axios.post<IApplyStorageSurplusAdminResponse>(
|
|
42
|
-
`${BASE}/delete-archives`,
|
|
43
|
-
body,
|
|
44
|
-
)
|
|
45
|
-
).data;
|
|
1
|
+
import { AbyssReflexCore } from '..';
|
|
2
|
+
import type {
|
|
3
|
+
IApplyStorageSurplusAdminBody,
|
|
4
|
+
IApplyStorageSurplusAdminResponse,
|
|
5
|
+
IListStorageSurplusAdminBody,
|
|
6
|
+
IListStorageSurplusAdminResponse,
|
|
7
|
+
} from '../types';
|
|
8
|
+
|
|
9
|
+
const BASE = 'storage-surplus/admin';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* `POST /storage-surplus/admin/archivable`: the days of runs of an organization that can move to the archive now. For
|
|
13
|
+
* abyss-main only: behind the admin token (`setConfig({ adminToken })`). AbyssReflex lists nothing for the surplus of
|
|
14
|
+
* the ONLINE quota (its runs are not in it): the answer is always empty.
|
|
15
|
+
*/
|
|
16
|
+
export const listArchivableStorageSurplusAdmin = async (
|
|
17
|
+
body: IListStorageSurplusAdminBody,
|
|
18
|
+
): Promise<IListStorageSurplusAdminResponse> =>
|
|
19
|
+
(await AbyssReflexCore.axios.post<IListStorageSurplusAdminResponse>(`${BASE}/archivable`, body))
|
|
20
|
+
.data;
|
|
21
|
+
|
|
22
|
+
/** `POST /storage-surplus/admin/archive`: moves the given days to the archive; idempotent. Admin token. */
|
|
23
|
+
export const archiveStorageSurplusAdmin = async (
|
|
24
|
+
body: IApplyStorageSurplusAdminBody,
|
|
25
|
+
): Promise<IApplyStorageSurplusAdminResponse> =>
|
|
26
|
+
(await AbyssReflexCore.axios.post<IApplyStorageSurplusAdminResponse>(`${BASE}/archive`, body))
|
|
27
|
+
.data;
|
|
28
|
+
|
|
29
|
+
/** `POST /storage-surplus/admin/archives`: the archived days of the organization, the oldest first. Admin token. */
|
|
30
|
+
export const listArchivesStorageSurplusAdmin = async (
|
|
31
|
+
body: IListStorageSurplusAdminBody,
|
|
32
|
+
): Promise<IListStorageSurplusAdminResponse> =>
|
|
33
|
+
(await AbyssReflexCore.axios.post<IListStorageSurplusAdminResponse>(`${BASE}/archives`, body))
|
|
34
|
+
.data;
|
|
35
|
+
|
|
36
|
+
/** `POST /storage-surplus/admin/delete-archives`: deletes the given archived days for good; idempotent. Admin token. */
|
|
37
|
+
export const deleteArchivesStorageSurplusAdmin = async (
|
|
38
|
+
body: IApplyStorageSurplusAdminBody,
|
|
39
|
+
): Promise<IApplyStorageSurplusAdminResponse> =>
|
|
40
|
+
(
|
|
41
|
+
await AbyssReflexCore.axios.post<IApplyStorageSurplusAdminResponse>(
|
|
42
|
+
`${BASE}/delete-archives`,
|
|
43
|
+
body,
|
|
44
|
+
)
|
|
45
|
+
).data;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { buildDiscordCommandTree, discordCommandPathOf } from './discord-command-tree';
|
|
2
|
+
|
|
3
|
+
const node = (id: string, name: string, parentId: string | null = null) => ({ id, name, parentId });
|
|
4
|
+
|
|
5
|
+
const COMMANDS = [
|
|
6
|
+
node('c', 'config'),
|
|
7
|
+
node('t', 'ticket'),
|
|
8
|
+
node('tc', 'close', 't'),
|
|
9
|
+
node('to', 'open', 't'),
|
|
10
|
+
node('cr', 'role', 'c'),
|
|
11
|
+
node('cra', 'add', 'cr'),
|
|
12
|
+
node('crr', 'remove', 'cr'),
|
|
13
|
+
node('p', 'ping'),
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
describe('buildDiscordCommandTree', () => {
|
|
17
|
+
it('nests groups and subcommands under their command, sorted by name at every level', () => {
|
|
18
|
+
const tree = buildDiscordCommandTree(COMMANDS);
|
|
19
|
+
|
|
20
|
+
expect(tree.map((root) => root.command.name)).toEqual(['config', 'ping', 'ticket']);
|
|
21
|
+
expect(tree[2].children.map((child) => child.command.name)).toEqual(['close', 'open']);
|
|
22
|
+
expect(tree[0].children[0].command.name).toBe('role');
|
|
23
|
+
expect(tree[0].children[0].children.map((child) => child.command.name)).toEqual([
|
|
24
|
+
'add',
|
|
25
|
+
'remove',
|
|
26
|
+
]);
|
|
27
|
+
expect(tree[1].children).toEqual([]);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('leaves out a node whose parent is not in the list', () => {
|
|
31
|
+
const tree = buildDiscordCommandTree([node('x', 'orphan', 'missing'), node('p', 'ping')]);
|
|
32
|
+
|
|
33
|
+
expect(tree.map((root) => root.command.name)).toEqual(['ping']);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('discordCommandPathOf', () => {
|
|
38
|
+
it('gives the names from the root down', () => {
|
|
39
|
+
expect(discordCommandPathOf(COMMANDS[5], COMMANDS)).toEqual(['config', 'role', 'add']);
|
|
40
|
+
expect(discordCommandPathOf(COMMANDS[3], COMMANDS)).toEqual(['ticket', 'open']);
|
|
41
|
+
expect(discordCommandPathOf(COMMANDS[7], COMMANDS)).toEqual(['ping']);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('is empty when an ancestor is missing', () => {
|
|
45
|
+
expect(discordCommandPathOf(node('x', 'add', 'gone'), COMMANDS)).toEqual([]);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
IReflexDiscordCommand,
|
|
3
|
+
IReflexDiscordCommandTreeNode,
|
|
4
|
+
} from '../types/interface/models';
|
|
5
|
+
|
|
6
|
+
type TreeSource = Pick<IReflexDiscordCommand, 'id' | 'parentId' | 'name'>;
|
|
7
|
+
|
|
8
|
+
const byName = <T extends TreeSource>(
|
|
9
|
+
a: IReflexDiscordCommandTreeNode<T>,
|
|
10
|
+
b: IReflexDiscordCommandTreeNode<T>,
|
|
11
|
+
): number => (a.command.name < b.command.name ? -1 : a.command.name > b.command.name ? 1 : 0);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The commands of a Service as trees: each root (a node without parent) with its children, sorted by name at
|
|
15
|
+
* every level, as the registration sends them and the screens show them. A node whose parent is not in the
|
|
16
|
+
* list is left out: it cannot be placed.
|
|
17
|
+
*/
|
|
18
|
+
export const buildDiscordCommandTree = <T extends TreeSource>(
|
|
19
|
+
commands: readonly T[],
|
|
20
|
+
): IReflexDiscordCommandTreeNode<T>[] => {
|
|
21
|
+
const nodes = new Map<string, IReflexDiscordCommandTreeNode<T>>(
|
|
22
|
+
commands.map((command) => [command.id, { command, children: [] }]),
|
|
23
|
+
);
|
|
24
|
+
const roots: IReflexDiscordCommandTreeNode<T>[] = [];
|
|
25
|
+
for (const node of nodes.values()) {
|
|
26
|
+
if (node.command.parentId === null) {
|
|
27
|
+
roots.push(node);
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
nodes.get(node.command.parentId)?.children.push(node);
|
|
31
|
+
}
|
|
32
|
+
const sort = (level: IReflexDiscordCommandTreeNode<T>[]): IReflexDiscordCommandTreeNode<T>[] => {
|
|
33
|
+
level.sort(byName);
|
|
34
|
+
level.forEach((node) => sort(node.children));
|
|
35
|
+
return level;
|
|
36
|
+
};
|
|
37
|
+
return sort(roots);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The names from the root down to the command, `['config', 'role', 'add']`: what a user types after the slash,
|
|
42
|
+
* and, joined by a space, the routing key of a leaf. Empty when an ancestor is missing from the list.
|
|
43
|
+
*/
|
|
44
|
+
export const discordCommandPathOf = <T extends TreeSource>(
|
|
45
|
+
command: T,
|
|
46
|
+
commands: readonly T[],
|
|
47
|
+
): string[] => {
|
|
48
|
+
const byId = new Map(commands.map((entry) => [entry.id, entry]));
|
|
49
|
+
const path = [command.name];
|
|
50
|
+
let parentId = command.parentId;
|
|
51
|
+
while (parentId !== null) {
|
|
52
|
+
const parent = byId.get(parentId);
|
|
53
|
+
if (parent === undefined || path.length > 3) {
|
|
54
|
+
return [];
|
|
55
|
+
}
|
|
56
|
+
path.unshift(parent.name);
|
|
57
|
+
parentId = parent.parentId;
|
|
58
|
+
}
|
|
59
|
+
return path;
|
|
60
|
+
};
|