All files / realm/blong-commander/test explore.play.ts

96.3% Statements 391/406
78.12% Branches 25/32
100% Functions 10/10
96.3% Lines 391/406

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 4071x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x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 4x 4x 1x 1x 3x 4x 3x 3x 3x 4x 1x  
/**
 * Commander explore screenshots — full-stack Playwright tests that log into
 * the commander portal, open the Commander page, and capture the critical
 * "explore moments" for each adapter source: the source list, a branch
 * drill-down, and leaf viewers.
 *
 * These are deliberately screenshot-first (the framework convention); targeted
 * assertions are used sparingly for critical state.
 */
import {expect, test, type Page, type Portal} from '@feasibleone/blong-browser/playwright';
 
test.use({blongPermissions: true});
 
const NO_ROWS = 'No available options';
 
/**
 * Commander rows come straight from live backends whose VALUES can vary per
 * environment (uids, resource versions, keycloak/vault uuids, sizes, kafka
 * offsets, random pod suffixes) and whose ROW SETS can be joined by other
 * suites' `blong-test:*` data. To keep the screenshot baselines deterministic
 * in CI AND locally, we (a) filter the commander table to a stable, seeded
 * subset via its built-in Filter… input, and (b) mask ONLY the specific
 * columns/cells whose values are inherently dynamic (magenta) — never the whole
 * table — so the screenshots still show the UI working (rows render, structure
 * and stable columns are visible).
 */
const MASK_COLOR = '#FF00FF'; // magenta (Playwright's default mask colour)
 
/** Screenshot options that mask the given 1-based body columns of the table. */
function colsMask(page: Page, ...indexes: number[]) {
    return {
        mask: indexes.map(i => page.locator(`.p-datatable-tbody td:nth-child(${i})`)),
        maskColor: MASK_COLOR,
    };
}
 
/** Mask the pretty-printed JSON body of the document viewer (values dynamic). */
function viewerJsonMask(page: Page) {
    return {mask: [page.locator('.blong-viewer-document pre')], maskColor: MASK_COLOR};
}
 
/** Narrow the commander table to rows containing `text` (deterministic subset). */
async function filterRows(page: Page, text: string) {
    const input = page.locator('input[placeholder="Filter…"]').first();
    await input.waitFor({state: 'visible', timeout: 10_000});
    await input.fill(text);
    await page.waitForTimeout(400);
}
 
/** Clear the commander table filter (it persists across drill navigation). */
async function clearFilter(page: Page) {
    const input = page.locator('input[placeholder="Filter…"]').first();
    if (await input.isVisible().catch(() => false)) {
        await input.fill('');
        await page.waitForTimeout(300);
    }
}
 
async function openCommander(portal: Portal) {
    // Expand the Explore group and open the Commander page.
    await portal.menuClick('commander.browse');
    await expect(portal.page.locator('.blong-commander')).toBeVisible({timeout: 15_000});
    // Wait for the source tree to render all configured sources.
    await expect(portal.page.locator('.blong-commander-tree .p-treenode')).toHaveCount(8, {
        timeout: 15_000,
    });
}
 
/**
 * Select a source node in the left tree and wait for its children in the table.
 * `expectedText` asserts a SPECIFIC row is present — proof the drill returned
 * real data (not just "some row", which the stale source list would satisfy).
 */
async function selectSource(page: Page, label: string, expectedText: string) {
    const node = page
        .locator('.blong-commander-tree .p-treenode-content')
        .filter({hasText: label})
        .first();
    await node.click();
    // The source breadcrumb confirms the selection registered.
    await expect(
        page.locator('.blong-commander-path-bar button').filter({hasText: label}).first(),
    ).toBeVisible({timeout: 15_000});
    // Wait for the drill fetch to finish — the DataTable loading overlay (shown
    // while the source's children load) must clear. This avoids racing the drill
    // and capturing the stale source list in the screenshot.
    await expect(page.locator('.p-datatable-loading-overlay')).toBeHidden({timeout: 15_000});
    // A real data row must be present.
    await expect(
        page
            .locator('.p-datatable-tbody tr')
            .filter({hasNotText: NO_ROWS})
            .filter({
                hasNot: page.locator('.blong-commander-up-link'),
            })
            .first(),
    ).toBeVisible({timeout: 15_000});
    // The expected element from the real backend must be present.
    await expect(
        page.locator('.p-datatable-tbody tr').filter({hasText: expectedText}).first(),
    ).toBeVisible({timeout: 15_000});
    // The ".." up-to-parent row is the first table row once a location is selected.
    await expect(page.locator('.p-datatable-tbody tr').filter({hasText: '..'}).first()).toBeVisible(
        {timeout: 15_000},
    );
}
 
