Skip to content

feat(superset-embedded-sdk): add comprehensive test suite and JSDoc type documentation - #45957

Merged
hainenber merged 7 commits into
apache:masterfrom
nextjohnp7:feat/sdk-tests
Nov 7, 2026
Merged

hainenber merged 7 commits into
apache:masterfrom
nextjohnp7:feat/sdk-tests

Conversation

@nextjohnp7

Copy link
Copy Markdown
Contributor

📝 Summary

This PR adds comprehensive test coverage and JSDoc type documentation for the superset-embedded-sdk.

🧪 Testing Area

  • 24 tests passing across 3 test files.
  • guestTokenRefresh.test.ts: 16 test cases (epoch/ISO exp, malformed tokens, TTL boundaries).
  • index.test.ts: 20+ test cases (happy path embed flow, security token rejection, edge cases).
  • withTimeout.test.ts: 3 test cases.
  • Also fixed a boundary TTL test assertion (toBeGreaterThan -> toBeGreaterThanOrEqual).

📖 Type Definitions Area

  • Added JSDoc documentation to all major exported types (GuestTokenFetchFn, UiConfigType, EmbedDashboardParams, Size, ThemeMode, etc.).
  • Restored the missing ResolvePermalinkUrlFn type signature.

✅ Verification

  • npx vitest --run --dir src passes (24/24 tests).
  • tsc --noEmit passes with 0 errors.

