|
| 1 | +/** |
| 2 | + * E2E tests for ULabel API behavior contracts. |
| 3 | + * |
| 4 | + * Verifies that null-dependent control flow in public methods like |
| 5 | + * suggest_edits, fly_to_annotation_id, and show_global_edit_suggestion |
| 6 | + * behaves correctly. |
| 7 | + */ |
| 8 | +import { test, expect } from "./fixtures"; |
| 9 | +import { draw_bbox, draw_point } from "../testing-utils/drawing_utils"; |
| 10 | +import { wait_for_ulabel_init } from "../testing-utils/init_utils"; |
| 11 | + |
| 12 | +test.describe("ULabel API Behavior", () => { |
| 13 | + test("suggest_edits with no arguments should not throw", async ({ page }) => { |
| 14 | + await wait_for_ulabel_init(page); |
| 15 | + |
| 16 | + // Create an annotation so there's something to suggest edits for |
| 17 | + await draw_bbox(page, [100, 100], [200, 200]); |
| 18 | + |
| 19 | + // suggest_edits() with no arguments should use null defaults internally |
| 20 | + // and fall through to last_move. Should not throw. |
| 21 | + const result = await page.evaluate(() => { |
| 22 | + try { |
| 23 | + window.ulabel.suggest_edits(); |
| 24 | + return { success: true }; |
| 25 | + } catch (e) { |
| 26 | + return { success: false, error: e.message }; |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + expect(result.success).toBe(true); |
| 31 | + }); |
| 32 | + |
| 33 | + test("suggest_edits with null nonspatial_id should not create bad candidate", async ({ page }) => { |
| 34 | + await wait_for_ulabel_init(page); |
| 35 | + |
| 36 | + await draw_bbox(page, [100, 100], [200, 200]); |
| 37 | + |
| 38 | + // Passing null for nonspatial_id should NOT create a best_candidate |
| 39 | + // (the check is: if (nonspatial_id !== null)) |
| 40 | + const result = await page.evaluate(() => { |
| 41 | + try { |
| 42 | + window.ulabel.suggest_edits(null, null, true); |
| 43 | + return { success: true }; |
| 44 | + } catch (e) { |
| 45 | + return { success: false, error: e.message }; |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + expect(result.success).toBe(true); |
| 50 | + }); |
| 51 | + |
| 52 | + test("fly_to_annotation_id with null subtask_key should not switch subtasks", async ({ page }) => { |
| 53 | + await wait_for_ulabel_init(page); |
| 54 | + |
| 55 | + // Create an annotation to fly to |
| 56 | + await draw_point(page, [150, 150]); |
| 57 | + |
| 58 | + const result = await page.evaluate(() => { |
| 59 | + const annotations = window.ulabel.get_current_subtask().annotations; |
| 60 | + const annotation_id = annotations.ordering[0]; |
| 61 | + const initial_subtask = window.ulabel.state.current_subtask; |
| 62 | + |
| 63 | + // null subtask_key should NOT trigger set_subtask |
| 64 | + window.ulabel.fly_to_annotation_id(annotation_id, null); |
| 65 | + |
| 66 | + return { |
| 67 | + subtask_unchanged: window.ulabel.state.current_subtask === initial_subtask, |
| 68 | + }; |
| 69 | + }); |
| 70 | + |
| 71 | + expect(result.subtask_unchanged).toBe(true); |
| 72 | + }); |
| 73 | + |
| 74 | + test("show_global_edit_suggestion with null nonspatial_id uses spatial path", async ({ page }) => { |
| 75 | + await wait_for_ulabel_init(page); |
| 76 | + |
| 77 | + // Create a spatial annotation |
| 78 | + await draw_bbox(page, [100, 100], [200, 200]); |
| 79 | + |
| 80 | + // With null nonspatial_id, should use the spatial path (containing_box) |
| 81 | + // not the nonspatial path (reclf__ DOM element) |
| 82 | + const result = await page.evaluate(() => { |
| 83 | + const annotations = window.ulabel.get_current_subtask().annotations; |
| 84 | + const annotation_id = annotations.ordering[0]; |
| 85 | + |
| 86 | + try { |
| 87 | + window.ulabel.show_global_edit_suggestion(annotation_id, null, null); |
| 88 | + return { success: true }; |
| 89 | + } catch (e) { |
| 90 | + return { success: false, error: e.message }; |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + expect(result.success).toBe(true); |
| 95 | + }); |
| 96 | + |
| 97 | + test("submit payload preserves task_meta value from config", async ({ page }) => { |
| 98 | + await wait_for_ulabel_init(page); |
| 99 | + |
| 100 | + // Check that task_meta in config is preserved as-is |
| 101 | + const task_meta = await page.evaluate(() => { |
| 102 | + return window.ulabel.config.task_meta; |
| 103 | + }); |
| 104 | + |
| 105 | + // task_meta should be whatever the demo page configured (likely {} or an object) |
| 106 | + // The key assertion: it should NOT be unexpectedly converted |
| 107 | + expect(task_meta).not.toBeUndefined(); |
| 108 | + }); |
| 109 | + |
| 110 | + test("class keybinds should be null when not configured", async ({ page }) => { |
| 111 | + await wait_for_ulabel_init(page); |
| 112 | + |
| 113 | + const keybind_values = await page.evaluate(() => { |
| 114 | + const subtask = window.ulabel.get_current_subtask(); |
| 115 | + return subtask.class_defs.map((cd) => ({ |
| 116 | + id: cd.id, |
| 117 | + keybind: cd.keybind, |
| 118 | + keybind_is_null: cd.keybind === null, |
| 119 | + keybind_is_undefined: cd.keybind === undefined, |
| 120 | + })); |
| 121 | + }); |
| 122 | + |
| 123 | + // Classes without configured keybinds should have null (not undefined or "") |
| 124 | + for (const entry of keybind_values) { |
| 125 | + if (entry.keybind === null) { |
| 126 | + expect(entry.keybind_is_null).toBe(true); |
| 127 | + expect(entry.keybind_is_undefined).toBe(false); |
| 128 | + } |
| 129 | + } |
| 130 | + }); |
| 131 | +}); |
0 commit comments