Copilot commented on code in PR #5461:
URL: https://github.com/apache/texera/pull/5461#discussion_r3447696122


##########
frontend/src/app/common/service/blob-error-http-interceptor.service.spec.ts:
##########
@@ -0,0 +1,132 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { HttpErrorResponse, HttpEvent, HttpHandler, HttpRequest, HttpResponse 
} from "@angular/common/http";
+import { Observable, firstValueFrom, of, throwError } from "rxjs";

Review Comment:
   The structured-error path is supposed to preserve the original `headers` 
(the interceptor passes `headers: err.headers`), but this spec doesn’t 
currently assert that. Adding a custom header to the original 
`HttpErrorResponse` and asserting it’s present on the rejected error will 
better lock in the intended behavior.



##########
frontend/src/app/common/service/blob-error-http-interceptor.service.spec.ts:
##########
@@ -0,0 +1,132 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { HttpErrorResponse, HttpEvent, HttpHandler, HttpRequest, HttpResponse 
} from "@angular/common/http";
+import { Observable, firstValueFrom, of, throwError } from "rxjs";
+
+import { BlobErrorHttpInterceptor } from 
"./blob-error-http-interceptor.service";
+
+/**
+ * The interceptor is a pure function of (req, next), so the specs drive it
+ * directly with a stub `HttpHandler` rather than through HttpClient. Two of
+ * the branches under test — a non-`HttpErrorResponse` error and a
+ * `FileReader` failure — cannot be produced through `HttpClient` at all
+ * (it always wraps errors as `HttpErrorResponse`, and a readable Blob never
+ * triggers `FileReader.onerror`), so direct invocation is the only way to
+ * cover them.
+ */
+describe("BlobErrorHttpInterceptor", () => {
+  let interceptor: BlobErrorHttpInterceptor;
+  const req = new HttpRequest("GET", "/test");
+
+  const handlerReturning = (obs: Observable<HttpEvent<any>>): HttpHandler => ({
+    handle: (_req: HttpRequest<any>) => obs,
+  });
+
+  // Run the interceptor and resolve to the emitted value or, on error, the 
error.
+  const run = (next: HttpHandler): Promise<any> => 
firstValueFrom(interceptor.intercept(req, next)).catch(e => e);
+
+  beforeEach(() => {
+    interceptor = new BlobErrorHttpInterceptor();
+  });
+
+  afterEach(() => {
+    vi.unstubAllGlobals();
+  });
+
+  it("passes a successful response through unchanged", async () => {
+    const response = new HttpResponse({ body: "ok", status: 200 });
+    expect(await run(handlerReturning(of(response)))).toBe(response);
+  });
+
+  it("re-throws an error that is not an HttpErrorResponse unchanged", async () 
=> {
+    const err = new Error("not-http");
+    expect(await run(handlerReturning(throwError(() => err)))).toBe(err);
+  });
+
+  it("re-throws an HttpErrorResponse whose error is not a Blob unchanged", 
async () => {
+    const err = new HttpErrorResponse({ error: { message: "plain" }, status: 
500 });
+    expect(await run(handlerReturning(throwError(() => err)))).toBe(err);
+  });
+
+  it("re-throws an HttpErrorResponse with a non-json Blob unchanged", async () 
=> {
+    const err = new HttpErrorResponse({
+      error: new Blob(["whatever"], { type: "text/plain" }),
+      status: 500,
+    });
+    expect(await run(handlerReturning(throwError(() => err)))).toBe(err);
+  });
+
+  it("parses an application/json Blob error into a structured 
HttpErrorResponse", async () => {
+    const err = new HttpErrorResponse({
+      error: new Blob([JSON.stringify({ message: "Boom" })], { type: 
"application/json" }),
+      status: 502,
+      statusText: "Bad Gateway",
+      url: "http://example.com/api";,
+    });
+
+    const rejected = await run(handlerReturning(throwError(() => err)));
+
+    expect(rejected).toBeInstanceOf(HttpErrorResponse);
+    expect(rejected.error).toEqual({ message: "Boom" });
+    expect(rejected.status).toBe(502);
+    expect(rejected.statusText).toBe("Bad Gateway");
+    expect(rejected.url).toBe("http://example.com/api";);
+  });
+
+  it("normalizes a null url to undefined when building the structured error", 
async () => {
+    const err = new HttpErrorResponse({
+      error: new Blob([JSON.stringify({ message: "Boom" })], { type: 
"application/json" }),
+      status: 500,
+      // url omitted → HttpErrorResponse defaults it to null, exercising the
+      // `err.url !== null ? err.url : undefined` false branch.
+    });
+
+    const rejected = await run(handlerReturning(throwError(() => err)));
+
+    expect(rejected).toBeInstanceOf(HttpErrorResponse);
+    expect(rejected.error).toEqual({ message: "Boom" });
+    expect(rejected.url).toBeNull();
+  });

Review Comment:
   This test claims the interceptor “normalizes a null url to undefined”, but 
it currently asserts `rejected.url` is `null`. That’s misleading (and doesn’t 
prove a new `HttpErrorResponse` was constructed). Consider renaming the test to 
reflect that it’s exercising the null-url branch, and assert the structured 
error is a *new* instance (not the original `err`).



##########
frontend/src/app/common/service/blob-error-http-interceptor.service.spec.ts:
##########
@@ -0,0 +1,132 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { HttpErrorResponse, HttpEvent, HttpHandler, HttpRequest, HttpResponse 
} from "@angular/common/http";
+import { Observable, firstValueFrom, of, throwError } from "rxjs";
+
+import { BlobErrorHttpInterceptor } from 
"./blob-error-http-interceptor.service";
+
+/**
+ * The interceptor is a pure function of (req, next), so the specs drive it
+ * directly with a stub `HttpHandler` rather than through HttpClient. Two of
+ * the branches under test — a non-`HttpErrorResponse` error and a
+ * `FileReader` failure — cannot be produced through `HttpClient` at all
+ * (it always wraps errors as `HttpErrorResponse`, and a readable Blob never
+ * triggers `FileReader.onerror`), so direct invocation is the only way to
+ * cover them.
+ */
+describe("BlobErrorHttpInterceptor", () => {
+  let interceptor: BlobErrorHttpInterceptor;
+  const req = new HttpRequest("GET", "/test");
+
+  const handlerReturning = (obs: Observable<HttpEvent<any>>): HttpHandler => ({
+    handle: (_req: HttpRequest<any>) => obs,
+  });
+
+  // Run the interceptor and resolve to the emitted value or, on error, the 
error.
+  const run = (next: HttpHandler): Promise<any> => 
firstValueFrom(interceptor.intercept(req, next)).catch(e => e);
+
+  beforeEach(() => {
+    interceptor = new BlobErrorHttpInterceptor();
+  });
+
+  afterEach(() => {
+    vi.unstubAllGlobals();
+  });
+
+  it("passes a successful response through unchanged", async () => {
+    const response = new HttpResponse({ body: "ok", status: 200 });
+    expect(await run(handlerReturning(of(response)))).toBe(response);
+  });
+
+  it("re-throws an error that is not an HttpErrorResponse unchanged", async () 
=> {
+    const err = new Error("not-http");
+    expect(await run(handlerReturning(throwError(() => err)))).toBe(err);
+  });
+
+  it("re-throws an HttpErrorResponse whose error is not a Blob unchanged", 
async () => {
+    const err = new HttpErrorResponse({ error: { message: "plain" }, status: 
500 });
+    expect(await run(handlerReturning(throwError(() => err)))).toBe(err);
+  });
+
+  it("re-throws an HttpErrorResponse with a non-json Blob unchanged", async () 
=> {
+    const err = new HttpErrorResponse({
+      error: new Blob(["whatever"], { type: "text/plain" }),
+      status: 500,
+    });
+    expect(await run(handlerReturning(throwError(() => err)))).toBe(err);
+  });
+
+  it("parses an application/json Blob error into a structured 
HttpErrorResponse", async () => {
+    const err = new HttpErrorResponse({
+      error: new Blob([JSON.stringify({ message: "Boom" })], { type: 
"application/json" }),
+      status: 502,
+      statusText: "Bad Gateway",
+      url: "http://example.com/api";,
+    });
+
+    const rejected = await run(handlerReturning(throwError(() => err)));
+
+    expect(rejected).toBeInstanceOf(HttpErrorResponse);
+    expect(rejected.error).toEqual({ message: "Boom" });
+    expect(rejected.status).toBe(502);
+    expect(rejected.statusText).toBe("Bad Gateway");
+    expect(rejected.url).toBe("http://example.com/api";);
+  });

Review Comment:
   This test validates the parsed JSON and status fields, but it doesn’t verify 
that the interceptor preserves the original `headers` (or that it actually 
returns a *new* `HttpErrorResponse` instance). Adding a custom header to the 
original error and asserting it’s present on the structured error would make 
the test more robust.



-- 
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]

Reply via email to