This repository was archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathgitlab.test.ts
More file actions
211 lines (188 loc) · 9.02 KB
/
gitlab.test.ts
File metadata and controls
211 lines (188 loc) · 9.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import assert from 'assert'
import { afterEach, beforeEach, describe, it } from 'mocha'
import type { Settings } from '@sourcegraph/shared/src/settings/settings'
import { createDriverForTest, type Driver } from '@sourcegraph/shared/src/testing/driver'
import { setupExtensionMocking, simpleHoverProvider } from '@sourcegraph/shared/src/testing/integration/mockExtension'
import { afterEachSaveScreenshotIfFailed } from '@sourcegraph/shared/src/testing/screenshotReporter'
import { readEnvironmentString, retry } from '@sourcegraph/shared/src/testing/utils'
import { type BrowserIntegrationTestContext, createBrowserIntegrationTestContext } from './context'
import { closeInstallPageTab } from './shared'
describe('GitLab', () => {
let driver: Driver
before(async () => {
driver = await createDriverForTest({ loadExtension: true })
await closeInstallPageTab(driver.browser)
if (driver.sourcegraphBaseUrl !== 'https://sourcegraph.com') {
await driver.setExtensionSourcegraphUrl()
}
})
after(() => driver?.close())
let testContext: BrowserIntegrationTestContext
beforeEach(async function () {
testContext = await createBrowserIntegrationTestContext({
driver,
currentTest: this.currentTest!,
directory: __dirname,
})
// Requests to other origins that we need to ignore to prevent breaking tests.
testContext.server.any('https://snowplow.trx.gitlab.net/*').intercept((request, response) => {
response.setHeader('Access-Control-Allow-Origin', 'https://gitlab.com')
response.setHeader('Access-Control-Allow-Credentials', 'true')
response.setHeader('Access-Control-Allow-Headers', 'Content-Type')
response.sendStatus(200)
})
testContext.server.any('https://gitlab.com/api/v4/projects/*').intercept((request, response) => {
response.sendStatus(200).send(JSON.stringify({ visibility: 'public' }))
})
testContext.server.any('https://sentry.gitlab.net/*').intercept((request, response) => {
response.sendStatus(200)
})
testContext.overrideGraphQL({
ViewerSettings: () => ({
viewerSettings: {
subjects: [],
merged: { contents: '', messages: [] },
},
}),
ResolveRepoName: () => ({
repository: {
name: 'gitlab.com/SourcegraphCody/jsonrpc2',
},
}),
ResolveRev: () => ({
repository: {
mirrorInfo: {
cloned: true,
},
commit: {
oid: '1'.repeat(40),
},
},
}),
ResolveRepo: ({ rawRepoName }) => ({
repository: {
name: rawRepoName,
},
}),
ResolveRawRepoName: ({ repoName }) => ({
repository: { uri: `${repoName}`, mirrorInfo: { cloned: true } },
}),
BlobContent: () => ({
repository: {
commit: {
file: {
content:
'package jsonrpc2\n\n// CallOption is an option that can be provided to (*Conn).Call to\n// configure custom behavior. See Meta.\ntype CallOption interface {\n\tapply(r *Request) error\n}\n\ntype callOptionFunc func(r *Request) error\n\nfunc (c callOptionFunc) apply(r *Request) error { return c(r) }\n\n// Meta returns a call option which attaches the given meta object to\n// the JSON-RPC 2.0 request (this is a Sourcegraph extension to JSON\n// RPC 2.0 for carrying metadata).\nfunc Meta(meta interface{}) CallOption {\n\treturn callOptionFunc(func(r *Request) error {\n\t\treturn r.SetMeta(meta)\n\t})\n}\n\n// PickID returns a call option which sets the ID on a request. Care must be\n// taken to ensure there are no conflicts with any previously picked ID, nor\n// with the default sequence ID.\nfunc PickID(id ID) CallOption {\n\treturn callOptionFunc(func(r *Request) error {\n\t\tr.ID = id\n\t\treturn nil\n\t})\n}\n',
},
},
},
}),
SiteProductVersion: () => ({
site: {
productVersion: '129819_2022-02-08_baac612f829f',
buildVersion: '129819_2022-02-08_baac612f829f',
hasCodeIntelligence: true,
},
}),
})
// Ensure that the same assets are requested in all environments.
await driver.page.emulateMediaFeatures([{ name: 'prefers-color-scheme', value: 'light' }])
})
afterEachSaveScreenshotIfFailed(() => driver.page)
afterEach(() => testContext?.dispose())
it('adds "view on Sourcegraph" buttons to files', async () => {
if (readEnvironmentString({ variable: 'POLLYJS_MODE', defaultValue: 'replay' }) === 'replay') {
// mock Sourcegraph icon and bootstrap.js loaded by the extension
// TODO: double-check it after we update tests snapshots
for (const url of [
'https://gitlab.com/uploads/-/system/group/avatar/*',
'https://gitlab.com/assets/webpack/164.0d16728f.chunk.js',
]) {
testContext.server.get(url).intercept((request, response) => {
response.sendStatus(200)
})
}
}
const repoName = 'gitlab.com/SourcegraphCody/jsonrpc2'
const url =
'https://gitlab.com/SourcegraphCody/jsonrpc2/blob/4fb7cd90793ee6ab445f466b900e6bffb9b63d78/call_opt.go'
await driver.page.goto(url)
await driver.page.waitForSelector('[data-testid="code-view-toolbar"] [data-testid="open-on-sourcegraph"]', {
timeout: 10000,
})
assert.strictEqual(
(await driver.page.$$('[data-testid="code-view-toolbar"] [data-testid="open-on-sourcegraph"]')).length,
1
)
await retry(async () => {
assert.strictEqual(
await driver.page.evaluate(
() =>
document.querySelector<HTMLAnchorElement>(
'[data-testid="code-view-toolbar"] [data-testid="open-on-sourcegraph"]'
)?.href
),
new URL(
`${driver.sourcegraphBaseUrl}/${repoName}@4fb7cd90793ee6ab445f466b900e6bffb9b63d78/-/blob/call_opt.go`
).href
)
})
})
// TODO(sqs): skipped because these have not been reimplemented after the extension API deprecation
it.skip('shows hover tooltips when hovering a token', async () => {
const { mockExtension, extensionSettings } = setupExtensionMocking()
const userSettings: Settings = {
extensions: extensionSettings,
}
testContext.overrideGraphQL({
ViewerSettings: () => ({
viewerSettings: {
subjects: [
{
__typename: 'User',
displayName: 'Test User',
id: 'TestUserSettingsID',
latestSettings: {
id: 123,
contents: JSON.stringify(userSettings),
},
username: 'test',
viewerCanAdminister: true,
settingsURL: '/users/test/settings',
},
],
merged: { contents: JSON.stringify(userSettings), messages: [] },
},
}),
})
// Serve a mock extension with a simple hover provider
mockExtension({
id: 'simple/hover',
bundle: simpleHoverProvider,
})
await driver.page.goto(
'https://gitlab.com/SourcegraphCody/jsonrpc2/blob/4fb7cd90793ee6ab445f466b900e6bffb9b63d78/call_opt.go'
)
await driver.page.waitForSelector('[data-testid="code-view-toolbar"] [data-testid="open-on-sourcegraph"]')
// Trigger tokenization of the line.
const lineNumber = 16
const line = await driver.page.waitForSelector(`#LC${lineNumber}`, {
timeout: 10000,
})
if (!line) {
throw new Error(`Found no line with number ${lineNumber}`)
}
// Hover line to give codeintellify time to register listeners for
// tokenization (only necessary in CI, not sure why).
await line.hover()
const [token] = await line.$x('.//span[text()="CallOption"]')
await token.hover()
await driver.findElementWithText('User is hovering over CallOption', {
selector: '[data-testid="hover-overlay-content"] > p',
fuzziness: 'contains',
wait: {
timeout: 6000,
},
})
})
})