Skip to content

Commit

Permalink
Merge pull request #841 from cardstack/cs-6292-flaky-test-allows-real…
Browse files Browse the repository at this point in the history
…m-selection

fix for flaky realm info tests
  • Loading branch information
habdelra authored Nov 21, 2023
2 parents fc6add0 + 41d7a00 commit 8ebd1ad
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
9 changes: 8 additions & 1 deletion packages/host/app/components/realm-dropdown.gts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ interface Signature {

export default class RealmDropdown extends Component<Signature> {
<template>
<BoxelDropdown @contentClass={{@contentClass}}>
<BoxelDropdown
@contentClass={{@contentClass}}
data-test-load-realms-loaded={{this.loaded}}
>
<:trigger as |bindings|>
<Button
class='realm-dropdown-trigger'
Expand Down Expand Up @@ -94,6 +97,10 @@ export default class RealmDropdown extends Component<Signature> {
this.realmInfoService.fetchAllKnownRealmInfos.perform();
}

get loaded() {
return this.realmInfoService.fetchAllKnownRealmInfos.isIdle;
}

get realms(): RealmDropdownItem[] {
let items: RealmDropdownItem[] | [] = [];
for (let [
Expand Down
59 changes: 37 additions & 22 deletions packages/host/app/services/realm-info-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Service, { service } from '@ember/service';

import { buildWaiter } from '@ember/test-waiters';

import { restartableTask } from 'ember-concurrency';
import { TrackedMap } from 'tracked-built-ins';

import {
RealmInfo,
Expand All @@ -14,11 +17,12 @@ import ENV from '@cardstack/host/config/environment';
import LoaderService from '@cardstack/host/services/loader-service';

const { ownRealmURL, otherRealmURLs } = ENV;
const waiter = buildWaiter('realm-info-service:waiter');

export default class RealmInfoService extends Service {
@service declare loaderService: LoaderService;
cachedRealmURLsForFileURL: Map<string, string> = new Map(); // Has the file url already been resolved to a realm url?
cachedRealmInfos: Map<string, RealmInfo> = new Map(); // Has the realm url already been resolved to a realm info?
cachedRealmURLsForFileURL: TrackedMap<string, string> = new TrackedMap(); // Has the file url already been resolved to a realm url?
cachedRealmInfos: TrackedMap<string, RealmInfo> = new TrackedMap(); // Has the realm url already been resolved to a realm info?

async fetchRealmURL(fileURL: string): Promise<string> {
if (this.cachedRealmURLsForFileURL.has(fileURL)) {
Expand Down Expand Up @@ -50,21 +54,26 @@ export default class RealmInfoService extends Service {
throw new Error("Must provide either 'realmUrl' or 'fileUrl'");
}

let realmURLString = realmURL
? realmURL.href
: await this.fetchRealmURL(fileURL!);

if (this.cachedRealmInfos.has(realmURLString)) {
return this.cachedRealmInfos.get(realmURLString)!;
} else {
let realmInfoResponse = await this.loaderService.loader.fetch(
`${realmURLString}_info`,
{ headers: { Accept: SupportedMimeType.RealmInfo } },
);

let realmInfo = (await realmInfoResponse.json())?.data?.attributes;
this.cachedRealmInfos.set(realmURLString, realmInfo);
return realmInfo;
let token = waiter.beginAsync();
try {
let realmURLString = realmURL
? realmURL.href
: await this.fetchRealmURL(fileURL!);

if (this.cachedRealmInfos.has(realmURLString)) {
return this.cachedRealmInfos.get(realmURLString)!;
} else {
let realmInfoResponse = await this.loaderService.loader.fetch(
`${realmURLString}_info`,
{ headers: { Accept: SupportedMimeType.RealmInfo } },
);

let realmInfo = (await realmInfoResponse.json())?.data?.attributes;
this.cachedRealmInfos.set(realmURLString, realmInfo);
return realmInfo;
}
} finally {
waiter.endAsync(token);
}
}

Expand All @@ -73,10 +82,16 @@ export default class RealmInfoService extends Service {
...new Set([ownRealmURL, baseRealm.url, ...otherRealmURLs]),
].map((path) => new RealmPaths(path).url);

await Promise.all(
paths.map(
async (path) => await this.fetchRealmInfo({ realmURL: new URL(path) }),
),
);
let token = waiter.beginAsync();
try {
await Promise.all(
paths.map(
async (path) =>
await this.fetchRealmInfo({ realmURL: new URL(path) }),
),
);
} finally {
waiter.endAsync(token);
}
});
}
4 changes: 0 additions & 4 deletions packages/host/tests/acceptance/code-submode/editor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,6 @@ module('Acceptance | code submode | editor tests', function (hooks) {
setMonacoContent('Hello Mars');

await waitFor('[data-test-save-idle]');

await percySnapshot(assert);
});

test<TestContextWithSave>('unsaved changes made in monaco editor are saved when switching out of code submode', async function (assert) {
Expand Down Expand Up @@ -767,8 +765,6 @@ module('Acceptance | code submode | editor tests', function (hooks) {
},
});

await percySnapshot(assert);

let fileRef = await adapter.openFile('pet.gts');
if (!fileRef) {
throw new Error('file not found');
Expand Down

0 comments on commit 8ebd1ad

Please sign in to comment.