Skip to content

Commit

Permalink
test(api-elasticsearch-tasks): add missing context variables
Browse files Browse the repository at this point in the history
  • Loading branch information
brunozoric committed Feb 19, 2025
1 parent e6ea8b0 commit cbfc135
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 34 deletions.
57 changes: 40 additions & 17 deletions packages/api-elasticsearch-tasks/__tests__/mocks/context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PluginsContainer } from "@webiny/plugins";
import { PartialDeep } from "type-fest";
import { createMockIdentity } from "~tests/mocks/identity";
import {
import type {
Context,
ITaskLogUpdateInput,
ITaskUpdateData,
Expand All @@ -10,31 +10,54 @@ import {
import { ElasticsearchContext } from "@webiny/api-elasticsearch/types";
// @ts-expect-error
import { createMockApiLog } from "@webiny/project-utils/testing/mockApiLog";
import type { Tenant } from "@webiny/api-tenancy/types";
import type { Context as LoggerContext } from "@webiny/api-log/types";

export const createContextMock = (
params?: PartialDeep<Context & ElasticsearchContext>
): Context & ElasticsearchContext => {
params?: PartialDeep<Context & ElasticsearchContext & LoggerContext>
): Context & ElasticsearchContext & LoggerContext => {
const tenants: Tenant[] = [
{
id: "root",
name: "Root",
parent: null
} as Tenant
];
const locales = [
{
code: "en-US",
default: true
}
];
return {
logger: createMockApiLog(),
tenancy: {
listTenants: async () => {
return [
{
id: "root",
name: "Root",
parent: null
}
];
return tenants;
},
withEachTenant: async (input: Tenant[], cb: (t: Tenant) => Promise<any>) => {
const results = [];
for (const t of input) {
results.push(await cb(t));
}
return results;
}
},
i18n: {
locales: {
listLocales: async () => {
return [
locales,
{
totalCount: locales.length,
hasMoreItems: false,
cursor: null
}
];
}
},
getLocales: async () => {
return [
{
code: "en-US",
default: true
}
];
return locales;
}
},
...params,
Expand Down Expand Up @@ -66,5 +89,5 @@ export const createContextMock = (
},
...params?.tasks
}
} as unknown as Context & ElasticsearchContext;
} as unknown as Context & ElasticsearchContext & LoggerContext;
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import {
ElasticsearchClient
} from "@webiny/project-utils/testing/elasticsearch/createClient";

import { timerFactory } from "@webiny/handler-aws";

describe("create indexes task runner", () => {
const timer = timerFactory();
let elasticsearchClient: ElasticsearchClient;

beforeEach(async () => {
Expand All @@ -36,7 +39,8 @@ describe("create indexes task runner", () => {
},
isAborted: () => {
return false;
}
},
timer
});
const indexManager = createIndexManagerMock();
const runner = new CreateIndexesTaskRunner(manager, indexManager);
Expand Down Expand Up @@ -79,7 +83,8 @@ describe("create indexes task runner", () => {
},
isAborted: () => {
return false;
}
},
timer
});
const indexManager = createIndexManagerMock({
client: elasticsearchClient
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Manager } from "~/tasks/Manager";
import { IndexManager } from "~/settings";
import type { Manager } from "~/tasks/Manager";
import type { IndexManager } from "~/settings";
import type { ITaskResponseResult } from "@webiny/tasks";
import type { IElasticsearchCreateIndexesTaskInput } from "./types";
import { listIndexes } from "./listIndexes";
import { createIndexFactory } from "./createIndex";
import type { Context } from "~/types";
import { listCreateElasticsearchIndexTaskPlugin } from "./listCreateElasticsearchIndexTaskPlugin";

export class CreateIndexesTaskRunner {
private readonly manager: Manager<IElasticsearchCreateIndexesTaskInput>;
Expand All @@ -22,8 +24,16 @@ export class CreateIndexesTaskRunner {
matching: string | undefined,
done: string[]
): Promise<ITaskResponseResult> {
const plugins = listCreateElasticsearchIndexTaskPlugin<Context>(
this.manager.context.plugins
);
if (plugins.length === 0) {
return this.manager.response.done("No index plugins found.");
}

const indexes = await listIndexes({
context: this.manager.context
context: this.manager.context,
plugins
});

if (indexes.length === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Context } from "~/types";
import type { IndexManager } from "~/settings";
import { listIndexes } from "./listIndexes";
import { createIndexFactory } from "~/tasks/createIndexes/createIndex";
import { listCreateElasticsearchIndexTaskPlugin } from "~/tasks/createIndexes/listCreateElasticsearchIndexTaskPlugin";

export interface IOnBeforeTriggerParams {
indexManager: IndexManager;
Expand All @@ -18,9 +19,12 @@ export class OnBeforeTrigger {
}

public async run(targets: string[] | undefined): Promise<void> {
const plugins = listCreateElasticsearchIndexTaskPlugin<Context>(this.context.plugins);

try {
const allIndexes = await listIndexes({
context: this.context
context: this.context,
plugins
});
const indexes = allIndexes.filter(index => {
if (!targets?.length) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { CreateElasticsearchIndexTaskPlugin } from "~/tasks/createIndexes/CreateElasticsearchIndexTaskPlugin";
import type { Context } from "~/types";
import type { PluginsContainer } from "@webiny/plugins";

export const listCreateElasticsearchIndexTaskPlugin = <C extends Context = Context>(
plugins: PluginsContainer
): CreateElasticsearchIndexTaskPlugin<C>[] => {
return plugins.byType<CreateElasticsearchIndexTaskPlugin<Context>>(
CreateElasticsearchIndexTaskPlugin.type
);
};
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
import type { I18NContextObject } from "@webiny/api-i18n/types";
import { CreateElasticsearchIndexTaskPlugin } from "~/tasks/createIndexes/CreateElasticsearchIndexTaskPlugin";
import type { CreateElasticsearchIndexTaskPluginIndex } from "~/tasks/createIndexes/CreateElasticsearchIndexTaskPlugin";
import { CreateElasticsearchIndexTaskPlugin } from "~/tasks/createIndexes/CreateElasticsearchIndexTaskPlugin";
import type { Context } from "~/types";
import type { Tenant } from "@webiny/api-tenancy/types";

export interface IListIndexesParams {
context: Context;
plugins: CreateElasticsearchIndexTaskPlugin<Context>[];
}

export const listIndexes = async (
params: IListIndexesParams
): Promise<CreateElasticsearchIndexTaskPluginIndex[]> => {
const { context } = params;

const plugins = context.plugins.byType<CreateElasticsearchIndexTaskPlugin<Context>>(
CreateElasticsearchIndexTaskPlugin.type
);
const { context, plugins } = params;
if (plugins.length === 0) {
throw new Error("No index plugins found.");
return [];
}

const i18n: I18NContextObject = context.i18n;

const tenants = await context.tenancy.listTenants();
const results = await context.tenancy.withEachTenant<
Tenant,
CreateElasticsearchIndexTaskPluginIndex[]
>(tenants, async tenant => {
const indexes: CreateElasticsearchIndexTaskPluginIndex[] = [];
const [locales] = await i18n.locales.listLocales();
const [locales] = await context.i18n.locales.listLocales();

for (const locale of locales) {
for (const plugin of plugins) {
Expand Down

0 comments on commit cbfc135

Please sign in to comment.