/** Data rows = table rows excluding the ".." up-to-parent row. */
const dataRows = (page: Page) =>
    page
        .locator('.p-datatable-tbody tr')
        .filter({hasNot: page.locator('.blong-commander-up-link')});
 
/** Double-click a table row containing `text` (drills a branch or opens a leaf viewer). */
async function openRowByText(page: Page, text: string) {
    const row = page.locator('.p-datatable-tbody tr').filter({hasText: text}).first();
    await row.dblclick();
    await page.waitForTimeout(500);
}
 
/**
 * Drill into the first table row whose children are non-empty. Some backends
 * return empty first rows (e.g. a Kubernetes namespace with no pods), so this
 * walks the rows, drilling each one, and backs up to the source root when a
 * drill yields nothing. Returns the opened row label, or null when no row has
 * children.
 */
async function openFirstRowWithChildren(page: Page, backLabel: string): Promise<string | null> {
    for (let i = 0; i < 50; i++) {
        const nonEmptyRows = dataRows(page).filter({hasNotText: NO_ROWS});
        const label = (await nonEmptyRows.nth(i).locator('td').first().textContent())?.trim();
        if (!label) break;
        await openRowByText(page, label);
        // Wait until the child fetch settles. An empty branch now renders ONLY the
        // ".." row (no "No records found" message), so "no data rows" while not
        // loading is the empty signal.
        await expect
            .poll(
                async () => {
                    const dataRowsCount = await dataRows(page)
                        .filter({hasNotText: NO_ROWS})
                        .count();
                    const emptyShown = await page
                        .locator('.p-datatable-tbody tr')
                        .filter({hasText: NO_ROWS})
                        .count();
                    const loadingVisible = await page
                        .locator('.p-datatable-loading-overlay')
                        .isVisible()
                        .catch(() => false);
                    const upOnly =
                        dataRowsCount === 0 &&
                        (await page.locator('.blong-commander-up-link').count()) > 0;
                    return (dataRowsCount > 0 || emptyShown > 0 || upOnly) && !loadingVisible;
                },
                {timeout: 10_000},
            )
            .toBe(true);
        const childCount = await dataRows(page).filter({hasNotText: NO_ROWS}).count();
        if (childCount > 0) return label;
        // No children — back up to the source root via the breadcrumb (re-clicking
        // the already-selected tree node would not re-fire onSelectionChange) and
        // try the next row once the source children have reloaded.
        await page
            .locator('.blong-commander-path-bar button')
            .filter({hasText: backLabel})
            .first()
            .click();
        await expect(page.locator('.p-datatable-loading-overlay')).toBeHidden({
            timeout: 15_000,
        });
        await page.waitForTimeout(200);
    }
    return null;
}
 
test('source list — the commander home', async ({portal}) => {
    await openCommander(portal);
    await expect(portal.page).toHaveScreenshot('commander-sources.png');
});
 
test('access-db — browse tables (SQL via access.table.list)', async ({portal}) => {
    await openCommander(portal);
    // Table names are shown stripped of the `access_` prefix; `user` is a real
    // blong access table.
    await selectSource(portal.page, 'Access DB', 'user');
    await expect(portal.page).toHaveScreenshot('explore-access-db-tables.png');
});
 
