Skip to content

Commit

Permalink
test(e2e): add updateOperationsWithConflicts and rename updateWithDep…
Browse files Browse the repository at this point in the history
…endencies
  • Loading branch information
EdouardDem committed May 8, 2024
1 parent 4dfab8a commit 3755534
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
1 change: 0 additions & 1 deletion packages/e2e/spec/dependencies/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './push-with-dependencies.js';
export * from './update-with-dependencies.js';
23 changes: 13 additions & 10 deletions packages/e2e/spec/entrypoint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@ import 'dotenv/config';
import './helpers/env.js';
import { Context } from './helpers/index.js';
import {
pullAndPushWithoutData,
pullAndPushWithoutChanges,
preserveIds,
pullAndPushWithChanges,
pullAndPushWithDeletions,
pullAndPushWithoutChanges,
pullAndPushWithoutData,
pullBasic,
pushFlushAndPush,
pullWithNewData,
pushFlushAndPush,
pushOnEmptyInstance,
pushTwiceOnEmptyInstance,
pullAndPushWithChanges,
pullAndPushWithDeletions,
} from './pull-diff-push/index.js';
import {
pushWithDependencies,
updateWithDependencies,
} from './dependencies/index.js';
import { pushWithDependencies } from './dependencies/index.js';
import {
collectionsOnDump,
collectionsOnLoad,
Expand All @@ -36,6 +33,10 @@ import {
removePermissionDuplicates,
} from './permissions/index.js';
import { removeTrackedItem } from './untrack/index.js';
import {
createOperationsWithConflicts,
updateOperationsWithConflicts,
} from './operations/index.js';

jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;

Expand Down Expand Up @@ -70,7 +71,9 @@ describe('Tests entrypoint ->', () => {
pushTwiceOnEmptyInstance(context);

pushWithDependencies(context);
updateWithDependencies(context);

updateOperationsWithConflicts(context);
createOperationsWithConflicts(context);

collectionsOnDump(context);
collectionsOnSave(context);
Expand Down
90 changes: 90 additions & 0 deletions packages/e2e/spec/operations/create-operations-with-conflicts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import {
Context,
getDumpedSystemCollectionsContents,
info,
readAllSystemCollections,
SystemCollectionsContentWithSyncId,
} from '../helpers/index.js';
import { deleteOperation, updateOperation } from '@directus/sdk';

type OperationFromJson = SystemCollectionsContentWithSyncId['operations'][0];

export const createOperationsWithConflicts = (context: Context) => {
it('create an operation that conflicts with other', async () => {
// Init sync client
const sync = await context.getSync('sources/multiple-dependencies', false);
const directus = context.getDirectus();
const client = directus.get();
await sync.push();

// Get the sync id map
let idMaps = await directus.getSyncIdMaps('operations');
const getLocalId = (syncId: string) => {
const localId = idMaps.find((m) => m.sync_id === syncId)?.local_id;
if (!localId) throw new Error(`Local id not found for sync id ${syncId}`);
return localId;
};

// Remove the second operation and make the first one point to the third
const { operations } = getDumpedSystemCollectionsContents(
sync.getDumpPath(),
);
const [operation1, operation2, operation3] = operations as [
OperationFromJson,
OperationFromJson,
OperationFromJson,
];
await client.request(
updateOperation(getLocalId(operation1._syncId), {
resolve: null,
}),
);
await client.request(deleteOperation(getLocalId(operation2._syncId)));
await client.request(
updateOperation(getLocalId(operation1._syncId), {
resolve: getLocalId(operation3._syncId),
}),
);

// Push back the data
const beforePushDate = new Date();
const output = await sync.push();

expect(output).toContain(info(`[operations] Deleted 1 dangling items`));
expect(output).toContain(info(`[operations] Created 1 items`));
expect(output).toContain(info(`[operations] Updated 1 items`));
expect(output).toContain(info(`[operations] Deleted 0 items`));

// Ensure that no activities were created
const activities = (await directus.getActivities(beforePushDate)).filter(
(a) => a.collection === 'directus_operations',
);
expect(activities.filter((a) => a.action === 'create').length).toEqual(1);
expect(activities.filter((a) => a.action === 'delete')).toEqual([]);

// One extra to nullify the resolve column and avoid unique constraint conflict
expect(activities.filter((a) => a.action === 'update').length).toEqual(2);

// Get the operations ids
idMaps = await directus.getSyncIdMaps('operations');
const id1 = getLocalId(operation1._syncId);
const id2 = getLocalId(operation2._syncId);
const id3 = getLocalId(operation3._syncId);

// Get the new operations
const { operations: newOperations } =
await readAllSystemCollections(client);
const newOperation1 = newOperations.find((o) => o.id === id1);
const newOperation2 = newOperations.find((o) => o.id === id2);
const newOperation3 = newOperations.find((o) => o.id === id3);

// Check if hte chain is correct
expect(newOperation1).toBeDefined();
expect(newOperation2).toBeDefined();
expect(newOperation3).toBeDefined();

expect(newOperation1?.resolve).toEqual(id2);
expect(newOperation2?.resolve).toEqual(id3);
expect(newOperation3?.resolve).toBeNull();
});
};
2 changes: 2 additions & 0 deletions packages/e2e/spec/operations/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './update-operations-with-conflicts.js';
export * from './create-operations-with-conflicts.js';
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Context, info } from '../helpers/index.js';

export const updateWithDependencies = (context: Context) => {
it('reverse 2 operations dependencies', async () => {
export const updateOperationsWithConflicts = (context: Context) => {
it('reverse 2 operations conflicts', async () => {
// Init sync client
const syncInit = await context.getSync(
'sources/multiple-dependencies',
Expand Down

0 comments on commit 3755534

Please sign in to comment.