Comment on lines +165 to +180
it("handles time travel - system time moves forward", () => {
// Time travel: set time forward, verify timing still calculates correctly
vi.useFakeTimers();
vi.setSystemTime(new Date("2022-03-03 01:00"));

const ttl = 1300;
const exp = Date.now() / 1000 + ttl;
const fakeToken = makeFakeJWT({ exp });

const timing = getGuestTokenRefreshTiming(fakeToken);

expect(timing).toBeGreaterThan(MIN_REFRESH_WAIT_MS);
expect(timing).toBe(ttl * 1000 - REFRESH_TIMING_BUFFER_MS);

vi.useRealTimers();
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: This switches fake timers off before the remaining tests; their exact refresh-delay checks then use advancing wall-clock time and can fail depending on elapsed milliseconds.

Assessment: 🟠 Major · 🔁 Occurrence: Sometimes · 🏷️ Possible bug

Use CodeAnt Skill Fix in Cursor Fix in VSCode Claude

Prompt for AI Agent 🤖
This is a comment left during a code review.

**Path:** superset-embedded-sdk/src/guestTokenRefresh.test.ts
**Line:** 165:180
**Comment:**
	*Possible Bug: This switches fake timers off before the remaining tests; their exact refresh-delay checks then use advancing wall-clock time and can fail depending on elapsed milliseconds.

Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix
👍 | 👎

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ CodeAnt verified this suggestion was addressed in subsequent commits and marked this thread resolved as of 9a187fc.

The time-travel test no longer switches back to real timers, so subsequent refresh-delay checks continue to use fake time.

If that's not right, unresolve this thread and CodeAnt will leave it open.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ CodeAnt verified this suggestion was addressed in subsequent commits and marked this thread resolved as of 5aa688b.

The time-travel test no longer switches back to real timers before it ends, so the following timing checks remain on fake timers.

If that's not right, unresolve this thread and CodeAnt will leave it open.

Comment thread superset-embedded-sdk/src/index.test.ts Outdated
Comment on lines +568 to +579
it("rejects fake guest token", async () => {
const mockFetch = vi.fn().mockResolvedValue("invalid-token");

await expect(
embedDashboard({
id: "test-dashboard",
supersetDomain: "http://localhost:8200",
mountPoint,
fetchGuestToken: mockFetch,
})
).rejects.toThrow();
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: The SDK forwards fetchGuestToken strings without validating JWTs, so this mock resolves embedding and the rejection assertion fails; the mocked switchboard cannot represent server-side token rejection.

Assessment: 🟠 Major · 🔁 Occurrence: Often · 🏷️ Api mismatch

Use CodeAnt Skill Fix in Cursor Fix in VSCode Claude

Prompt for AI Agent 🤖
This is a comment left during a code review.

**Path:** superset-embedded-sdk/src/index.test.ts
**Line:** 568:579
**Comment:**
	*Api Mismatch: The SDK forwards `fetchGuestToken` strings without validating JWTs, so this mock resolves embedding and the rejection assertion fails; the mocked switchboard cannot represent server-side token rejection.

Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix
👍 | 👎

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ CodeAnt verified this suggestion was addressed in subsequent commits and marked this thread resolved as of 5aa688b.

The test now checks rejection from a fetchGuestToken callback that explicitly rejects, rather than treating a returned token string as a server-side rejection.

If that's not right, unresolve this thread and CodeAnt will leave it open.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ CodeAnt verified this suggestion was addressed in subsequent commits and marked this thread resolved as of e718e62.

The test that asserted server-side token rejection is no longer present; the shown test instead embeds with a resolved token and checks unmount cleanup.

If that's not right, unresolve this thread and CodeAnt will leave it open.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ CodeAnt verified this suggestion was addressed in subsequent commits and marked this thread resolved as of da47442.

The test now uses a resolved token to verify unmount behavior and no longer asserts that the mocked switchboard rejects an invalid token.

If that's not right, unresolve this thread and CodeAnt will leave it open.

Comment thread superset-embedded-sdk/src/index.test.ts Outdated
Comment on lines +581 to +598
it("handles token expiration gracefully", async () => {
let callCount = 0;
const mockFetch = vi.fn().mockImplementation(() => {
callCount++;
if (callCount === 1) return Promise.reject(new Error("Token expired"));
return Promise.resolve("new-valid-token");
});

await expect(embedding).rejects.toThrow("host down");
expect(mountPoint.querySelector("iframe")).toBeNull();
expect(ports[0].close).toHaveBeenCalled();
const result = await embedDashboard({
id: "test-dashboard",
supersetDomain: "http://localhost:8200",
mountPoint,
fetchGuestToken: mockFetch,
guestTokenFetchTimeoutMs: 5000,
});

expect(result).toBeDefined();
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: fetchGuestToken rejects on the initial call, which embedDashboard rethrows and tears down; retries occur only during later refreshes, so this test rejects before result is assigned.

Assessment: 🟠 Major · 🔁 Occurrence: Often · 🏷️ Api mismatch

Use CodeAnt Skill Fix in Cursor Fix in VSCode Claude

Prompt for AI Agent 🤖
This is a comment left during a code review.

**Path:** superset-embedded-sdk/src/index.test.ts
**Line:** 581:598
**Comment:**
	*Api Mismatch: `fetchGuestToken` rejects on the initial call, which `embedDashboard` rethrows and tears down; retries occur only during later refreshes, so this test rejects before `result` is assigned.

Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix
👍 | 👎

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ CodeAnt verified this suggestion was addressed in subsequent commits and marked this thread resolved as of 5aa688b.

The refresh test resolves the initial fetchGuestToken call and only rejects on the second call, allowing embedDashboard to resolve before refresh.

If that's not right, unresolve this thread and CodeAnt will leave it open.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ CodeAnt verified this suggestion was addressed in subsequent commits and marked this thread resolved as of e718e62.

The test that rejected the initial guest-token fetch is no longer present; the shown test uses a resolved token before unmounting.

If that's not right, unresolve this thread and CodeAnt will leave it open.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ CodeAnt verified this suggestion was addressed in subsequent commits and marked this thread resolved as of da47442.

The initial token request now resolves with a valid fake JWT; the test awaits embedding before triggering and resolving the later refresh.

If that's not right, unresolve this thread and CodeAnt will leave it open.

@codeant-ai-for-open-source

codeant-ai-for-open-source Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

CodeAnt  CodeAnt PR Risk

🟢  Low Risk

  • The PR appears safe to merge; the refresh test verifies recovery after a failed refresh without rejecting the initial token request.
  • The unmount test uses a resolved initial token and checks that a later refresh sends no message after cleanup.

Assessed commit: da47442af764

@sadpandajoe sadpandajoe added the review:checkpoint Last PR reviewed during the daily review standup label Nov 2, 2026
@netlify

netlify Bot commented Nov 2, 2026

Copy link
Copy Markdown

✅ Deploy Preview for superset-docs-preview ready!

Name Link
🔨 Latest commit 9a187fc
🔍 Latest deploy log https://app.netlify.com/projects/superset-docs-preview/deploys/6ae8d86ba090ecc5fb8f3b19
😎 Deploy Preview https://deploy-preview-45957--superset-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@gabotorresruiz gabotorresruiz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @nextjohnp7, thanks for putting time into the SDK tests and docs! Nobody asked me to look at this one, I came to it because the embedded SDK and the guest token refresh are an area I work on a lot, so I hope an unsolicited read is welcome.

I ran the SDK suite and tsc on this branch and left a few inline comments. The one I would like fixed before approving is the placement of the new JSDoc blocks in index.ts, since that is what consumers of the published package see on hover. The test comments are smaller and I included the code for each. Happy to dig in with you on any of them.

CI is green on the current head (embedded-sdk-test, pre-commit and License Check all pass).

Comment thread superset-embedded-sdk/src/index.ts Resolved
Comment thread superset-embedded-sdk/src/index.test.ts Resolved
Comment thread superset-embedded-sdk/src/index.test.ts Resolved
@sadpandajoe sadpandajoe removed the review:checkpoint Last PR reviewed during the daily review standup label Nov 3, 2026
@nextjohnp7

Copy link
Copy Markdown
Contributor Author

Hi @gabotorresruiz, thank you so much for the voluntary review! I really appreciate you taking the time to look into the SDK.

I've applied all the suggestions you made:

  • Moved the JSDoc blocks to their correct places above the types.
  • Removed the duplicated tests in guestTokenRefresh.test.ts.
  • Removed the redundant handles iframe navigation internally test.

Let me know if there's anything else that needs tweaking!

@gabotorresruiz gabotorresruiz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @nextjohnp7, thanks for the quick turnaround! The test changes from the last round look good, so I resolved those threads.

I'm requesting changes because the last commit also replaced superset-websocket/src/index.ts and most of superset-embedded-sdk/src/index.ts, which I'm sure wasn't intended. Details are inline. Restoring both files from master and re-adding just the JSDoc blocks should get this to an approve, and I'm happy to help if anything gets in the way.

I built and ran the tests for both packages on this branch and on master. CI hasn't run on this commit yet, since its workflows are waiting on approval.

Comment thread superset-websocket/src/index.ts Resolved
Comment thread superset-embedded-sdk/src/index.ts Resolved
…pache#45957

- Restore superset-websocket/src/index.ts from master (accidental SDK code replacement)
- Restore superset-embedded-sdk/src/index.ts from master (embedDashboard rewrite)
- Move JSDoc blocks above their export types (UiConfigType, EmbedDashboardParams, Size, ThemeMode)
- Restore existing tests from master; add only new test cases
- Add 5 guest token refresh timing tests
- Add 3 embedDashboard tests (refresh retry, unmount, iframe channel handoff)
@nextjohnp7

Copy link
Copy Markdown
Contributor Author

Hi @gabotorresruiz, thank you again for your thorough review! I've addressed all the points you raised:

Changes Made

  1. Restored superset-websocket/src/index.ts from master — The accidental SDK code replacement is reverted; the websocket server is intact again.

  2. Restored superset-embedded-sdk/src/index.ts from master — The embedDashboard rewrite is reverted, so the iframe channel handoff (postMessage with port2), uiConfig bitmask, guestTokenFetchTimeoutMs, and ThemeMode behavior all match master.

  3. Moved JSDoc blocks above their correct export types — UiConfigType, EmbedDashboardParams, Size, and ThemeMode now each have their doc block directly above the export type declaration, so consumers see the correct documentation on hover and in dist/index.d.ts.

  4. Restored existing tests from master — No modifications to existing test logic or assertions in guestTokenRefresh.test.ts or index.test.ts. Only new test cases were added.

Tests Added

  • guestTokenRefresh.test.ts: 5 new tests (very long TTL, TTL just above minimum, time travel, expires immediately, buffer period)
  • index.test.ts: 3 new tests (refresh retry delay, unmount during token refresh, iframe channel handoff)

Verification

  • ✅ 35/35 tests passing (vitest --run --dir src)
  • ✅ tsc --noEmit passes with 0 errors
  • ✅ Pre-commit checks pass
  • ✅ superset-websocket/src/index.ts has zero diff against master

The branch is ready for re-review. Let me know if anything else needs adjustment!


@gabotorresruiz gabotorresruiz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @nextjohnp7, this turned around quickly. superset-websocket/src/index.ts is untouched, index.ts is master plus the doc blocks (each one sits on its own type now), and the existing tests are untouched. I ran the SDK suite here (35 of 35) and broke index.ts and guestTokenRefresh.ts on purpose to check the new tests: the postMessage handoff and the retry delay are now pinned. Approving. The workflows on this PR are sitting at action_required for first-time contributor approval, so only the labeler and the docs preview have run.

Comment thread superset-embedded-sdk/src/index.test.ts Resolved
@nextjohnp7

Copy link
Copy Markdown
Contributor Author

Hi @gabotorresruiz @sadpandajoe, I've pushed commit da47442 with the additional refresh-after-unmount test. All 36 tests pass locally and tsc --noEmit is clean. Could someone please approve the pending CI workflows so this PR can be merged?

@gabotorresruiz gabotorresruiz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @nextjohnp7, a refresh that resolves after unmount sends nothing is exactly what was missing. I ran the SDK suite at da47442a (36 of 36), then removed the unmounted guards from refreshGuestToken and deliverGuestToken: the new test is the only one that fails. CI is green here as well, so the workflows have already been approved. Still approving.

@hainenber
hainenber merged commit b92fcb2 into apache:master Nov 7, 2026
67 checks passed
hainenber pushed a commit that referenced this pull request Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants