codeant-ai-for-open-source[bot] commented on code in PR #42157:
URL: https://github.com/apache/superset/pull/42157#discussion_r3607423570


##########
superset-frontend/packages/superset-ui-core/src/chart/components/StatefulChart.test.tsx:
##########
@@ -720,6 +720,435 @@ test('should refetch when mixing renderTrigger string 
control with non-renderTri
   });
 });
 
+test('resolves async (202) responses via the injected handleAsyncChartData 
hook', async () => {
+  const asyncJob = {
+    channel_id: 'c1',
+    job_id: 'j1',
+    status: 'running',
+    result_url: '/api/v1/chart/data/abc',
+  };
+  mockChartClient.client.post.mockResolvedValue({
+    response: { status: 202 } as Response,
+    json: asyncJob,
+  });
+  const handleAsyncChartData = jest
+    .fn()
+    .mockResolvedValue([{ data: 'async result' }]);
+
+  const { getByTestId } = render(
+    <StatefulChart
+      formData={mockFormData}
+      chartType="test_chart"
+      hooks={{ handleAsyncChartData }}
+    />,
+  );
+
+  await waitFor(() => {
+    expect(handleAsyncChartData).toHaveBeenCalledTimes(1);
+  });
+  // Delegates the raw response + job metadata (and useLegacyApi + abort 
signal)
+  expect(handleAsyncChartData).toHaveBeenCalledWith(
+    { status: 202 },
+    asyncJob,
+    false,
+    expect.any(AbortSignal),
+  );
+  // Chart renders once the async data resolves
+  await waitFor(() => {
+    expect(getByTestId('super-chart')).toBeInTheDocument();
+  });
+});
+
+test('errors on async (202) response when no async handler is provided', async 
() => {
+  mockChartClient.client.post.mockResolvedValue({
+    response: { status: 202 } as Response,
+    json: { job_id: 'j1', channel_id: 'c1', status: 'running' },
+  });
+  const onError = jest.fn();
+
+  const { findByText } = render(
+    <StatefulChart
+      formData={mockFormData}
+      chartType="test_chart"
+      onError={onError}
+    />,
+  );
+
+  // Fails loudly instead of rendering the job metadata as empty data
+  expect(await findByText(/async handler/i)).toBeInTheDocument();
+  await waitFor(() => {
+    expect(onError).toHaveBeenCalledTimes(1);
+  });
+});
+
+test('renders synchronous (200) responses that include a response object', 
async () => {
+  mockChartClient.client.post.mockResolvedValue({
+    response: { status: 200 } as Response,
+    json: [{ result: [{ data: 'sync result' }] }],
+  });
+
+  const { getByTestId } = render(
+    <StatefulChart formData={mockFormData} chartType="test_chart" />,
+  );
+
+  await waitFor(() => {
+    expect(getByTestId('super-chart')).toBeInTheDocument();
+  });
+  // Synchronous path: no async handler needed, single request
+  expect(mockChartClient.client.post).toHaveBeenCalledTimes(1);
+});
+
+test('wraps the legacy async body as { result: [body] } for the async 
handler', async () => {
+  const legacyBody = { job_id: 'j1', channel_id: 'c1', status: 'running' };
+  mockChartClient.client.post.mockResolvedValue({
+    response: { status: 202 } as Response,
+    json: legacyBody,
+  });
+  // Force the legacy API path for this viz type
+  (getChartMetadataRegistry as any).mockReturnValue({

Review Comment:
   **Suggestion:** Replace the `any` cast with a concrete mocked function type 
so the registry mock remains type-safe. [custom_rule]
   
   **Severity Level:** Minor ๐Ÿงน
   <details>
   <summary><b>Why it matters? โญ </b></summary>
   
   The final file still contains a changed TypeScript line casting 
`getChartMetadataRegistry` to `any` before calling `mockReturnValue`, which 
directly violates the no-any-types rule.
   </details>
   <details>
   <summary><b>Rule source ๐Ÿ“– </b></summary>
   
   .cursor/rules/dev-standard.mdc (line 16)
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=2999fd71b98b48feafab7d4c53627fc7&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=2999fd71b98b48feafab7d4c53627fc7&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   
   *(Use Cmd/Ctrl + Click for best experience)*
   <details>
   <summary><b>Prompt for AI Agent ๐Ÿค– </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** 
superset-frontend/packages/superset-ui-core/src/chart/components/StatefulChart.test.tsx
   **Line:** 808:808
   **Comment:**
        *Custom Rule: Replace the `any` cast with a concrete mocked function 
type so the registry mock remains type-safe.
   
   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
   ```
   </details>
   <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42157&comment_hash=ec6796dedb23a030bd24b72e1c6e00de0f4045b205c564f802b38e5d1f31fb16&reaction=like'>๐Ÿ‘</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42157&comment_hash=ec6796dedb23a030bd24b72e1c6e00de0f4045b205c564f802b38e5d1f31fb16&reaction=dislike'>๐Ÿ‘Ž</a>



##########
superset-frontend/packages/superset-ui-core/src/chart/components/StatefulChart.test.tsx:
##########
@@ -720,6 +720,435 @@ test('should refetch when mixing renderTrigger string 
control with non-renderTri
   });
 });
 
+test('resolves async (202) responses via the injected handleAsyncChartData 
hook', async () => {
+  const asyncJob = {
+    channel_id: 'c1',
+    job_id: 'j1',
+    status: 'running',
+    result_url: '/api/v1/chart/data/abc',
+  };
+  mockChartClient.client.post.mockResolvedValue({
+    response: { status: 202 } as Response,
+    json: asyncJob,
+  });
+  const handleAsyncChartData = jest
+    .fn()
+    .mockResolvedValue([{ data: 'async result' }]);
+
+  const { getByTestId } = render(
+    <StatefulChart
+      formData={mockFormData}
+      chartType="test_chart"
+      hooks={{ handleAsyncChartData }}
+    />,
+  );
+
+  await waitFor(() => {
+    expect(handleAsyncChartData).toHaveBeenCalledTimes(1);
+  });
+  // Delegates the raw response + job metadata (and useLegacyApi + abort 
signal)
+  expect(handleAsyncChartData).toHaveBeenCalledWith(
+    { status: 202 },
+    asyncJob,
+    false,
+    expect.any(AbortSignal),
+  );
+  // Chart renders once the async data resolves
+  await waitFor(() => {
+    expect(getByTestId('super-chart')).toBeInTheDocument();
+  });
+});
+
+test('errors on async (202) response when no async handler is provided', async 
() => {
+  mockChartClient.client.post.mockResolvedValue({
+    response: { status: 202 } as Response,
+    json: { job_id: 'j1', channel_id: 'c1', status: 'running' },
+  });
+  const onError = jest.fn();
+
+  const { findByText } = render(
+    <StatefulChart
+      formData={mockFormData}
+      chartType="test_chart"
+      onError={onError}
+    />,
+  );
+
+  // Fails loudly instead of rendering the job metadata as empty data
+  expect(await findByText(/async handler/i)).toBeInTheDocument();
+  await waitFor(() => {
+    expect(onError).toHaveBeenCalledTimes(1);
+  });
+});
+
+test('renders synchronous (200) responses that include a response object', 
async () => {
+  mockChartClient.client.post.mockResolvedValue({
+    response: { status: 200 } as Response,
+    json: [{ result: [{ data: 'sync result' }] }],
+  });
+
+  const { getByTestId } = render(
+    <StatefulChart formData={mockFormData} chartType="test_chart" />,
+  );
+
+  await waitFor(() => {
+    expect(getByTestId('super-chart')).toBeInTheDocument();
+  });
+  // Synchronous path: no async handler needed, single request
+  expect(mockChartClient.client.post).toHaveBeenCalledTimes(1);
+});
+
+test('wraps the legacy async body as { result: [body] } for the async 
handler', async () => {
+  const legacyBody = { job_id: 'j1', channel_id: 'c1', status: 'running' };
+  mockChartClient.client.post.mockResolvedValue({
+    response: { status: 202 } as Response,
+    json: legacyBody,
+  });
+  // Force the legacy API path for this viz type
+  (getChartMetadataRegistry as any).mockReturnValue({
+    get: jest.fn().mockReturnValue({ useLegacyApi: true }),
+  });
+  const handleAsyncChartData = jest
+    .fn()
+    .mockResolvedValue([{ data: 'legacy result' }]);
+
+  const { getByTestId } = render(
+    <StatefulChart
+      formData={mockFormData}
+      chartType="test_chart"
+      hooks={{ handleAsyncChartData }}
+    />,
+  );
+
+  await waitFor(() => {
+    expect(handleAsyncChartData).toHaveBeenCalledTimes(1);
+  });
+  // Legacy body must be wrapped to match the V1 response signature
+  expect(handleAsyncChartData).toHaveBeenCalledWith(
+    { status: 202 },
+    { result: [legacyBody] },
+    true,
+    expect.any(AbortSignal),
+  );
+  await waitFor(() => {
+    expect(getByTestId('super-chart')).toBeInTheDocument();
+  });
+});
+
+test('does not apply a superseded async response over a newer one', async () 
=> {
+  mockChartClient.client.post.mockResolvedValue({
+    response: { status: 202 } as Response,
+    json: { job_id: 'j', channel_id: 'c' },
+  });
+  let resolveFirst: (data: unknown) => void = () => {};
+  let resolveSecond: (data: unknown) => void = () => {};
+  const handleAsyncChartData = jest
+    .fn()
+    .mockImplementationOnce(
+      () =>
+        new Promise(resolve => {
+          resolveFirst = resolve;
+        }),
+    )
+    .mockImplementationOnce(
+      () =>
+        new Promise(resolve => {
+          resolveSecond = resolve;
+        }),
+    );
+  const onLoad = jest.fn();
+
+  const { rerender } = render(
+    <StatefulChart
+      formData={mockFormData}
+      chartType="test_chart"
+      hooks={{ handleAsyncChartData }}
+      onLoad={onLoad}
+    />,
+  );
+
+  await waitFor(() => {
+    expect(handleAsyncChartData).toHaveBeenCalledTimes(1);
+  });
+
+  // A newer request supersedes the first (viz_type change forces a refetch)
+  const newFormData = { ...mockFormData, viz_type: 'different_chart' };
+  rerender(
+    <StatefulChart
+      formData={newFormData}
+      chartType="different_chart"
+      hooks={{ handleAsyncChartData }}
+      onLoad={onLoad}
+    />,
+  );
+
+  await waitFor(() => {
+    expect(handleAsyncChartData).toHaveBeenCalledTimes(2);
+  });
+
+  // Resolve the newer request first, then the stale one
+  await act(async () => {
+    resolveSecond([{ data: 'B' }]);
+  });
+  await act(async () => {
+    resolveFirst([{ data: 'A' }]);
+  });
+
+  await waitFor(() => {
+    expect(onLoad).toHaveBeenCalledWith([{ data: 'B' }]);
+  });
+  // The stale (superseded) response must not overwrite the newer one
+  expect(onLoad).not.toHaveBeenCalledWith([{ data: 'A' }]);
+  expect(onLoad).toHaveBeenCalledTimes(1);
+});
+
+test('preserves the detailed message from an async (array) rejection', async 
() => {
+  mockChartClient.client.post.mockResolvedValue({
+    response: { status: 202 } as Response,
+    json: { job_id: 'j', channel_id: 'c' },
+  });
+  const handleAsyncChartData = jest
+    .fn()
+    .mockRejectedValue([{ error: 'Async query failed: table not found' }]);
+  const onError = jest.fn();
+
+  const { findByText } = render(
+    <StatefulChart
+      formData={mockFormData}
+      chartType="test_chart"
+      hooks={{ handleAsyncChartData }}
+      onError={onError}
+    />,
+  );
+
+  // The detailed message survives instead of collapsing to the generic one
+  expect(await findByText(/table not found/i)).toBeInTheDocument();
+  await waitFor(() => {
+    expect(onError).toHaveBeenCalledTimes(1);
+    expect(onError.mock.calls[0][0].message).toContain('table not found');
+  });
+});
+
+test('refetches with the latest formData rather than the initial props', async 
() => {
+  mockChartClient.client.post.mockResolvedValue({
+    response: { status: 200 } as Response,
+    json: [{ result: [{ data: 'x' }] }],
+  });
+
+  const { rerender } = render(
+    <StatefulChart
+      formData={{ ...mockFormData, metrics: ['metric_v1'] }}
+      chartType="test_chart"
+    />,
+  );
+  await waitFor(() => {
+    expect(mockChartClient.client.post).toHaveBeenCalledTimes(1);
+  });
+
+  // Change a data-affecting control -> triggers a refetch
+  rerender(
+    <StatefulChart
+      formData={{ ...mockFormData, metrics: ['metric_v2'] }}
+      chartType="test_chart"
+    />,
+  );
+  await waitFor(() => {
+    expect(mockChartClient.client.post).toHaveBeenCalledTimes(2);
+  });
+
+  // The second request must carry the updated formData, not the initial props
+  const secondRequestConfig = mockChartClient.client.post.mock.calls[1][0];
+  expect(JSON.stringify(secondRequestConfig)).toContain('metric_v2');
+  expect(JSON.stringify(secondRequestConfig)).not.toContain('metric_v1');
+});
+
+test('does not revert a render-only change when a slow async request 
resolves', async () => {
+  mockChartClient.client.post.mockResolvedValue({
+    response: { status: 202 } as Response,
+    json: { job_id: 'j', channel_id: 'c' },
+  });
+  // color_scheme is a renderTrigger control -> its change does not refetch
+  (getChartControlPanelRegistry as any).mockReturnValue({

Review Comment:
   **Suggestion:** Use a specific mock/registry type instead of casting to 
`any` when overriding the control panel registry return value. [custom_rule]
   
   **Severity Level:** Minor ๐Ÿงน
   <details>
   <summary><b>Why it matters? โญ </b></summary>
   
   The final file still contains a changed TypeScript line casting 
`getChartControlPanelRegistry` to `any` before calling `mockReturnValue`, which 
is a real instance of the prohibited `any` usage.
   </details>
   <details>
   <summary><b>Rule source ๐Ÿ“– </b></summary>
   
   .cursor/rules/dev-standard.mdc (line 16)
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=0e54bafd88264c15ad65ae2d67a164f8&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=0e54bafd88264c15ad65ae2d67a164f8&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   
   *(Use Cmd/Ctrl + Click for best experience)*
   <details>
   <summary><b>Prompt for AI Agent ๐Ÿค– </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** 
superset-frontend/packages/superset-ui-core/src/chart/components/StatefulChart.test.tsx
   **Line:** 971:971
   **Comment:**
        *Custom Rule: Use a specific mock/registry type instead of casting to 
`any` when overriding the control panel registry return value.
   
   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
   ```
   </details>
   <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42157&comment_hash=015921f7c66d715e2f4e2955fd8814c4a6e29cb3baf39210fb177e7a8bb0e73d&reaction=like'>๐Ÿ‘</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42157&comment_hash=015921f7c66d715e2f4e2955fd8814c4a6e29cb3baf39210fb177e7a8bb0e73d&reaction=dislike'>๐Ÿ‘Ž</a>



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to