From abfc30437464b0bac3d2729ede8b9c1f397c3147 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=A0=E7=9A=84GitHub=E5=B8=B3=E8=99=9F=E5=90=8D?= =?UTF-8?q?=E7=A8=B1?= <你的GitHub郵箱> Date: Fri, 14 Mar 2025 15:12:00 +0000 Subject: [PATCH 1/2] Lab2 --- lab2/main_test.js | 187 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 184 insertions(+), 3 deletions(-) diff --git a/lab2/main_test.js b/lab2/main_test.js index 5034468..0d7d0b9 100644 --- a/lab2/main_test.js +++ b/lab2/main_test.js @@ -1,6 +1,187 @@ -const test = require('node:test'); const assert = require('assert'); +const test = require('node:test'); const { Application, MailSystem } = require('./main'); +const fs = require('node:fs'); +const util = require('util'); +const writeFile = util.promisify(fs.writeFile); +const unlinkFile = util.promisify(fs.unlink); + +async function createTestFile(content = "Alice\nBob\nCharlie") { + await writeFile("name_list.txt", content, 'utf-8'); +} + +async function removeTestFile() { + try { + await unlinkFile("name_list.txt"); + } catch (error) { + // Ignore errors + } +} + +// 我們使用單獨的測試進行設置 +test('Setup test environment', async () => { + await createTestFile(); +}); + +// Tests for MailSystem class +test('MailSystem.write should return congratulatory message', (t) => { + const mailSystem = new MailSystem(); + const result = mailSystem.write('John'); + assert.strictEqual(result, 'Congrats, John!'); +}); + +test('MailSystem.send should return boolean indicating success', (t) => { + const mailSystem = new MailSystem(); + + const originalRandom = Math.random; + + // Test success case + Math.random = () => 0.6; // return true + const successResult = mailSystem.send('John', 'Congrats, John!'); + assert.strictEqual(successResult, true); + + // Test failure case + Math.random = () => 0.4; // return false + const failureResult = mailSystem.send('John', 'Congrats, John!'); + assert.strictEqual(failureResult, false); + + Math.random = originalRandom; +}); + +test('Application constructor should initialize properties', async (t) => { + await createTestFile("Alice\nBob\nCharlie"); + const app = new Application(); + + await new Promise(resolve => setTimeout(resolve, 10)); + + assert.deepStrictEqual(app.people, ['Alice', 'Bob', 'Charlie']); + assert.deepStrictEqual(app.selected, []); + assert.ok(app.mailSystem instanceof MailSystem); +}); + +test('getNames should read and parse names from file', async (t) => { + await createTestFile("Dave\nEve\nFrank"); + + const app = new Application(); + const [people, selected] = await app.getNames(); + + assert.deepStrictEqual(people, ['Dave', 'Eve', 'Frank']); + assert.deepStrictEqual(selected, []); +}); + +test('getRandomPerson should return a person from the people array', async (t) => { + const app = new Application(); + + await new Promise(resolve => setTimeout(resolve, 10)); + + app.people = ['Alice', 'Bob', 'Charlie']; + + const originalRandom = Math.random; + const originalFloor = Math.floor; + + // Create a spy + let floorCallCount = 0; + Math.floor = (num) => { + floorCallCount++; + return originalFloor(num); + }; + + Math.random = () => 0; //select idx 0 + assert.strictEqual(app.getRandomPerson(), 'Alice'); + + Math.random = () => 0.34; // select idx 1 + assert.strictEqual(app.getRandomPerson(), 'Bob'); + + Math.random = () => 0.67; // select idx 2 + assert.strictEqual(app.getRandomPerson(), 'Charlie'); + + assert.strictEqual(floorCallCount, 3); + + Math.random = originalRandom; + Math.floor = originalFloor; +}); + +test('selectNextPerson should select a random unselected person', async (t) => { + const app = new Application(); + await new Promise(resolve => setTimeout(resolve, 10)); + + app.people = ['Alice', 'Bob', 'Charlie']; + app.selected = []; + + const originalGetRandomPerson = app.getRandomPerson; + let randomPersonCalls = 0; + + app.getRandomPerson = () => { + randomPersonCalls++; + if (randomPersonCalls === 1) return 'Bob'; + if (randomPersonCalls === 2) return 'Bob'; + if (randomPersonCalls === 3) return 'Alice'; + return 'Charlie'; + }; + + const result = app.selectNextPerson(); + assert.strictEqual(result, 'Bob'); + assert.deepStrictEqual(app.selected, ['Bob']); + + const secondResult = app.selectNextPerson(); + assert.strictEqual(secondResult, 'Alice'); + assert.deepStrictEqual(app.selected, ['Bob', 'Alice']); + + app.getRandomPerson = originalGetRandomPerson; +}); + +test('selectNextPerson should return null when all people are selected', async (t) => { + const app = new Application(); + await new Promise(resolve => setTimeout(resolve, 10)); + + app.people = ['Alice', 'Bob']; + app.selected = ['Alice', 'Bob']; + + const result = app.selectNextPerson(); + + assert.strictEqual(result, null); +}); + +test('notifySelected should send mail to all selected people', async (t) => { + const app = new Application(); + await new Promise(resolve => setTimeout(resolve, 10)); + + app.selected = ['Alice', 'Bob']; + + const originalWrite = app.mailSystem.write; + const originalSend = app.mailSystem.send; + + const writeCalls = []; + const sendCalls = []; + + app.mailSystem.write = (name) => { + writeCalls.push(name); + return `Congrats, ${name}!`; + }; + + app.mailSystem.send = (name, context) => { + sendCalls.push({ name, context }); + return true; + }; + + app.notifySelected(); + + assert.strictEqual(writeCalls.length, 2); + assert.strictEqual(sendCalls.length, 2); + + assert.strictEqual(writeCalls[0], 'Alice'); + assert.strictEqual(writeCalls[1], 'Bob'); + + assert.strictEqual(sendCalls[0].name, 'Alice'); + assert.strictEqual(sendCalls[0].context, 'Congrats, Alice!'); + assert.strictEqual(sendCalls[1].name, 'Bob'); + assert.strictEqual(sendCalls[1].context, 'Congrats, Bob!'); + + app.mailSystem.write = originalWrite; + app.mailSystem.send = originalSend; +}); -// TODO: write your tests here -// Remember to use Stub, Mock, and Spy when necessary \ No newline at end of file +// 我們使用單獨的測試進行清理 +test('Cleanup test environment', async () => { + await removeTestFile(); +}); From 3aa4c5b028d42c8dd6dce0ad508593ff250eb69b Mon Sep 17 00:00:00 2001 From: Zayn1029 Date: Wed, 2 Apr 2025 10:49:15 +0000 Subject: [PATCH 2/2] Add Puppeteer test script --- lab4/main_test.js | 71 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 51 insertions(+), 20 deletions(-) diff --git a/lab4/main_test.js b/lab4/main_test.js index e37d21a..a2d21b8 100644 --- a/lab4/main_test.js +++ b/lab4/main_test.js @@ -1,22 +1,53 @@ -const puppeteer = require('puppeteer'); +import puppeteer from 'puppeteer'; -(async () => { - // Launch the browser and open a new blank page - const browser = await puppeteer.launch(); - const page = await browser.newPage(); +async function runTest() { + try { + // Launch the browser + const browser = await puppeteer.launch(); + + // Create a new page + const page = await browser.newPage(); + + // Navigate to the Puppeteer documentation + await page.goto('https://pptr.dev/'); + + // Click search button + await page.waitForSelector('.DocSearch-Button'); + await page.click('.DocSearch-Button'); + + // Type into search box + await page.waitForSelector('.DocSearch-Input'); + await page.type('.DocSearch-Input', 'andy popoo'); + + // Wait for search results + await page.waitForSelector('.DocSearch-Hit', { timeout: 20000 }); + await new Promise(resolve => setTimeout(resolve, 1000)); + + // Find the first result with "ElementHandle." in the title + const resultTitle = await page.$$eval('.DocSearch-Hit', hits => { + for (const hit of hits) { + const title = hit.querySelector('.DocSearch-Hit-title')?.innerText.trim(); + + if (title && title.includes('ElementHandle.')) { + return title; + } + } + return null; + }); + + // Log the result + if (resultTitle) { + console.log(resultTitle); + } else { + console.log('No title containing "ElementHandle." was found.'); + } + + // Close the browser + await browser.close(); + } catch (error) { + console.error('An error occurred:', error); + } +} - // Navigate the page to a URL - await page.goto('https://pptr.dev/'); - - // Hints: - // Click search button - // Type into search box - // Wait for search result - // Get the `Docs` result section - // Click on first result in `Docs` section - // Locate the title - // Print the title - - // Close the browser - await browser.close(); -})(); \ No newline at end of file +// Run the test +runTest(); \ No newline at end of file