Skip to content

Commit

Permalink
#675 More stable tests and added back commit queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Polleps committed Nov 15, 2023
1 parent 0a623b6 commit 2d45bd7
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 28 deletions.
6 changes: 5 additions & 1 deletion browser/data-browser/src/components/TableEditor/Cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,14 @@ export function Cell({
return;
}

if (isActive && cursorMode === CursorMode.Edit) {
return;
}

setCursorMode(CursorMode.Visual);
setActiveCell(rowIndex, columnIndex);
},
[setActiveCell, columnIndex, shouldEnterEditMode],
[setActiveCell, columnIndex, shouldEnterEditMode, cursorMode, isActive],
);

const handleClick = useCallback(() => {
Expand Down
2 changes: 1 addition & 1 deletion browser/data-browser/src/routes/History/useVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function useVersions(resource: Resource): UseVersionsResult {
.finally(() => {
setLoading(false);
});
}, [resource.getSubject()]);
}, [resource]);

return { versions, loading, error };
}
8 changes: 1 addition & 7 deletions browser/data-browser/src/views/ChatRoomPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -413,19 +413,13 @@ function MessagesPage({ subject, setReplyTo }: MessagesPageProps) {
const resource = useResource(subject);
const [messages] = useArray(resource, properties.chatRoom.messages);
const [nextPage] = useString(resource, properties.chatRoom.nextPage);
const [inView] = useState(true);
const ref = useRef<HTMLDivElement>(null);

if (!inView) {
return <>Not in view...</>;
}

if (!resource.isReady()) {
return <>loading...</>;
}

return (
<div ref={ref}>
<div>
{nextPage && <MessagesPage subject={nextPage} setReplyTo={setReplyTo} />}
{messages.map(message => (
<Message
Expand Down
9 changes: 7 additions & 2 deletions browser/data-browser/src/views/DocumentPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function DocumentPageEdit({
const [elements, setElements] = useArray(
resource,
properties.document.elements,
{ commit: true, validate: false, commitDebounce: 0 },
{ commit: false, validate: false, commitDebounce: 0 },
);

const titleRef = React.useRef<HTMLInputElement>(null);
Expand Down Expand Up @@ -201,9 +201,10 @@ function DocumentPageEdit({
newElement.set(properties.parent, resource.getSubject(), store),
newElement.set(properties.description, '', store),
]);
newElement.save(store);
await setElements(newElements);
focusElement(position);
await newElement.save(store);
await resource.save(store);
} catch (e) {
setErr(e);
}
Expand Down Expand Up @@ -236,6 +237,7 @@ function DocumentPageEdit({
if (elements.length === 1) {
setElements([]);
focusElement(0);
resource.save(store);

return;
}
Expand All @@ -254,6 +256,7 @@ function DocumentPageEdit({
addElement(index + 1);
} else {
focusElement(index + 1);
resource.save(store);
}
}

Expand All @@ -263,6 +266,7 @@ function DocumentPageEdit({
elements.splice(to, 0, element);
setElements(elements);
focusElement(to);
resource.save(store);
}

function handleSortEnd(event: DragEndEvent): void {
Expand All @@ -285,6 +289,7 @@ function DocumentPageEdit({
toast.success('Upload succeeded!');
fileSubjects.map(subject => elements.push(subject));
setElements([...elements]);
resource.save(store);
}

/** Add a new line, or move to the last line if it is empty */
Expand Down
2 changes: 2 additions & 0 deletions browser/data-browser/tests/documents.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
openNewSubjectWindow,
timestamp,
before,
waitForCommitOnCurrentResource,
} from './test-utils';
test.describe('documents', async () => {
test.beforeEach(before);
Expand All @@ -36,6 +37,7 @@ test.describe('documents', async () => {

// multi-user
const currentSubject = await getCurrentSubject(page);
await waitForCommitOnCurrentResource(page);
const page2 = await openNewSubjectWindow(browser, currentSubject!);
await expect(
page2.locator(`text=${teststring}`),
Expand Down
32 changes: 17 additions & 15 deletions browser/data-browser/tests/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
waitForCommit,
openAgentPage,
fillSearchBox,
waitForCommitOnCurrentResource,
} from './test-utils';

test.describe('data-browser', async () => {
Expand Down Expand Up @@ -231,20 +232,13 @@ test.describe('data-browser', async () => {
'Chat message not appearing directly after sending',
).toBeVisible();

const dropdownId = await page
.locator(contextMenu)
.getAttribute('aria-controls');

await page.click(contextMenu);
await page
.locator(`[id="${dropdownId}"] >> [data-test="menu-item-share"]`)
.click();
await publicReadRightLocator(page).click();
await page.click('text=save');

const page2 = await openNewSubjectWindow(browser, chatRoomUrl);
// Second user
await signIn(page2);

// TODO: TEMP FIX, NO LONGER NEEDED IF #686 IS FIXED
page2.reload();

await expect(page2.locator(`text=${teststring}`)).toBeVisible();
const teststring2 = `My reply: ${timestamp()}`;
await page2.fill('[data-test="message-input"]', teststring2);
Expand Down Expand Up @@ -298,7 +292,7 @@ test.describe('data-browser', async () => {
// wait for commit debounce
// await page.waitForTimeout(2000);
// make sure no commits are waiting for each other
await page.waitForLoadState('networkidle');
await waitForCommitOnCurrentResource(page);
await page.reload();
await expect(
page.locator(`text=${alphabet}`).first(),
Expand Down Expand Up @@ -438,7 +432,9 @@ test.describe('data-browser', async () => {
page.locator(`[data-test="sidebar"] >> text=${d1}`),
"Sidebar doesn't show child resource title",
).toBeVisible();
await page.waitForLoadState('networkidle');

await waitForCommitOnCurrentResource(page);

await page.reload();
await expect(
page.locator(`[data-test="sidebar"] >> text=${d1}`),
Expand Down Expand Up @@ -551,8 +547,14 @@ test.describe('data-browser', async () => {
await expect(
page.getByRole('heading', { name: 'Second Title', level: 1 }),
).toBeVisible();
// Wait for commit debounce
await page.waitForTimeout(500);

// The history page does not update when the resource changes so we need to wait to be sure the commit is done
// before opening the history page.
await waitForCommitOnCurrentResource(page, {
set: {
['https://atomicdata.dev/properties/name']: 'Second Title',
},
});

await contextMenuClick('history', page);

Expand Down
2 changes: 2 additions & 0 deletions browser/data-browser/tests/ontology.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ test.describe('Ontology', async () => {
test.beforeEach(before);

test('Create and edit ontology', async ({ page }) => {
test.slow();

const pickOption = async (query: Locator) => {
await page.waitForTimeout(100);
await query.hover();
Expand Down
4 changes: 3 additions & 1 deletion browser/data-browser/tests/tables.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ test.describe('tables', async () => {
test.beforeEach(before);

test('create and fill', async ({ page }) => {
test.slow();

const newColumn = async (type: string) => {
await page.getByRole('button', { name: 'Add column' }).click();
await page.click(`text=${type}`);
Expand Down Expand Up @@ -51,7 +53,7 @@ test.describe('tables', async () => {
await page.waitForTimeout(300);
// Wait for the table to refresh by checking if the next row is visible
await expect(
page.getByRole('rowheader', { name: `${currentRowNumber}` }),
page.getByRole('rowheader', { name: `${currentRowNumber + 1}` }),
).toBeAttached();

await page.keyboard.type(date);
Expand Down
33 changes: 33 additions & 0 deletions browser/data-browser/tests/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,39 @@ export async function getCurrentSubject(page: Page) {
return page.locator(addressBar).getAttribute('value');
}

export async function waitForCommitOnCurrentResource(
page: Page,
match?: { set?: Record<string, unknown> },
) {
const currentSubject = await getCurrentSubject(page);

await page.waitForResponse(async response => {
const result = await response.json();

const isForCurrentResource =
result['https://atomicdata.dev/properties/subject'] === currentSubject;

if (!isForCurrentResource) {
return false;
}

if (match) {
const set = result['https://atomicdata.dev/properties/set'];

for (const key in match.set) {
if (set[key] !== match.set[key]) {
return false;
}
}
}

// Wait for commit response to be processed by the store.
await page.waitForTimeout(200);

return true;
});
}

export async function openAgentPage(page: Page) {
page.goto(`${FRONTEND_URL}/app/agent`);
}
Expand Down
31 changes: 30 additions & 1 deletion browser/lib/src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export class Resource<C extends OptionalClass = any> {
private subject: string;
private propvals: PropVals;

private inProgressCommit: Promise<void> | undefined;
private hasQueue = false;

public constructor(subject: string, newResource?: boolean) {
if (typeof subject !== 'string') {
throw new Error(
Expand Down Expand Up @@ -305,7 +308,7 @@ export class Resource<C extends OptionalClass = any> {
for (const commit of commits as unknown as string[]) {
const commitResource = await store.getResourceAsync(commit);
const parsedCommit = parseCommitResource(commitResource);
const builtResource = await applyCommitToResource(
const builtResource = applyCommitToResource(
previousResource.clone(),
parsedCommit,
);
Expand Down Expand Up @@ -467,13 +470,26 @@ export class Resource<C extends OptionalClass = any> {
throw new Error('No agent has been set or passed, you cannot save.');
}

if (this.hasQueue) {
return;
}

// If the parent of this resource is new we can't save yet so we add it to a batched that gets saved when the parent does.
if (this.isParentNew(store)) {
store.batchResource(this.getSubject());

return;
}

if (this.inProgressCommit) {
this.hasQueue = true;
await this.inProgressCommit;
this.hasQueue = false;
this.inProgressCommit = undefined;

return this.save(store, differentAgent);
}

// The previousCommit is required in Commits. We should use the `lastCommit` value on the resource.
// This makes sure that we're making adjustments to the same version as the server.
const lastCommit = this.get(properties.commit.lastCommit)?.toString();
Expand All @@ -484,6 +500,14 @@ export class Resource<C extends OptionalClass = any> {

const wasNew = this.new;

let reportDone: () => void = () => undefined;

this.inProgressCommit = new Promise(resolve => {
reportDone = () => {
resolve();
};
});

// Cloning the CommitBuilder to prevent race conditions, and keeping a back-up of current state for when things go wrong during posting.
const oldCommitBuilder = this.commitBuilder.clone();
this.commitBuilder = new CommitBuilder(this.getSubject());
Expand Down Expand Up @@ -520,6 +544,8 @@ export class Resource<C extends OptionalClass = any> {
await store.saveBatchForParent(this.getSubject());
}

reportDone();

return createdCommit.id as string;
} catch (e) {
// Logic for handling error if the previousCommit is wrong.
Expand All @@ -540,13 +566,16 @@ export class Resource<C extends OptionalClass = any> {
}

// Try again!
reportDone();

return await this.save(store, agent);
}

// If it fails, revert to the old resource with the old CommitBuilder
this.commitBuilder = oldCommitBuilder;
this.commitError = e;
store.addResources(this, { skipCommitCompare: true });
reportDone();
throw e;
}
}
Expand Down

0 comments on commit 2d45bd7

Please sign in to comment.