-
Notifications
You must be signed in to change notification settings - Fork 37
feat: create shared contract test utilities package (SDK-1866) #1151
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
Closed
Closed
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
62adbbd
feat: create shared contract test utilities package (SDK-1866)
devin-ai-integration[bot] d5fedb9
fix: resolve CI failures - point to source files and fix import paths
devin-ai-integration[bot] d16c527
fix: split into subpath exports to avoid cross-dependency resolution
devin-ai-integration[bot] 8b8163d
refactor: move contract-test-utils to packages/tooling/ and use local…
devin-ai-integration[bot] 987b0c2
fix: compile shared package to dist/ and build before contract tests
devin-ai-integration[bot] bb68d02
fix: split exports - source for client/base, compiled for server subpath
devin-ai-integration[bot] fd38015
fix: inline shared package in shopify-oxygen tsup bundle
devin-ai-integration[bot] d9bf413
Merge remote-tracking branch 'origin/main' into devin/SDK-1866-177264…
devin-ai-integration[bot] d04d7ae
feat: migrate electron and react-native contract tests to shared package
devin-ai-integration[bot] 496d545
fix: resolve prettier import ordering in electron and react-native co…
devin-ai-integration[bot] 68929cb
fix: enable package exports in Metro config for subpath import resolu…
devin-ai-integration[bot] ce6a745
fix: add custom Metro resolver for TypeScript .js extension convention
devin-ai-integration[bot] 5b4b3c1
refactor: extract shared adapter implementation for browser and react…
devin-ai-integration[bot] 961ba7b
ci: re-trigger CI checks
devin-ai-integration[bot] e79615f
style: consolidate eslint-disable comments in adapter
devin-ai-integration[bot] 35e3007
fix: pre-build shared package server output before adapter compilation
devin-ai-integration[bot] 2f8a2e3
fix: use adapter-only tsconfig to avoid node-server-sdk dependency in CI
devin-ai-integration[bot] 45b270f
fix: switch adapter packages to ESM to resolve ERR_REQUIRE_ESM at run…
devin-ai-integration[bot] 3860a36
feat: add CLI executable (sdk-testharness-server / sts) with config f…
devin-ai-integration[bot] 498810c
docs: add README.md for contract-test-utils package
devin-ai-integration[bot] 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 was deleted.
Oops, something went wrong.
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
14 changes: 9 additions & 5 deletions
14
packages/sdk/browser/contract-tests/entity/src/ClientEntity.ts
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
90 changes: 0 additions & 90 deletions
90
packages/sdk/browser/contract-tests/entity/src/ConfigParams.ts
This file was deleted.
Oops, something went wrong.
114 changes: 20 additions & 94 deletions
114
packages/sdk/browser/contract-tests/entity/src/TestHarnessWebSocket.ts
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 |
|---|---|---|
| @@ -1,96 +1,22 @@ | ||
| import { LDLogger } from '@launchdarkly/js-client-sdk'; | ||
|
|
||
| import { ClientEntity, newSdkClientEntity } from './ClientEntity'; | ||
| import { makeLogger } from './makeLogger'; | ||
|
|
||
| export default class TestHarnessWebSocket { | ||
| private _ws?: WebSocket; | ||
| private readonly _entities: Record<string, ClientEntity> = {}; | ||
| private _clientCounter = 0; | ||
| private _logger: LDLogger = makeLogger('TestHarnessWebSocket'); | ||
|
|
||
| constructor(private readonly _url: string) {} | ||
|
|
||
| connect() { | ||
| this._logger.info(`Connecting to web socket.`); | ||
| this._ws = new WebSocket(this._url, ['v1']); | ||
| this._ws.onopen = () => { | ||
| this._logger.info('Connected to websocket.'); | ||
| }; | ||
| this._ws.onclose = () => { | ||
| this._logger.info('Websocket closed. Attempting to reconnect in 1 second.'); | ||
| setTimeout(() => { | ||
| this.connect(); | ||
| }, 1000); | ||
| }; | ||
| this._ws.onerror = (err) => { | ||
| this._logger.info(`error:`, err); | ||
| }; | ||
|
|
||
| this._ws.onmessage = async (msg) => { | ||
| this._logger.info('Test harness message', msg); | ||
| const data = JSON.parse(msg.data); | ||
| const resData: any = { reqId: data.reqId }; | ||
| switch (data.command) { | ||
| case 'getCapabilities': | ||
| resData.capabilities = [ | ||
| 'client-side', | ||
| 'service-endpoints', | ||
| 'tags', | ||
| 'user-type', | ||
| 'inline-context-all', | ||
| 'anonymous-redaction', | ||
| 'strongly-typed', | ||
| 'client-prereq-events', | ||
| 'client-per-context-summaries', | ||
| 'track-hooks', | ||
| ]; | ||
|
|
||
| break; | ||
| case 'createClient': | ||
| { | ||
| resData.resourceUrl = `/clients/${this._clientCounter}`; | ||
| resData.status = 201; | ||
| const entity = await newSdkClientEntity(data.body); | ||
| this._entities[this._clientCounter] = entity; | ||
| this._clientCounter += 1; | ||
| } | ||
| break; | ||
| case 'runCommand': | ||
| if (Object.prototype.hasOwnProperty.call(this._entities, data.id)) { | ||
| const entity = this._entities[data.id]; | ||
| const body = await entity.doCommand(data.body); | ||
| resData.body = body; | ||
| resData.status = body ? 200 : 204; | ||
| } else { | ||
| resData.status = 404; | ||
| this._logger.warn(`Client did not exist: ${data.id}`); | ||
| } | ||
|
|
||
| break; | ||
| case 'deleteClient': | ||
| if (Object.prototype.hasOwnProperty.call(this._entities, data.id)) { | ||
| const entity = this._entities[data.id]; | ||
| entity.close(); | ||
| delete this._entities[data.id]; | ||
| } else { | ||
| resData.status = 404; | ||
| this._logger.warn(`Could not delete client because it did not exist: ${data.id}`); | ||
| } | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
|
|
||
| this.send(resData); | ||
| }; | ||
| } | ||
|
|
||
| disconnect() { | ||
| this._ws?.close(); | ||
| } | ||
|
|
||
| send(data: unknown) { | ||
| this._ws?.send(JSON.stringify(data)); | ||
| import { TestHarnessWebSocket as SharedTestHarnessWebSocket } from '@launchdarkly/js-contract-test-utils/client'; | ||
|
|
||
| import { newSdkClientEntity } from './ClientEntity'; | ||
|
|
||
| const CAPABILITIES = [ | ||
| 'client-side', | ||
| 'service-endpoints', | ||
| 'tags', | ||
| 'user-type', | ||
| 'inline-context-all', | ||
| 'anonymous-redaction', | ||
| 'strongly-typed', | ||
| 'client-prereq-events', | ||
| 'client-per-context-summaries', | ||
| 'track-hooks', | ||
| ]; | ||
|
|
||
| export default class TestHarnessWebSocket extends SharedTestHarnessWebSocket { | ||
| constructor(url: string) { | ||
| super(url, CAPABILITIES, newSdkClientEntity); | ||
| } | ||
| } |
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
4 changes: 2 additions & 2 deletions
4
packages/sdk/electron/contract-tests/entity/src/ClientFactory.ts
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
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.
I don't think it makes much sense to have this without the adaptor implementation (eg https://github.com/launchdarkly/js-core/tree/main/packages/sdk/browser/contract-tests/adapter) since the websocket contract is specific to that adaptor implementation. I think it would be good to have a shared adaptor implementation that is executable.
I am already trying to leverage the adaptor implementation from the browser in my react sdk branch (https://github.com/launchdarkly/js-core/tree/skz/react-sdk-next/packages/sdk/react/contract-tests).
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.
Good call — the adapter was indeed duplicated identically between browser and react-native. I've now extracted it into the shared package as
@launchdarkly/js-contract-test-utils/adapter(compiled todist/since it's Node.js code).Both browser and react-native adapters are now one-liners:
The shared
startAdapter()function accepts optional{ wsPort, httpPort }config (defaults to 8001/8000). This should make it easy to reuse in the react SDK contract tests too.See commit 5b4b3c1.
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 think it would be more effective if we made this an executable and read off modifications from a configuration file (like what
jestdoes for example). Reference https://docs.npmjs.com/cli/v11/configuring-npm/package-json#bin and we can call the runnersdk-testharness-server(stsas an alias) and thestartAdapterfunction could be called with the commandsdk-testharness-server adapter.I think the configuration file can be called
contract-test.configand we should support different formats (eg json, js, ts, etc)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.
Implemented the CLI executable and config file support as suggested:
CLI (
sdk-testharness-server/sts):binfield in the shared package'spackage.json(npm bin docs)sdk-testharness-server adaptercallsstartAdapter()with config from the config filestsis registered as an aliasConfig file (
contract-test.config.*):.json,.js,.mjs,.cjs,.ts,.mtsformats (similar to Jest'sjest.config.*)loadConfig()Example
contract-test.config.json:{ "adapter": { "wsPort": 8001, "httpPort": 8000 } }Adapter packages simplified:
Both browser and react-native adapter packages no longer need their own source code or tsconfig — they just delegate to the CLI:
{ "scripts": { "build": "yarn workspace @launchdarkly/js-contract-test-utils build:adapter", "start": "yarn build && sdk-testharness-server adapter" } }Exported
ContractTestConfigtype so consumers can type their config files:All 43 CI checks pass.