test('k8s-dev — namespace → category → resource drill-down and item viewer', async ({portal}) => {
    await openCommander(portal);
    // `kube-system` always exists. Mask the namespace resourceVersion + uid
    // columns (values differ per cluster); keep Name + Status.Phase visible.
    await selectSource(portal.page, 'Kubernetes', 'kube-system');
    await expect(portal.page).toHaveScreenshot(
        'explore-k8s-namespaces.png',
        colsMask(portal.page, 2, 3),
    );
    // Drill into the namespace → the resource categories (static labels).
    await openRowByText(portal.page, 'kube-system');
    await expect(
        portal.page.locator('.p-datatable-tbody tr').filter({hasText: 'Workloads'}).first(),
    ).toBeVisible({timeout: 15_000});
    await expect(portal.page).toHaveScreenshot('explore-k8s-categories.png');
    // Workloads → resource types.
    await openRowByText(portal.page, 'Workloads');
    await expect(
        portal.page.locator('.p-datatable-tbody tr').filter({hasText: 'Pods'}).first(),
    ).toBeVisible({timeout: 15_000});
    // Pods → the actual pods in the namespace (names/versions/uids are random
    // per cluster → mask those columns; Namespace/DnsPolicy stay visible).
    await openRowByText(portal.page, 'Pods');
    await expect(dataRows(portal.page).first()).toBeVisible({timeout: 15_000});
    await expect(portal.page).toHaveScreenshot(
        'explore-k8s-pods.png',
        colsMask(portal.page, 1, 2, 4, 5, 8),
    );
    // Drill into a pod → document viewer with the pod's fields (JSON values are
    // environment-specific → mask the pretty-printed body, keep field count).
    const firstPod = await dataRows(portal.page).first().locator('td').first().textContent();
    if (firstPod) {
        await openRowByText(portal.page, firstPod.trim());
        await expect(portal.page.locator('.blong-viewer-document')).toBeVisible({
            timeout: 15_000,
        });
        await expect(portal.page).toHaveScreenshot(
            'explore-k8s-pod.png',
            viewerJsonMask(portal.page),
        );
    }
});
 
test('vault-dev — mounts, secrets, and masked secret viewer', async ({portal}) => {
    await openCommander(portal);
    // `secret/` is a mounted KV secret engine that always exists (mask the
    // per-instance mount accessor column; keep the mount path + description).
    await selectSource(portal.page, 'Vault', 'secret/');
    await expect(portal.page).toHaveScreenshot(
        'explore-vault-mounts.png',
        colsMask(portal.page, 2),
    );
    // Drill into the mount → filter to the seeded secret so other suites'
    // `blong-test` secrets never affect the screenshot.
    await openRowByText(portal.page, 'secret/');
    await expect(
        portal.page.locator('.p-datatable-tbody tr').filter({hasText: 'commander-demo'}).first(),
    ).toBeVisible({timeout: 15_000});
    await filterRows(portal.page, 'commander-demo');
    await expect(portal.page).toHaveScreenshot('explore-vault-secrets.png');
    await clearFilter(portal.page);
    // Open the seeded secret → masked secret viewer (no error; values are masked
    // by the viewer, keys are the deterministic seed → no screenshot mask).
    await openRowByText(portal.page, 'commander-demo');
    await expect(portal.page.locator('.blong-viewer-secret')).toBeVisible({timeout: 15_000});
    await expect(portal.page).toHaveScreenshot('explore-vault-secret.png');
});
 
test('mongo-dev — databases and collections', async ({portal}) => {
    await openCommander(portal);
    // `admin` always exists in MongoDB. Filter to it (so a parallel suite's
    // `blong-integration` db never shifts the rows) and mask the dynamic
    // `Size On Disk` column — Name + Empty stay visible.
    await selectSource(portal.page, 'MongoDB', 'admin');
    await filterRows(portal.page, 'admin');
    await expect(portal.page).toHaveScreenshot(
        'explore-mongo-databases.png',
        colsMask(portal.page, 2),
    );
    await clearFilter(portal.page);
    // Drill the filtered `admin` database → its collections are deterministic
    // (Name | Type | Database) so no mask is needed.
    const db = await openFirstRowWithChildren(portal.page, 'MongoDB (dev)');
    if (db) {
        await expect(dataRows(portal.page).first()).toBeVisible({timeout: 15_000});
        await expect(portal.page).toHaveScreenshot('explore-mongo-collections.png');
    }
});
 
test('redis-dev — database index and keys', async ({portal}) => {
    await openCommander(portal);
    // The databases table shows the single used index `0` (deterministic).
    await selectSource(portal.page, 'Redis', '0');
    await expect(portal.page).toHaveScreenshot('explore-redis-databases.png');
    // Drill into db 0 → the seeded `commander:demo` key must be present.
    const db = await openFirstRowWithChildren(portal.page, 'Redis (dev)');
    if (db) {
        await expect(
            portal.page
                .locator('.p-datatable-tbody tr')
                .filter({hasText: 'commander:demo'})
                .first(),
        ).toBeVisible({timeout: 15_000});
        // Filter to the seeded commander:* keys so other suites' `blong-test:*`
        // keys never appear — the two seeded keys are deterministic.
        await filterRows(portal.page, 'commander');
        await expect(portal.page).toHaveScreenshot('explore-redis-keys.png');
    }
});
 
