From 1c6237ae460827444995a9cb0bb2ffa938850421 Mon Sep 17 00:00:00 2001 From: Zhou xiao Date: Mon, 2 Sep 2024 14:09:56 +0800 Subject: [PATCH] chore: delete unless debug code and fix ci cache logic (#84) --- .github/workflows/ci.yml | 17 +-- biome.json | 3 +- packages/midscene/modern.config.ts | 2 +- packages/web-integration/modern.config.ts | 1 - .../web-integration/src/debug/img/index.ts | 143 ------------------ .../web-integration/src/debug/img/util.ts | 33 ---- .../ai-report-1-report.txt | 2 +- 7 files changed, 10 insertions(+), 191 deletions(-) delete mode 100644 packages/web-integration/src/debug/img/index.ts delete mode 100644 packages/web-integration/src/debug/img/util.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a573a68c..a53788bb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -35,19 +35,14 @@ jobs: with: node-version: '18' - - name: Get pnpm store directory - id: pnpm-cache - shell: bash - run: | - echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT - - - uses: actions/cache@v3 - name: Setup pnpm cache + - name: Cache Puppeteer dependencies + uses: actions/cache@v3 + id: cache-puppeteer with: - path: ${{ steps.pnpm-cache.outputs.STORE_PATH }} - key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} + path: ~/.cache/puppeteer + key: ${{ runner.os }}-puppeteer-${{ hashFiles('pnpm-lock.yaml') }} restore-keys: | - ${{ runner.os }}-pnpm-store- + ${{ runner.os }}-puppeteer- - name: Install dependencies run: pnpm install --frozen-lockfile diff --git a/biome.json b/biome.json index 0c961773..e07a0de6 100644 --- a/biome.json +++ b/biome.json @@ -12,7 +12,8 @@ "**/doc_build", "*-dump.json", "script_get_all_texts.tmp.js", - "**/playwright-report/**" + "**/playwright-report/**", + "**/todo-report.spec.ts-snapshots/**" ] }, "javascript": { diff --git a/packages/midscene/modern.config.ts b/packages/midscene/modern.config.ts index 93e50998..c47fc085 100644 --- a/packages/midscene/modern.config.ts +++ b/packages/midscene/modern.config.ts @@ -12,7 +12,7 @@ export default defineConfig({ 'ai-model': 'src/ai-model/index.ts', }, // input: ['src/utils.ts', 'src/index.ts', 'src/image/index.ts'], - externals: ['node:buffer', 'sharp'], + externals: ['node:buffer'], target: 'es2017', }, }); diff --git a/packages/web-integration/modern.config.ts b/packages/web-integration/modern.config.ts index d387da98..cbfe4b3e 100644 --- a/packages/web-integration/modern.config.ts +++ b/packages/web-integration/modern.config.ts @@ -13,6 +13,5 @@ export default defineConfig({ 'playwright-report': './src/playwright/reporter/index.ts', }, target: 'es2017', - externals: ['sharp'], }, }); diff --git a/packages/web-integration/src/debug/img/index.ts b/packages/web-integration/src/debug/img/index.ts deleted file mode 100644 index 9661145e..00000000 --- a/packages/web-integration/src/debug/img/index.ts +++ /dev/null @@ -1,143 +0,0 @@ -import assert from 'node:assert'; -import { Buffer } from 'node:buffer'; -import type { NodeType } from '@/extractor/constants'; -import sharp from 'sharp'; - -// Define picture path -type ElementType = { - x: number; - y: number; - width: number; - height: number; - label: string; - attributes: { - [key: string]: string; - nodeType: NodeType; - }; -}; - -const createSvgOverlay = ( - elements: Array, - imageWidth: number, - imageHeight: number, -) => { - let svgContent = ``; - - // Define color array - const colors = [ - { rect: 'blue', text: 'white' }, - { rect: 'green', text: 'white' }, - ]; - - // Define clipping path - svgContent += ''; - elements.forEach((element, index) => { - svgContent += ` - - - - `; - }); - svgContent += ''; - - elements.forEach((element, index) => { - // Calculate the width and height of the text - const textWidth = element.label.length * 8; // Assume that each character is 8px wide - const textHeight = 12; // Assume that the text height is 20px - - // Calculates the position of the initial color block so that it wraps and centers the text - const rectWidth = textWidth + 5; - const rectHeight = textHeight + 4; - let rectX = element.x - rectWidth; - let rectY = element.y + element.height / 2 - textHeight / 2 - 2; - - // Initial text position - let textX = rectX + rectWidth / 2; - let textY = rectY + rectHeight / 2 + 6; - - // Check to see if it's obscured by the left - if (rectX < 0) { - rectX = element.x; - rectY = element.y - rectHeight; - textX = rectX + rectWidth / 2; - textY = rectY + rectHeight / 2 + 6; - } - - // Choose color - const color = colors[index % colors.length]; - - // Draw boxes and text - svgContent += ` - - - - ${element.label} - - `; - }); - - svgContent += ''; - return Buffer.from(svgContent); -}; - -export const processImageElementInfo = async (options: { - inputImgBase64: string; - elementsPositionInfo: Array; - elementsPositionInfoWithoutText: Array; -}) => { - // Get the size of the original image - const base64Image = options.inputImgBase64.split(';base64,').pop(); - assert(base64Image, 'base64Image is undefined'); - - const imageBuffer = Buffer.from(base64Image, 'base64'); - const metadata = await sharp(imageBuffer).metadata(); - const { width, height } = metadata; - - if (width && height) { - // Create svg overlay - const svgOverlay = createSvgOverlay( - options.elementsPositionInfo, - width, - height, - ); - const svgOverlayWithoutText = createSvgOverlay( - options.elementsPositionInfoWithoutText, - width, - height, - ); - - // Composite picture - const compositeElementInfoImgBase64 = await sharp(imageBuffer) - // .resize(newDimensions.width, newDimensions.height) - .composite([{ input: svgOverlay, blend: 'over' }]) - .toBuffer() - .then((data) => { - // Convert image data to base64 encoding - return data.toString('base64'); - }) - .catch((err) => { - throw err; - }); - - // Composite picture withoutText - const compositeElementInfoImgWithoutTextBase64 = await sharp(imageBuffer) - // .resize(newDimensions.width, newDimensions.height) - .composite([{ input: svgOverlayWithoutText, blend: 'over' }]) - .toBuffer() - .then((data) => { - // Convert image data to base64 encoding - return data.toString('base64'); - }) - .catch((err) => { - throw err; - }); - - return { - compositeElementInfoImgBase64, - compositeElementInfoImgWithoutTextBase64, - }; - } - throw Error('Image processing failed because width or height is undefined'); -}; diff --git a/packages/web-integration/src/debug/img/util.ts b/packages/web-integration/src/debug/img/util.ts deleted file mode 100644 index 98c98d72..00000000 --- a/packages/web-integration/src/debug/img/util.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { NodeType } from '@/extractor/constants'; -import type { ElementInfo } from '@/extractor/extractor'; -import { getElementInfosFromPage } from '../../common/utils'; - -export async function getElementInfos(page: any) { - const captureElementSnapshot: Array = - await getElementInfosFromPage(page); - const elementsPositionInfo = captureElementSnapshot.map( - (elementInfo, index) => { - return { - label: elementInfo.indexId, - x: elementInfo.rect.left, - y: elementInfo.rect.top, - width: elementInfo.rect.width, - height: elementInfo.rect.height, - attributes: elementInfo.attributes, - }; - }, - ); - const elementsPositionInfoWithoutText = elementsPositionInfo.filter( - (elementInfo) => { - if (elementInfo.attributes.nodeType === NodeType.TEXT) { - return false; - } - return true; - }, - ); - return { - elementsPositionInfo, - captureElementSnapshot, - elementsPositionInfoWithoutText, - }; -} diff --git a/packages/web-integration/tests/ai/report/todo-report.spec.ts-snapshots/ai-report-1-report.txt b/packages/web-integration/tests/ai/report/todo-report.spec.ts-snapshots/ai-report-1-report.txt index 7af19240..b6e74a4f 100644 --- a/packages/web-integration/tests/ai/report/todo-report.spec.ts-snapshots/ai-report-1-report.txt +++ b/packages/web-integration/tests/ai/report/todo-report.spec.ts-snapshots/ai-report-1-report.txt @@ -34,4 +34,4 @@ "Action / KeyboardPress" ] } -] +] \ No newline at end of file