Skip to content

Commit 566162c

Browse files
committed
Rename har-fixture to corpus-fixture
1 parent c5e9635 commit 566162c

File tree

3 files changed

+40
-81
lines changed

3 files changed

+40
-81
lines changed

browser-extension/tests/har-fixture.ts renamed to browser-extension/tests/corpus-fixture.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,38 +31,19 @@ vi.mock('overtype', () => {
3131

3232
import { describe as baseDescribe, test as baseTest, expect } from 'vitest'
3333
import type { CORPUS } from './corpus/_corpus-index'
34-
import { cleanupDOM, setupDOM, setupHarDOM } from './har-fixture-utils'
34+
import { cleanupDOM, setupDOM } from './corpus-utils'
3535

3636
export const describe = baseDescribe
3737

3838
// Re-export expect from vitest
3939
export { expect }
4040

41-
// Fluent interface for HAR-based tests
42-
export function usingHar(harKey: keyof typeof CORPUS) {
43-
return {
44-
it: (name: string, fn: () => void | Promise<void>) => {
45-
return baseTest(`${String(harKey)}:${name}`, async () => {
46-
// Setup HAR DOM before test
47-
await setupHarDOM(harKey)
48-
49-
try {
50-
return await fn()
51-
} finally {
52-
// Cleanup after test
53-
cleanupDOM()
54-
}
55-
})
56-
},
57-
}
58-
}
59-
6041
// Fluent interface for any corpus type (HAR or HTML)
61-
export function using(corpusKey: keyof typeof CORPUS) {
42+
export function forCorpus(corpusKey: keyof typeof CORPUS) {
6243
return {
6344
it: (name: string, fn: () => void | Promise<void>) => {
6445
return baseTest(`${String(corpusKey)}:${name}`, async () => {
65-
// Setup DOM for any corpus type
46+
// Setup DOM for any corpus type (delegates to HAR or HTML based on type)
6647
await setupDOM(corpusKey)
6748

6849
try {

browser-extension/tests/har-fixture-utils.ts renamed to browser-extension/tests/corpus-utils.ts

Lines changed: 31 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,26 @@ export interface TestDOMContext {
3232
let currentDOMInstance: any = null
3333
let originalGlobals: Partial<TestDOMGlobals> = {}
3434

35-
export async function loadHtmlFromHar(key: keyof typeof CORPUS): Promise<string> {
35+
export async function setupDOM(key: keyof typeof CORPUS): Promise<TestDOMGlobals> {
36+
const entry = CORPUS[key]
37+
if (!entry) {
38+
throw new Error(`Invalid corpus key: ${String(key)}`)
39+
}
40+
41+
let html: string
42+
if (entry.type === 'har') {
43+
html = await loadRootHtmlStringFromHar(key)
44+
} else if (entry.type === 'html') {
45+
html = await loadHtmlStringFromHtml(key)
46+
} else {
47+
throw new Error(`Unsupported corpus type: ${entry.type}`)
48+
}
49+
const domGlobals = createDOMFromString(html, entry.url)
50+
setupDOMFromHar(domGlobals)
51+
return domGlobals
52+
}
53+
54+
async function loadRootHtmlStringFromHar(key: keyof typeof CORPUS): Promise<string> {
3655
const entry = CORPUS[key]
3756
if (!entry || entry.type !== 'har') {
3857
throw new Error(`Invalid HAR corpus key: ${String(key)}`)
@@ -42,15 +61,22 @@ export async function loadHtmlFromHar(key: keyof typeof CORPUS): Promise<string>
4261
const harContent = await fs.readFile(harPath, 'utf-8')
4362
const harData: HarFile = JSON.parse(harContent)
4463
const mainEntry = harData.log.entries.find((entry) => entry.request.url === url)
45-
4664
if (!mainEntry) {
4765
throw new Error(`No entry found for URL: ${url} in HAR file: ${harPath}`)
4866
}
67+
return mainEntry.response.content.text!
68+
}
4969

50-
return mainEntry.response.content.text || ''
70+
async function loadHtmlStringFromHtml(key: keyof typeof CORPUS): Promise<string> {
71+
const entry = CORPUS[key]
72+
if (!entry || entry.type !== 'html') {
73+
throw new Error(`Invalid HTML corpus key: ${String(key)}`)
74+
}
75+
const htmlPath = path.join(__dirname, 'corpus', 'html', `${String(key)}.html`)
76+
return await fs.readFile(htmlPath, 'utf-8')
5177
}
5278

53-
export function createDOMFromHar(html: string, url: string): TestDOMGlobals {
79+
function createDOMFromString(html: string, url: string): TestDOMGlobals {
5480
const dom = parseHTML(html)
5581

5682
return {
@@ -72,7 +98,7 @@ export function createDOMFromHar(html: string, url: string): TestDOMGlobals {
7298
}
7399
}
74100

75-
export function setupDOMFromHar(domGlobals: TestDOMGlobals): void {
101+
function setupDOMFromHar(domGlobals: TestDOMGlobals): void {
76102
// Store original globals for cleanup
77103
originalGlobals = {
78104
Document: (globalThis as any).Document,
@@ -104,51 +130,3 @@ export function cleanupDOM(): void {
104130
originalGlobals = {}
105131
}
106132
}
107-
108-
export async function loadHtmlFromHtml(key: keyof typeof CORPUS): Promise<string> {
109-
const entry = CORPUS[key]
110-
if (!entry || entry.type !== 'html') {
111-
throw new Error(`Invalid HTML corpus key: ${String(key)}`)
112-
}
113-
const htmlPath = path.join(__dirname, 'corpus', 'html', `${String(key)}.html`)
114-
return await fs.readFile(htmlPath, 'utf-8')
115-
}
116-
117-
export async function setupHtmlDOM(key: keyof typeof CORPUS): Promise<TestDOMGlobals> {
118-
const html = await loadHtmlFromHtml(key)
119-
const entry = CORPUS[key]
120-
if (!entry || entry.type !== 'html') {
121-
throw new Error(`Invalid HTML corpus key: ${String(key)}`)
122-
}
123-
const url = entry.url
124-
const domGlobals = createDOMFromHar(html, url)
125-
setupDOMFromHar(domGlobals)
126-
return domGlobals
127-
}
128-
129-
export async function setupHarDOM(key: keyof typeof CORPUS): Promise<TestDOMGlobals> {
130-
const html = await loadHtmlFromHar(key)
131-
const entry = CORPUS[key]
132-
if (!entry || entry.type !== 'har') {
133-
throw new Error(`Invalid HAR corpus key: ${String(key)}`)
134-
}
135-
const url = entry.url
136-
const domGlobals = createDOMFromHar(html, url)
137-
setupDOMFromHar(domGlobals)
138-
return domGlobals
139-
}
140-
141-
export async function setupDOM(key: keyof typeof CORPUS): Promise<TestDOMGlobals> {
142-
const entry = CORPUS[key]
143-
if (!entry) {
144-
throw new Error(`Invalid corpus key: ${String(key)}`)
145-
}
146-
147-
if (entry.type === 'har') {
148-
return await setupHarDOM(key)
149-
} else if (entry.type === 'html') {
150-
return await setupHtmlDOM(key)
151-
} else {
152-
throw new Error(`Unsupported corpus type: ${entry.type}`)
153-
}
154-
}

browser-extension/tests/lib/enhancers/github.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, using, usingHar } from '../../har-fixture'
1+
import { describe, expect, forCorpus as withCorpus } from '../../corpus-fixture'
22

33
// must import fixture **first** for mocks, the `expect` keeps biome from changing sort-order
44
expect
@@ -35,7 +35,7 @@ function enhancements(document: Document, window: Window) {
3535
}
3636

3737
describe('github', () => {
38-
usingHar('gh_pr').it('should create the correct spot object', async () => {
38+
withCorpus('gh_pr').it('should create the correct spot object', async () => {
3939
expect(enhancements(document, window)).toMatchInlineSnapshot(`
4040
[
4141
{
@@ -70,7 +70,7 @@ describe('github', () => {
7070
]
7171
`)
7272
})
73-
usingHar('gh_new_pr').it('should create the correct spot object', async () => {
73+
withCorpus('gh_new_pr').it('should create the correct spot object', async () => {
7474
expect(enhancements(document, window)).toMatchInlineSnapshot(`
7575
[
7676
{
@@ -102,7 +102,7 @@ describe('github', () => {
102102
]
103103
`)
104104
})
105-
usingHar('gh_issue').it('no enhancement on initial page load', async () => {
105+
withCorpus('gh_issue').it('no enhancement on initial page load', async () => {
106106
expect(enhancements(document, window)).toMatchInlineSnapshot(`
107107
[
108108
{
@@ -112,7 +112,7 @@ describe('github', () => {
112112
]
113113
`)
114114
})
115-
using('gh_issue_populated_comment').it('should create the correct spot object', async () => {
115+
withCorpus('gh_issue_populated_comment').it('should create the correct spot object', async () => {
116116
expect(enhancements(document, window)).toMatchInlineSnapshot(`
117117
[
118118
{
@@ -147,7 +147,7 @@ describe('github', () => {
147147
]
148148
`)
149149
})
150-
usingHar('gh_new_issue').it('should create the correct spot object', async () => {
150+
withCorpus('gh_new_issue').it('should create the correct spot object', async () => {
151151
expect(enhancements(document, window)).toMatchInlineSnapshot(`
152152
[
153153
{

0 commit comments

Comments
 (0)