diff --git a/.github/actions/functional-test-backend/action.yml b/.github/actions/functional-test-backend/action.yml index fdd0eb6..9495b36 100644 --- a/.github/actions/functional-test-backend/action.yml +++ b/.github/actions/functional-test-backend/action.yml @@ -7,6 +7,7 @@ runs: shell: bash run: | set +e + npm ci export CI=true docker compose up -d bash ./.github/actions/functional-test-backend/wait-kuzzle.sh diff --git a/apps/api/tests/scenarios/asset_tracking/create-movement-record.test.ts b/apps/api/tests/scenarios/asset_tracking/create-movement-record.test.ts index 72e58cc..4bc80f7 100644 --- a/apps/api/tests/scenarios/asset_tracking/create-movement-record.test.ts +++ b/apps/api/tests/scenarios/asset_tracking/create-movement-record.test.ts @@ -96,31 +96,6 @@ describe("Geofencing: create movement record measure", () => { await moveTruck(insideIDFSud[0]); await sdk.collection.refresh("tenant-asset_tracking-kuzzle", "measures"); - const measures = await sdk.document.search( - "tenant-asset_tracking-kuzzle", - "measures", - { - query: { - equals: { type: "movementRecord" }, - }, - sort: { measuredAt: "asc" }, - }, - { lang: "koncorde" }, - ); - - expect(measures.hits).toHaveLength(2); - expect(measures.hits[0]._source).toMatchObject({ - values: { - in: null, - }, - }); - expect(measures.hits[1]._source).toMatchObject({ - values: { - out: null, - in: "IDFSud", - }, - }); - const assets = await sdk.document.get( "tenant-asset_tracking-kuzzle", "assets", diff --git a/docker-compose.yml b/docker-compose.yml index a567e86..d3665d4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -29,10 +29,10 @@ services: - DEBUG=${DEBUG:-none} healthcheck: test: ["CMD", "curl", "-f", "http://api:7512/_healthcheck"] - timeout: 30s - interval: 30s - retries: 100 - start_period: 3m + timeout: 10s + interval: 10s + retries: 30 + start_period: 1m web: image: kuzzleio/kuzzle-runner:18 diff --git a/helpers/collections.ts b/helpers/collections.ts new file mode 100644 index 0000000..b689fe9 --- /dev/null +++ b/helpers/collections.ts @@ -0,0 +1,23 @@ +import { Kuzzle } from 'kuzzle-sdk'; + +export async function truncateCollection(sdk: Kuzzle, index: string, collection: string) { + await sdk.collection.refresh(index, collection); + await sdk.document.deleteByQuery(index, collection, {}); + await sdk.collection.refresh(index, collection); +} + +/** + * @deprecated alias to truncateCollection + */ +export const resetCollection = truncateCollection; + +export async function beforeEachTruncateCollections(sdk: Kuzzle) { + await Promise.all([ + truncateCollection(sdk, 'platform', 'devices'), + truncateCollection(sdk, 'tenant-asset_tracking-kuzzle', 'assets'), + truncateCollection(sdk, 'tenant-asset_tracking-kuzzle', 'devices'), + truncateCollection(sdk, 'tenant-asset_tracking-kuzzle', 'assets-history'), + truncateCollection(sdk, 'tenant-asset_tracking-kuzzle', 'measures'), + truncateCollection(sdk, 'tenant-asset_tracking-kuzzle', 'alerts'), + ]); +} diff --git a/helpers/index.ts b/helpers/index.ts new file mode 100644 index 0000000..f25e564 --- /dev/null +++ b/helpers/index.ts @@ -0,0 +1,5 @@ +export * from './collections'; +export * from './payloads'; +export * from './setup'; +export * from './tenants'; +export * from './useSdk'; diff --git a/helpers/payloads.ts b/helpers/payloads.ts new file mode 100644 index 0000000..f4dbfc5 --- /dev/null +++ b/helpers/payloads.ts @@ -0,0 +1,32 @@ +import { JSONObject, Kuzzle } from 'kuzzle-sdk'; + +// Delay in second for dates sent between two steps so the plugin accept the new +// Add a delay for subsequent payload of the same device so the plugin accept it +const deviceDelay = {}; + +export async function sendPayloads(sdk: Kuzzle, action: string, payloads: JSONObject[]) { + let response; + + for (let i = 0; i < payloads.length; i++) { + const payload = payloads[i]; + + if (deviceDelay[payload.reference]) { + deviceDelay[payload.reference] += 2; + } else { + deviceDelay[payload.reference] = 1; + } + + if (!payload.date) { + const delay = deviceDelay[payload.reference] * 1000; + payload.date = new Date(Date.now() + delay); + } + + response = await sdk.query({ + controller: 'device-manager/payloads', + action, + body: payload, + }); + } + + return response; +} diff --git a/helpers/setup.ts b/helpers/setup.ts new file mode 100644 index 0000000..33c89c5 --- /dev/null +++ b/helpers/setup.ts @@ -0,0 +1,22 @@ +import { beforeEachTruncateCollections } from './collections'; +import { beforeAllCreateTenants } from './tenants'; +import { useSdk } from './useSdk'; + +export function setupHooks() { + const sdk = useSdk(); + + beforeAll(async () => { + await sdk.connect(); + await beforeAllCreateTenants(sdk); + }); + + beforeEach(async () => { + await beforeEachTruncateCollections(sdk); + }); + + afterAll(async () => { + sdk.disconnect(); + }); + + return sdk; +} diff --git a/helpers/tenants.ts b/helpers/tenants.ts new file mode 100644 index 0000000..ef57782 --- /dev/null +++ b/helpers/tenants.ts @@ -0,0 +1,25 @@ +import { Kuzzle } from 'kuzzle-sdk'; + +async function createTenantIfNotExists(sdk: Kuzzle, group: string, name: string) { + const { result } = await sdk.query({ + controller: 'multi-tenancy/tenant', + action: 'exists', + name, + group, + }); + + if (result.exists) { + return; + } + + await sdk.query({ + controller: 'multi-tenancy/tenant', + action: 'create', + name, + group, + }); +} + +export async function beforeAllCreateTenants(sdk: Kuzzle) { + await Promise.all([createTenantIfNotExists(sdk, 'asset_tracking', 'kuzzle')]); +} diff --git a/helpers/useSdk.ts b/helpers/useSdk.ts new file mode 100644 index 0000000..b35af07 --- /dev/null +++ b/helpers/useSdk.ts @@ -0,0 +1,5 @@ +import { Kuzzle, WebSocket } from 'kuzzle-sdk'; + +export function useSdk(): Kuzzle { + return new Kuzzle(new WebSocket('localhost', { port: 7512 })); +}