-
Notifications
You must be signed in to change notification settings - Fork 12
Get types to work inside of a realm test file locally #4181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tintinthong
wants to merge
9
commits into
main
Choose a base branch
from
getting-test-to-work-for-test-file
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+241
−99
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
438b049
add sample command card
tintinthong c8f5de9
get types to work
tintinthong 5ea2269
fix merge conflict
tintinthong a834eac
remove any live test
tintinthong 627495e
add fix for test
tintinthong 7075f5f
Merge branch 'main' into getting-test-to-work-for-test-file
tintinthong 28ed2b0
add pnpm install
tintinthong 03ac2c5
improve type definition again
tintinthong dad6552
prettify
tintinthong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import { | ||
| CardDef, | ||
| field, | ||
| contains, | ||
| Component, | ||
| } from 'https://cardstack.com/base/card-api'; | ||
| import StringField from 'https://cardstack.com/base/string'; | ||
|
|
||
| export class SampleCommandCard extends CardDef { | ||
| static displayName = 'Sample Command Card'; | ||
| @field title = contains(StringField); | ||
|
|
||
| static isolated = class Isolated extends Component<typeof SampleCommandCard> { | ||
| <template> | ||
| <h1><@fields.title /></h1> | ||
| <button type='button'>Create Card</button> | ||
| </template> | ||
| }; | ||
| } | ||
|
|
||
| // ── Tests (imports resolved via loader.shimModule in live-test.js) ──────────── | ||
| import { on } from '@ember/modifier'; | ||
| import { service } from '@ember/service'; | ||
| import { click, render } from '@ember/test-helpers'; | ||
| import GlimmerComponent from '@glimmer/component'; | ||
| import { module, test } from 'qunit'; | ||
|
|
||
| import { getService } from '@universal-ember/test-support'; | ||
| import { baseRealm, type Store } from '@cardstack/runtime-common'; | ||
| import { | ||
| setupIntegrationTestRealm, | ||
| setupLocalIndexing, | ||
| setupOnSave, | ||
| setupCardLogs, | ||
| testRealmURL, | ||
| setupRealmCacheTeardown, | ||
| withCachedRealmSetup, | ||
| type TestContextWithSave, | ||
| } from '@cardstack/host/tests/helpers'; | ||
| import { TestRealmAdapter } from '@cardstack/host/tests/helpers/adapter'; | ||
| import { setupMockMatrix } from '@cardstack/host/tests/helpers/mock-matrix'; | ||
| import { setupRenderingTest } from '@cardstack/host/tests/helpers/setup'; | ||
|
|
||
| class CreateCardButton extends GlimmerComponent { | ||
| @service declare store: Store; | ||
|
|
||
| createCard = async () => { | ||
| await this.store.add( | ||
| new SampleCommandCard({ title: 'Hello from live-test' }), | ||
| ); | ||
| }; | ||
|
|
||
| <template> | ||
| <button type='button' {{on 'click' this.createCard}}>Create Card</button> | ||
| </template> | ||
| } | ||
|
|
||
| module('Experiments | SampleCommandCard', function (hooks) { | ||
| setupRenderingTest(hooks); | ||
| setupLocalIndexing(hooks); | ||
| setupOnSave(hooks); | ||
| setupRealmCacheTeardown(hooks); | ||
| setupCardLogs(hooks, async () => | ||
| (getService('loader-service') as any).loader.import( | ||
| `${baseRealm.url}card-api`, | ||
| ), | ||
| ); | ||
|
|
||
| let mockMatrixUtils = setupMockMatrix(hooks, { | ||
| loggedInAs: '@testuser:localhost', | ||
| activeRealms: [testRealmURL], | ||
| autostart: true, | ||
| }); | ||
|
|
||
| let testRealmAdapter: TestRealmAdapter; | ||
|
|
||
| hooks.beforeEach(async function () { | ||
| ({ adapter: testRealmAdapter } = await withCachedRealmSetup(async () => | ||
| setupIntegrationTestRealm({ | ||
| mockMatrixUtils, | ||
| contents: { | ||
| 'sample-command-card.gts': { SampleCommandCard }, | ||
| '.realm.json': '{ "name": "Sample Realm" }', | ||
| }, | ||
| }), | ||
| )); | ||
| }); | ||
|
|
||
| test('clicking Create Card writes a new card to the realm', async function (this: TestContextWithSave, assert) { | ||
| assert.expect(3); | ||
|
|
||
| let savedUrl: URL | undefined; | ||
| this.onSave((url, doc) => { | ||
| savedUrl = url; | ||
| assert.strictEqual( | ||
| (doc as any).data.attributes.title, | ||
| 'Hello from live-test', | ||
| 'saved doc has correct title', | ||
| ); | ||
| }); | ||
|
|
||
| await render(<template><CreateCardButton /></template>); | ||
| await click('button'); | ||
|
|
||
| assert.ok(savedUrl, 'card was saved to realm'); | ||
| let relativePath = `${savedUrl!.href.substring(testRealmURL.length)}.json`; | ||
| let file = await testRealmAdapter.openFile(relativePath); | ||
| assert.ok(file, 'card JSON file exists in the realm adapter'); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Im perfroming a full reindex on experiments to be certain this doesn't break the indexer. Which is the key issue
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems to have succesfully indexed the file independent of the host
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a really important comment: specifically, due to the static nature you have defined the modules tests are run as a result of just loading this card. I don't think we ever want that. that means indexing is actually running tests (other things will end up running tests--like the user just interacting with this card will always end up running tests as a result of using this card). we should never have the
module()ortest()functions in module scope. A better shape might be:and then tests are only executed when
runTests()is called, not as a side effect of just loading the module.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i feel so strongly about this, that we should probably include a lint and/or skills for this as well, because users or AI might author cards in this way. this would be a very bad performance hit to our indexing system if this gets into a card.