Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr Social Engine & Remote Navigator8 * File : workers/navigator-worker/src/driver.ts9 * Module : workers10 * Repository : https://git.spboucher.ai/maclustr-social11 * Project Website : https://social.spboucher.ai12 * Created : 2026-12-0113 * Last Modified : 2026-12-0114 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Playwright implementation of the workflow-engine Driver: semantic locators only, explicit waits, no sleeps.43import type { Locator as PwLocator, Page } from 'playwright';44import type { Locator } from '@mls/contracts';45import type { Driver } from '@mls/workflow-engine';46import { classifyUrl } from '@mls/policy-engine';4748export function toLocator(page: Page, l: Locator): PwLocator {49 if ('role' in l) { const root = l.within ? page.getByRole(l.within.role, l.within.name ? { name: l.within.name } : {}) : page; return root.getByRole(l.role as any, l.name !== undefined ? { name: l.name, exact: l.exact ?? false } : {}); }50 if ('label' in l) return page.getByLabel(l.label, { exact: l.exact ?? false });51 if ('text' in l) return page.getByText(l.text, { exact: l.exact ?? false });52 if ('placeholder' in l) return page.getByPlaceholder(l.placeholder);53 return page.getByTestId(l.testId);54}55/** Selectors that are always masked in a capture, whatever the recipe says. */56export const ALWAYS_MASK = 'input[type=password], input[type=email], input[type=tel], input[autocomplete*="one-time"], input[autocomplete*="current-password"], input[name*="pass" i], input[name*="code" i], [data-private]';5758export class PlaywrightDriver implements Driver {59 constructor(private page: Page, private uploadEvidence: (png: Buffer) => Promise<{ ref: string; sha256: string }>) {}60 async currentUrl() { return this.page.url(); }61 async navigate(url: string, timeoutMs: number) { await this.page.goto(url, { timeout: timeoutMs, waitUntil: 'domcontentloaded' }); }62 async isVisible(l: Locator, t: number) { try { await toLocator(this.page, l).first().waitFor({ state: 'visible', timeout: t }); return true; } catch { return false; } }63 async isHidden(l: Locator, t: number) { try { await toLocator(this.page, l).first().waitFor({ state: 'hidden', timeout: t }); return true; } catch { return false; } }64 async textVisible(text: string, t: number) { try { await this.page.getByText(text, { exact: false }).first().waitFor({ state: 'visible', timeout: t }); return true; } catch { return false; } }65 async read(l: Locator, what: 'text' | 'value' | 'count' | 'href', t: number) {66 const loc = toLocator(this.page, l);67 if (what === 'count') return String(await loc.count());68 const one = this.strict(loc); await one.waitFor({ state: 'visible', timeout: t });69 if (what === 'value') return (await one.evaluate((el) => ('value' in el ? String((el as HTMLInputElement).value) : (el.textContent ?? ''))));70 if (what === 'href') return (await one.getAttribute('href', { timeout: t })) ?? '';71 return (await one.innerText({ timeout: t })).trim();72 }73 /** A locator matching several elements is an error, never "take the first": ambiguity must fail loudly. */74 private strict(loc: PwLocator) { return loc; }75 async click(l: Locator, t: number) { await toLocator(this.page, l).click({ timeout: t }); }76 async fill(l: Locator, value: string, t: number) { await toLocator(this.page, l).fill(value, { timeout: t }); }77 async select(l: Locator, option: string, t: number) { await toLocator(this.page, l).selectOption({ label: option }, { timeout: t }); }78 async upload(l: Locator, files: string[], t: number) { await toLocator(this.page, l).setInputFiles(files, { timeout: t }); }79 async captureRedacted(_label: string, mask: Locator[], t: number) {80 if (classifyUrl(this.page.url()) !== 'ok') throw new Error('capture_interdite_sur_page_de_connexion_ou_de_securite');81 const png = await this.page.screenshot({ type: 'png', timeout: t, animations: 'disabled', caret: 'hide', mask: [this.page.locator(ALWAYS_MASK), ...mask.map((m) => toLocator(this.page, m))], maskColor: '#111111' });82 return this.uploadEvidence(png);83 }84}85