Skip to content

Commit d11593a

Browse files
committed
Adapt har-fixture to work for html.
1 parent 4e6748c commit d11593a

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

browser-extension/tests/har-fixture-utils.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,27 @@ export function cleanupDOM(): void {
105105
}
106106
}
107107

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+
108129
export async function setupHarDOM(key: keyof typeof CORPUS): Promise<TestDOMGlobals> {
109130
const html = await loadHtmlFromHar(key)
110131
const entry = CORPUS[key]
@@ -116,3 +137,18 @@ export async function setupHarDOM(key: keyof typeof CORPUS): Promise<TestDOMGlob
116137
setupDOMFromHar(domGlobals)
117138
return domGlobals
118139
}
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/har-fixture.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ 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, setupHarDOM } from './har-fixture-utils'
34+
import { cleanupDOM, setupDOM, setupHarDOM } from './har-fixture-utils'
3535

3636
export const describe = baseDescribe
3737

@@ -56,3 +56,22 @@ export function usingHar(harKey: keyof typeof CORPUS) {
5656
},
5757
}
5858
}
59+
60+
// Fluent interface for any corpus type (HAR or HTML)
61+
export function using(corpusKey: keyof typeof CORPUS) {
62+
return {
63+
it: (name: string, fn: () => void | Promise<void>) => {
64+
return baseTest(`${String(corpusKey)}:${name}`, async () => {
65+
// Setup DOM for any corpus type
66+
await setupDOM(corpusKey)
67+
68+
try {
69+
return await fn()
70+
} finally {
71+
// Cleanup after test
72+
cleanupDOM()
73+
}
74+
})
75+
},
76+
}
77+
}

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

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

33
// must import fixture **first** for mocks, the `expect` keeps biome from changing sort-order
44
expect
@@ -102,7 +102,7 @@ describe('github', () => {
102102
]
103103
`)
104104
})
105-
usingHar('gh_issue').it('should create the correct spot object', async () => {
105+
usingHar('gh_issue').it('no enhancement on initial page load', async () => {
106106
expect(enhancements(document, window)).toMatchInlineSnapshot(`
107107
[
108108
{
@@ -112,6 +112,41 @@ describe('github', () => {
112112
]
113113
`)
114114
})
115+
using('gh_issue_populated_comment').it('should create the correct spot object', async () => {
116+
expect(enhancements(document, window)).toMatchInlineSnapshot(`
117+
[
118+
{
119+
"for": "id=:rn: name=null className=prc-Textarea-TextArea-13q4j overtype-input",
120+
"spot": {
121+
"domain": "github.com",
122+
"number": 523,
123+
"slug": "diffplug/selfie",
124+
"title": "TODO_TITLE",
125+
"type": "GH_ISSUE_ADD_COMMENT",
126+
"unique_key": "github.com:diffplug/selfie:523",
127+
},
128+
"title": "TITLE_TODO",
129+
"upperDecoration": <React.Fragment>
130+
<span
131+
className="flex h-4 w-4 flex-shrink-0 items-center justify-center"
132+
>
133+
<IssueOpenedIcon
134+
size={16}
135+
/>
136+
</span>
137+
#
138+
523
139+
<a
140+
className="truncate hover:underline"
141+
href="https://github.com/diffplug/selfie"
142+
>
143+
diffplug/selfie
144+
</a>
145+
</React.Fragment>,
146+
},
147+
]
148+
`)
149+
})
115150
usingHar('gh_new_issue').it('should create the correct spot object', async () => {
116151
expect(enhancements(document, window)).toMatchInlineSnapshot(`
117152
[

0 commit comments

Comments
 (0)