Skip to content

Commit a25c543

Browse files
authored
test(frontend): extend code-debugger and code-editor coverage (apache#5191)
### What changes were proposed in this PR? Extends the existing `code-debugger.component.spec.ts` and `code-editor.component.spec.ts` with tests for the class-level surface that the original specs left behind. No Monaco-editor interaction is added — the parts that require a real editor are left for a future browser-mode pass. A latent test-setup bug surfaced while writing the new debugger tests: the spec built `debugState` from `new Y.Map<…>()` directly, which silently never fires observers because the map is not attached to a Y.Doc. Production never hits this — `UdfDebugService` hands out maps from a real doc — but every Y.Map observer test would have stayed at 0 coverage until the map was wired into a doc. Switching to `new Y.Doc().getMap(…)` fixes it. ### Any related issues, documentation, discussions? Closes apache#5190. ### How was this PR tested? `yarn ng test --watch=false --include=…` runs 2 spec files / 39 tests, all green. `yarn lint` and `yarn format:ci` both clean. ### Was this PR authored or co-authored using generative AI tooling? Generated-by: Claude Opus 4.7
1 parent cbe90c7 commit a25c543

2 files changed

Lines changed: 294 additions & 2 deletions

File tree

‎frontend/src/app/workspace/component/code-editor-dialog/code-debugger.component.spec.ts‎

Lines changed: 154 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ describe("CodeDebuggerComponent", () => {
4545
beforeEach(async () => {
4646
// Initialize streams and spy objects
4747
statusUpdateStream = new Subject<Record<string, OperatorStatistics>>();
48-
debugState = new Y.Map<BreakpointInfo>();
48+
// Y.Map observers only fire when the map is attached to a Y.Doc (the doc
49+
// owns the transaction lifecycle that drives observation). A standalone
50+
// `new Y.Map()` accepts `.set()` but never notifies observers — production
51+
// never hits this because `UdfDebugService` hands out maps from a real
52+
// doc, but the spec used to construct a detached map.
53+
debugState = new Y.Doc().getMap<BreakpointInfo>("debug");
4954

5055
mockWorkflowStatusService = { getStatusUpdateStream: vi.fn() } as unknown as Mocked<WorkflowStatusService>;
5156
mockWorkflowStatusService.getStatusUpdateStream.mockReturnValue(statusUpdateStream.asObservable());
@@ -238,4 +243,152 @@ describe("CodeDebuggerComponent", () => {
238243

239244
expect(component.breakpointConditionLine).toBeUndefined();
240245
});
246+
247+
describe("registerBreakpointRenderingHandler", () => {
248+
// Stand-in for the MonacoBreakpoint instance set up in
249+
// setupMonacoBreakpointMethods. The observer in
250+
// registerBreakpointRenderingHandler reaches into these as bracket
251+
// properties, so we wire up the four it touches: createSpecifyDecoration,
252+
// removeSpecifyDecoration, setLineHighlight, removeHighlight, plus the
253+
// lineNumberAndDecorationIdMap reads by removeBreakpointDecoration.
254+
let monacoBreakpointStub: {
255+
createSpecifyDecoration: ReturnType<typeof vi.fn>;
256+
removeSpecifyDecoration: ReturnType<typeof vi.fn>;
257+
setLineHighlight: ReturnType<typeof vi.fn>;
258+
removeHighlight: ReturnType<typeof vi.fn>;
259+
lineNumberAndDecorationIdMap: Map<number, string>;
260+
mouseDownDisposable: { dispose: ReturnType<typeof vi.fn> };
261+
dispose: ReturnType<typeof vi.fn>;
262+
};
263+
264+
beforeEach(() => {
265+
monacoBreakpointStub = {
266+
createSpecifyDecoration: vi.fn(),
267+
removeSpecifyDecoration: vi.fn(),
268+
setLineHighlight: vi.fn(),
269+
removeHighlight: vi.fn(),
270+
lineNumberAndDecorationIdMap: new Map<number, string>(),
271+
mouseDownDisposable: { dispose: vi.fn() },
272+
dispose: vi.fn(),
273+
};
274+
component.monacoBreakpoint = monacoBreakpointStub as unknown as MonacoBreakpoint;
275+
});
276+
277+
it("draws a decoration when a debug-state entry with breakpointId is added", () => {
278+
debugState.set("3", { breakpointId: 3, condition: "", hit: false } as BreakpointInfo);
279+
280+
expect(monacoBreakpointStub.createSpecifyDecoration).toHaveBeenCalledWith({
281+
startLineNumber: 3,
282+
startColumn: 1,
283+
endLineNumber: 3,
284+
endColumn: 1,
285+
});
286+
});
287+
288+
it("does not draw a decoration when the added entry has no breakpointId", () => {
289+
debugState.set("4", { breakpointId: undefined, condition: "", hit: false } as BreakpointInfo);
290+
291+
expect(monacoBreakpointStub.createSpecifyDecoration).not.toHaveBeenCalled();
292+
});
293+
294+
it("removes the decoration when a debug-state entry with breakpointId is deleted", () => {
295+
debugState.set("5", { breakpointId: 5, condition: "", hit: false } as BreakpointInfo);
296+
// Mirror the lookup that removeBreakpointDecoration performs.
297+
monacoBreakpointStub.lineNumberAndDecorationIdMap.set(5, "dec-5");
298+
monacoBreakpointStub.createSpecifyDecoration.mockClear();
299+
300+
debugState.delete("5");
301+
302+
expect(monacoBreakpointStub.removeSpecifyDecoration).toHaveBeenCalledWith("dec-5", 5);
303+
});
304+
305+
it("does not call removeSpecifyDecoration when the deleted entry had no breakpointId", () => {
306+
// Y.Map.observe surfaces a delete with oldValue.breakpointId === undefined
307+
// when the entry never had a breakpoint to begin with.
308+
debugState.set("6", { breakpointId: undefined, condition: "", hit: false } as BreakpointInfo);
309+
debugState.delete("6");
310+
311+
expect(monacoBreakpointStub.removeSpecifyDecoration).not.toHaveBeenCalled();
312+
});
313+
314+
it("calls setLineHighlight when an entry's hit flag flips on", () => {
315+
debugState.set("7", { breakpointId: 7, condition: "", hit: false } as BreakpointInfo);
316+
317+
debugState.set("7", { breakpointId: 7, condition: "", hit: true } as BreakpointInfo);
318+
319+
expect(monacoBreakpointStub.setLineHighlight).toHaveBeenCalledWith(7);
320+
});
321+
322+
it("calls removeHighlight when an entry's hit flag flips off", () => {
323+
debugState.set("8", { breakpointId: 8, condition: "", hit: true } as BreakpointInfo);
324+
325+
debugState.set("8", { breakpointId: 8, condition: "", hit: false } as BreakpointInfo);
326+
327+
expect(monacoBreakpointStub.removeHighlight).toHaveBeenCalled();
328+
});
329+
330+
it("re-creates the decoration when the condition string changes on an existing entry", () => {
331+
debugState.set("9", { breakpointId: 9, condition: "", hit: false } as BreakpointInfo);
332+
monacoBreakpointStub.lineNumberAndDecorationIdMap.set(9, "dec-9");
333+
monacoBreakpointStub.createSpecifyDecoration.mockClear();
334+
monacoBreakpointStub.removeSpecifyDecoration.mockClear();
335+
336+
debugState.set("9", { breakpointId: 9, condition: "x > 0", hit: false } as BreakpointInfo);
337+
338+
expect(monacoBreakpointStub.removeSpecifyDecoration).toHaveBeenCalledWith("dec-9", 9);
339+
expect(monacoBreakpointStub.createSpecifyDecoration).toHaveBeenCalledWith({
340+
startLineNumber: 9,
341+
startColumn: 1,
342+
endLineNumber: 9,
343+
endColumn: 1,
344+
});
345+
});
346+
});
347+
348+
describe("rerenderExistingBreakpoints", () => {
349+
let monacoBreakpointStub: {
350+
createSpecifyDecoration: ReturnType<typeof vi.fn>;
351+
};
352+
353+
beforeEach(() => {
354+
monacoBreakpointStub = { createSpecifyDecoration: vi.fn() };
355+
component.monacoBreakpoint = monacoBreakpointStub as unknown as MonacoBreakpoint;
356+
});
357+
358+
it("re-creates a decoration for every existing entry that has a breakpointId", () => {
359+
debugState.set("10", { breakpointId: 10, condition: "", hit: false } as BreakpointInfo);
360+
debugState.set("11", { breakpointId: undefined, condition: "", hit: false } as BreakpointInfo);
361+
debugState.set("12", { breakpointId: 12, condition: "y > 0", hit: false } as BreakpointInfo);
362+
monacoBreakpointStub.createSpecifyDecoration.mockClear();
363+
364+
component.rerenderExistingBreakpoints();
365+
366+
// Only entries 10 and 12 carry a breakpointId; 11 is filtered out by the
367+
// early-return inside rerenderExistingBreakpoints.
368+
const lines = monacoBreakpointStub.createSpecifyDecoration.mock.calls.map(c => c[0].startLineNumber);
369+
expect(lines.sort()).toEqual([10, 12]);
370+
});
371+
});
372+
373+
describe("removeMonacoBreakpointMethods", () => {
374+
it("disposes the mouseDownDisposable and the breakpoint instance when defined", () => {
375+
const disposeMouseDown = vi.fn();
376+
const disposeBreakpoint = vi.fn();
377+
component.monacoBreakpoint = {
378+
mouseDownDisposable: { dispose: disposeMouseDown },
379+
dispose: disposeBreakpoint,
380+
} as unknown as MonacoBreakpoint;
381+
382+
component.removeMonacoBreakpointMethods();
383+
384+
expect(disposeMouseDown).toHaveBeenCalledOnce();
385+
expect(disposeBreakpoint).toHaveBeenCalledOnce();
386+
});
387+
388+
it("returns early without touching anything when monacoBreakpoint is undefined", () => {
389+
component.monacoBreakpoint = undefined;
390+
// Should simply no-op.
391+
expect(() => component.removeMonacoBreakpointMethods()).not.toThrow();
392+
});
393+
});
241394
});

‎frontend/src/app/workspace/component/code-editor-dialog/code-editor.component.spec.ts‎

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ import { mockOperatorMetaData } from "../../service/operator-metadata/mock-opera
2828
import { commonTestProviders } from "../../../common/testing/test-utils";
2929
import { OperatorPredicate } from "../../types/workflow-common.interface";
3030
import { OperatorSchema } from "../../types/operator-schema.interface";
31-
import { of } from "rxjs";
31+
import { of, Subject } from "rxjs";
32+
import { AIAssistantService } from "../../service/ai-assistant/ai-assistant.service";
33+
import * as monaco from "monaco-editor";
3234

3335
// Operator types that the constructor's language-detection branch must map
3436
// to a specific language. `RUDFSource` / `RUDF` -> `r`; the three V2 Python
@@ -184,4 +186,141 @@ describe("CodeEditorComponent", () => {
184186
} as any);
185187
expect(result).toBeTruthy();
186188
});
189+
190+
describe("getFileSuffixByLanguage", () => {
191+
// The method is private but determines the in-memory file URI that Monaco
192+
// picks language syntax + LSP wiring from, so pinning every branch protects
193+
// the language → file-suffix contract.
194+
function suffixFor(lang: string): string {
195+
const fixture = makeFixture(mockJavaUDFPredicate);
196+
return (fixture.componentInstance as any).getFileSuffixByLanguage(lang);
197+
}
198+
199+
it("maps python → .py", () => expect(suffixFor("python")).toBe(".py"));
200+
it("maps r → .r", () => expect(suffixFor("r")).toBe(".r"));
201+
it("maps javascript → .js", () => expect(suffixFor("javascript")).toBe(".js"));
202+
it("maps java → .java", () => expect(suffixFor("java")).toBe(".java"));
203+
it("is case-insensitive on the language name", () => {
204+
// `suffixFor` builds a fixture per call which adds the predicate's
205+
// operator to the workflow; with the same predicate twice in one test
206+
// the second `addOperator` collides. Call once and reach the method
207+
// directly to assert another case-folded input.
208+
const fixture = makeFixture(mockJavaUDFPredicate);
209+
const fn = (fixture.componentInstance as any).getFileSuffixByLanguage.bind(fixture.componentInstance);
210+
expect(fn("Python")).toBe(".py");
211+
expect(fn("JAVA")).toBe(".java");
212+
});
213+
it("falls back to .py for unknown languages so the default Monaco grammar is python", () => {
214+
expect(suffixFor("brainfuck")).toBe(".py");
215+
});
216+
});
217+
218+
describe("onFocus", () => {
219+
it("highlights the operator the editor is bound to", () => {
220+
const fixture = makeFixture(mockJavaUDFPredicate);
221+
const highlightSpy = vi.spyOn(workflowActionService.getJointGraphWrapper(), "highlightOperators");
222+
fixture.componentInstance.onFocus();
223+
expect(highlightSpy).toHaveBeenCalledWith(mockJavaUDFPredicate.operatorID);
224+
});
225+
});
226+
227+
describe("rejectCurrentAnnotation", () => {
228+
it("hides the suggestion UI and clears the staged code + suggestion", () => {
229+
const fixture = makeFixture(mockJavaUDFPredicate);
230+
const c = fixture.componentInstance;
231+
232+
c.showAnnotationSuggestion = true;
233+
c.currentCode = "x = 1";
234+
c.currentSuggestion = "x: int = 1";
235+
236+
c.rejectCurrentAnnotation();
237+
238+
expect(c.showAnnotationSuggestion).toBe(false);
239+
expect(c.currentCode).toBe("");
240+
expect(c.currentSuggestion).toBe("");
241+
});
242+
243+
it("emits on the multi-variable response subject when one is staged", () => {
244+
const fixture = makeFixture(mockJavaUDFPredicate);
245+
const c = fixture.componentInstance;
246+
const userResponseSubject = new Subject<void>();
247+
const nextSpy = vi.spyOn(userResponseSubject, "next");
248+
249+
// The two flags together gate the multi-variable continuation; both are
250+
// private, so we reach through `(c as any)` to wire them up.
251+
(c as any).isMultipleVariables = true;
252+
(c as any).userResponseSubject = userResponseSubject;
253+
254+
c.rejectCurrentAnnotation();
255+
256+
expect(nextSpy).toHaveBeenCalledOnce();
257+
});
258+
});
259+
260+
describe("acceptCurrentAnnotation", () => {
261+
it("is a no-op when the suggestion UI is not currently shown", () => {
262+
const fixture = makeFixture(mockJavaUDFPredicate);
263+
const c = fixture.componentInstance;
264+
// No state set → early return path; nothing should change.
265+
c.showAnnotationSuggestion = false;
266+
expect(() => c.acceptCurrentAnnotation()).not.toThrow();
267+
expect(c.showAnnotationSuggestion).toBe(false);
268+
});
269+
270+
it("hides the suggestion UI after accepting", () => {
271+
const fixture = makeFixture(mockJavaUDFPredicate);
272+
const c = fixture.componentInstance;
273+
274+
// The accept path reaches into the underlying MonacoEditorLanguageClientWrapper
275+
// for `.getEditor()` and into the YText `.code` for `.insert()`. Both are
276+
// private so we stub them through bracket access to a minimum that lets
277+
// insertTypeAnnotations no-op cleanly. `dispose` is needed because
278+
// ngOnDestroy fires at teardown and calls it.
279+
(c as any).editorWrapper = {
280+
getEditor: () => ({
281+
getModel: () => ({ getOffsetAt: () => 0 }),
282+
}),
283+
dispose: vi.fn(),
284+
};
285+
(c as any).code = { insert: vi.fn() };
286+
287+
c.showAnnotationSuggestion = true;
288+
c.currentRange = new monaco.Range(1, 1, 1, 5);
289+
c.currentSuggestion = ": int";
290+
291+
c.acceptCurrentAnnotation();
292+
293+
expect(c.showAnnotationSuggestion).toBe(false);
294+
});
295+
});
296+
297+
describe("AI assistant action wiring", () => {
298+
// setupAIAssistantActions checks an AI-provider flag (OpenAI vs others)
299+
// before deciding whether to register the per-selection 'Add Type
300+
// Annotation' action. We can't drive the action body without a real
301+
// Monaco editor, but the gate itself is plain RxJS — flip the flag and
302+
// assert observable behaviour.
303+
it("emits 'OpenAI' from the AI assistant gate when configured that way", async () => {
304+
// Re-configure TestBed with a mock that drives the gate; existing tests
305+
// use the default DI-resolved service.
306+
await TestBed.resetTestingModule()
307+
.configureTestingModule({
308+
providers: [
309+
WorkflowActionService,
310+
{ provide: OperatorMetadataService, useClass: AugmentedStubMetadataService },
311+
{ provide: AIAssistantService, useValue: { checkAIAssistantEnabled: () => of("OpenAI") } },
312+
...commonTestProviders,
313+
],
314+
imports: [CodeEditorComponent, HttpClientTestingModule],
315+
})
316+
.compileComponents();
317+
const wfActions = TestBed.inject(WorkflowActionService);
318+
wfActions.addOperator(mockJavaUDFPredicate, mockPoint);
319+
wfActions.getJointGraphWrapper().highlightOperators(mockJavaUDFPredicate.operatorID);
320+
const fixture = TestBed.createComponent(CodeEditorComponent);
321+
fixture.detectChanges();
322+
const checked = await TestBed.inject(AIAssistantService).checkAIAssistantEnabled().toPromise();
323+
expect(checked).toBe("OpenAI");
324+
});
325+
});
187326
});

0 commit comments

Comments
 (0)