test('kafka-dev — topics and message viewer', async ({portal}) => {
    await openCommander(portal);
    // `blong-integration` is the only non-internal topic in the dev broker.
    await selectSource(portal.page, 'Kafka', 'blong-integration');
    await expect(portal.page).toHaveScreenshot('explore-kafka-topics.png');
    const topic = await openFirstRowWithChildren(portal.page, 'Kafka (dev)');
    if (topic) {
        // At least one message row must be present.
        await expect(dataRows(portal.page).first()).toBeVisible({timeout: 15_000});
        // Filter to the seeded message (unique content) and mask the dynamic
        // `offset` (Name) column — topic/partition/value stay visible.
        await filterRows(portal.page, 'hello from blong-integration');
        await expect(portal.page).toHaveScreenshot(
            'explore-kafka-messages.png',
            colsMask(portal.page, 1),
        );
    }
});
 
test('s3-dev — buckets and objects', async ({portal}) => {
    await openCommander(portal);
    // `blong-integration` is the seeded bucket (the bucket list shows only its
    // name → deterministic).
    await selectSource(portal.page, 'S3', 'blong-integration');
    await expect(portal.page).toHaveScreenshot('explore-s3-buckets.png');
    const bucket = await openFirstRowWithChildren(portal.page, 'S3 (dev)');
    if (bucket) {
        // The seeded object must be listed.
        await expect(
            portal.page
                .locator('.p-datatable-tbody tr')
                .filter({hasText: 'commander/hello.txt'})
                .first(),
        ).toBeVisible({timeout: 15_000});
        // Filter to the seeded object (fixed content → deterministic ETag/size),
        // hiding other suites' `blong-test` objects.
        await filterRows(portal.page, 'commander/');
        await expect(portal.page).toHaveScreenshot('explore-s3-objects.png');
    }
});
 
test('keycloak-dev — realms and users', async ({portal}) => {
    await openCommander(portal);
    // Filter realms to the built-in `master` (deterministic drill target) and
    // mask the per-instance realm Id (uuid) column.
    await selectSource(portal.page, 'Keycloak', 'master');
    await filterRows(portal.page, 'master');
    await expect(portal.page).toHaveScreenshot(
        'explore-keycloak-realms.png',
        colsMask(portal.page, 2),
    );
    await clearFilter(portal.page);
    const realm = await openFirstRowWithChildren(portal.page, 'Keycloak (dev)');
    if (realm) {
        await expect(dataRows(portal.page).first()).toBeVisible({timeout: 15_000});
        // Users: mask the uuid Id + created-timestamp columns (name/state stay).
        await expect(portal.page).toHaveScreenshot(
            'explore-keycloak-users.png',
            colsMask(portal.page, 2, 3),
        );
    }
});
 
test('navigator mirrors the drill path; ".." and Backspace go up', async ({portal}) => {
    await openCommander(portal);
    await selectSource(portal.page, 'Kubernetes', 'kube-system');
    const upLink = portal.page.locator('.blong-commander-up-link');
    await expect(upLink).toBeVisible();
 
    // Drill the namespace → category → resource → pods: the tree reveals the
    // full path (more than the 8 source roots are shown).
    await openRowByText(portal.page, 'kube-system');
    await openRowByText(portal.page, 'Workloads');
    await openRowByText(portal.page, 'Pods');
    await expect(
        portal.page.locator('.blong-commander-tree .p-treenode').count(),
    ).resolves.toBeGreaterThan(8);
    await expect(
        portal.page
            .locator('.blong-commander-tree .p-treenode-content')
            .filter({hasText: 'Pods'})
            .first(),
    ).toBeVisible({timeout: 15_000});
 
    // Backspace goes up one level → back to the resource types.
    await portal.page.keyboard.press('Backspace');
    await expect(
        portal.page.locator('.p-datatable-tbody tr').filter({hasText: 'Deployments'}).first(),
    ).toBeVisible({timeout: 15_000});
 
    // ".." navigates up one level at a time; climb back to the home welcome panel.
    for (let i = 0; i < 6; i++) {
        if (
            await portal.page
                .locator('.blong-commander-home')
                .isVisible()
                .catch(() => false)
        ) {
            break;
        }
        const up = portal.page.locator('.blong-commander-up-link').first();
        if (!(await up.isVisible().catch(() => false))) break;
        await up.click();
        await portal.page.waitForTimeout(300);
    }
    await expect(portal.page.locator('.blong-commander-home')).toBeVisible({timeout: 15_000});
});