|
1 | 1 | import { beforeEach, describe, expect, it, vi } from 'vitest' |
2 | 2 | import { EnhancerRegistry } from '../../../src/lib/registries' |
| 3 | +import { PAGES } from '../../har-index' |
| 4 | +import fs from 'node:fs/promises' |
| 5 | +import path from 'node:path' |
| 6 | +import { fileURLToPath } from 'node:url' |
3 | 7 |
|
4 | 8 | // Mock WXT's defineContentScript global |
5 | 9 | vi.stubGlobal('defineContentScript', vi.fn()) |
6 | 10 |
|
7 | | -describe('GitHubHandler', () => { |
8 | | - let enhancers: EnhancerRegistry |
9 | | - let mockTextarea: HTMLTextAreaElement |
| 11 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
10 | 12 |
|
11 | | - beforeEach(() => { |
12 | | - // Reset DOM and registries for each test |
13 | | - document.body.innerHTML = '' |
14 | | - enhancers = new EnhancerRegistry() |
15 | | - |
16 | | - // Mock window.location for GitHub PR page |
17 | | - Object.defineProperty(window, 'location', { |
18 | | - value: { |
19 | | - hostname: 'github.com', |
20 | | - href: 'https://github.com/diffplug/selfie/pull/517', |
21 | | - pathname: '/diffplug/selfie/pull/517', |
22 | | - }, |
23 | | - writable: true, |
24 | | - }) |
| 13 | +// Helper function to load and extract HTML from HAR files |
| 14 | +async function loadHarHtml(key: string): Promise<string> { |
| 15 | + const harPath = path.join(__dirname, '../../har', `${key}.har`) |
| 16 | + const harContent = await fs.readFile(harPath, 'utf-8') |
| 17 | + const harData = JSON.parse(harContent) |
25 | 18 |
|
26 | | - // Create a mock textarea element that mimics GitHub's PR comment box |
27 | | - mockTextarea = document.createElement('textarea') |
28 | | - mockTextarea.name = 'comment[body]' |
29 | | - mockTextarea.placeholder = 'Leave a comment' |
30 | | - mockTextarea.className = 'form-control markdown-body' |
| 19 | + // Find the main HTML response (same logic as har-view.ts) |
| 20 | + const mainEntry = harData.log.entries.find((entry: any) => |
| 21 | + entry.request.url.includes('github.com') && |
| 22 | + entry.response.content.mimeType?.includes('text/html') && |
| 23 | + entry.response.content.text |
| 24 | + ) |
31 | 25 |
|
32 | | - // Add it to a typical GitHub comment form structure |
33 | | - const commentForm = document.createElement('div') |
34 | | - commentForm.className = 'js-new-comment-form' |
35 | | - commentForm.appendChild(mockTextarea) |
36 | | - document.body.appendChild(commentForm) |
37 | | - }) |
| 26 | + if (!mainEntry) { |
| 27 | + throw new Error(`No HTML content found in HAR file: ${key}.har`) |
| 28 | + } |
38 | 29 |
|
39 | | - it('should identify GitHub PR textarea and register it in TextareaRegistry', () => { |
40 | | - // Simulate the content script's enhanceMaybe function |
41 | | - // const enhancedTextarea = enhancers.tryToEnhance(mockTextarea) |
42 | | - // expect(enhancedTextarea).toBeTruthy() |
43 | | - // expect(enhancedTextarea?.textarea).toBe(mockTextarea) |
44 | | - // expect(enhancedTextarea?.spot.type).toBe('GH_PR_ADD_COMMENT') |
45 | | - // // Register the enhanced textarea |
46 | | - // if (enhancedTextarea) { |
47 | | - // enhancedTextareas.register(enhancedTextarea) |
48 | | - // } |
49 | | - // // Verify it's in the registry |
50 | | - // const registeredTextarea = enhancedTextareas.get(mockTextarea) |
51 | | - // expect(registeredTextarea).toBeTruthy() |
52 | | - // expect(registeredTextarea?.textarea).toBe(mockTextarea) |
53 | | - }) |
| 30 | + return mainEntry.response.content.text |
| 31 | +} |
54 | 32 |
|
55 | | - it('should create correct GitHubContext spot for PR comment', () => { |
56 | | - // const _enhancedTextarea = enhancers.tryToEnhance(mockTextarea) |
57 | | - // expect(enhancedTextarea).toBeTruthy() |
58 | | - // Snapshot test on the spot value |
59 | | - // expect(enhancedTextarea?.spot).toMatchSnapshot('github-pr-517-spot') |
60 | | - // Also verify specific expected values |
61 | | - // expect(enhancedTextarea?.spot).toMatchObject({ |
62 | | - // domain: 'github.com', |
63 | | - // number: 517, |
64 | | - // slug: 'diffplug/selfie', |
65 | | - // type: 'GH_PR_ADD_COMMENT', |
66 | | - // unique_key: 'github:diffplug/selfie:pull:517', |
67 | | - // }) |
| 33 | +describe('github', () => { |
| 34 | + beforeEach(() => { |
| 35 | + // Reset DOM between tests |
| 36 | + document.body.innerHTML = '' |
| 37 | + |
| 38 | + // Mock console methods to avoid noise |
| 39 | + vi.spyOn(console, 'warn').mockImplementation(() => {}) |
68 | 40 | }) |
69 | 41 |
|
70 | | - it('should not enhance textarea on non-GitHub pages', () => { |
71 | | - // Change location to non-GitHub site |
72 | | - Object.defineProperty(window, 'location', { |
73 | | - value: { |
74 | | - hostname: 'example.com', |
75 | | - href: 'https://example.com/some/page', |
76 | | - pathname: '/some/page', |
77 | | - }, |
78 | | - writable: true, |
| 42 | + // Helper to setup DOM environment with location mocking |
| 43 | + function setupDOMEnvironment(url: string, html: string) { |
| 44 | + // Set up the HTML content |
| 45 | + document.body.innerHTML = html |
| 46 | + |
| 47 | + // Mock window.location.pathname for GitHub enhancer |
| 48 | + Object.defineProperty(window.location, 'pathname', { |
| 49 | + value: new URL(url).pathname, |
| 50 | + configurable: true |
79 | 51 | }) |
| 52 | + |
| 53 | + // Add GitHub hostname meta tag (for the enhancer's new hostname check) |
| 54 | + const meta = document.createElement('meta') |
| 55 | + meta.name = 'hostname' |
| 56 | + meta.content = 'github.com' |
| 57 | + document.head.appendChild(meta) |
| 58 | + } |
80 | 59 |
|
81 | | - const enhancedTextarea = enhancers.tryToEnhance(mockTextarea) |
82 | | - expect(enhancedTextarea).toBeNull() |
| 60 | + it('should identify gh_pr textarea and create proper spot object', async () => { |
| 61 | + const enhancers = new EnhancerRegistry() |
| 62 | + const url = PAGES.gh_pr |
| 63 | + |
| 64 | + // Load the HTML from HAR file |
| 65 | + const html = await loadHarHtml('gh_pr') |
| 66 | + |
| 67 | + // Setup DOM environment with proper location |
| 68 | + setupDOMEnvironment(url, html) |
| 69 | + |
| 70 | + // Get all textarea elements from the page |
| 71 | + const textareas = document.querySelectorAll('textarea') |
| 72 | + |
| 73 | + // Try to enhance each textarea - should find at least one GitHub textarea |
| 74 | + let enhancedCount = 0 |
| 75 | + let lastEnhancedResult: any = null |
| 76 | + |
| 77 | + for (const textarea of textareas) { |
| 78 | + const enhancedTextarea = enhancers.tryToEnhance(textarea as HTMLTextAreaElement) |
| 79 | + if (enhancedTextarea) { |
| 80 | + enhancedCount++ |
| 81 | + lastEnhancedResult = enhancedTextarea |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + expect(enhancedCount).toBeGreaterThan(0) |
| 86 | + expect(lastEnhancedResult).toBeTruthy() |
| 87 | + |
| 88 | + // Snapshot test on the spot object structure |
| 89 | + expect(lastEnhancedResult.spot).toMatchInlineSnapshot(` |
| 90 | + { |
| 91 | + "domain": "github.com", |
| 92 | + "number": 517, |
| 93 | + "slug": "diffplug/selfie", |
| 94 | + "type": "GH_PR_ADD_COMMENT", |
| 95 | + "unique_key": "github.com:diffplug/selfie:517", |
| 96 | + } |
| 97 | + `) |
| 98 | + |
| 99 | + // Verify specific fields based on the URL |
| 100 | + const urlObj = new URL(url) |
| 101 | + const match = urlObj.pathname.match(/^\/([^/]+)\/([^/]+)\/(?:pull|issues)\/(\d+)/) |
| 102 | + expect(match).toBeTruthy() // Ensure URL pattern matches |
| 103 | + |
| 104 | + const [, owner, repo, numberStr] = match! |
| 105 | + |
| 106 | + expect(owner).toBeDefined() |
| 107 | + expect(repo).toBeDefined() |
| 108 | + expect(numberStr).toBeDefined() |
| 109 | + |
| 110 | + expect(lastEnhancedResult.spot.slug).toBe(`${owner}/${repo}`) |
| 111 | + expect(lastEnhancedResult.spot.number).toBe(parseInt(numberStr!, 10)) |
| 112 | + expect(lastEnhancedResult.spot.unique_key).toBe(`github.com:${owner}/${repo}:${numberStr}`) |
83 | 113 | }) |
84 | 114 | }) |
0 commit comments