Skip to content

Commit 02067b9

Browse files
committed
add integration tests for the external link sanitizer
1 parent 7047abb commit 02067b9

File tree

5 files changed

+91
-10
lines changed

5 files changed

+91
-10
lines changed

Sources/UnidocQueries/Packages/Unidoc.PackageCreated.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,44 @@ import MongoQL
33
import UnidocDB
44
import UnixTime
55

6+
extension Unidoc
7+
{
8+
/// A predicate that matches all packages whose associated GitHub repositories are owned by
9+
/// a particular user or organization.
10+
@frozen public
11+
struct PackageOwner:Equatable, Hashable, Sendable
12+
{
13+
@usableFromInline
14+
let owner:String
15+
@usableFromInline
16+
let limit:Int
17+
18+
@inlinable
19+
init(by owner:String, limit:Int)
20+
{
21+
self.owner = owner
22+
self.limit = limit
23+
}
24+
}
25+
}
26+
extension Unidoc.PackageOwner:Unidoc.PackagePredicate
27+
{
28+
@inlinable public
29+
var hint:Mongo.CollectionIndex? { Unidoc.DB.Packages.indexAccount }
30+
31+
public
32+
func extend(pipeline:inout Mongo.PipelineEncoder)
33+
{
34+
pipeline[stage: .match] = .init
35+
{
36+
$0[Unidoc.PackageMetadata[.repo]] { $0[.exists] = true }
37+
$0[Unidoc.PackageMetadata[.repo] / Unidoc.PackageRepo[.account]] = self.owner
38+
}
39+
40+
pipeline[stage: .limit] = self.limit
41+
}
42+
}
43+
644
extension Unidoc
745
{
846
/// A predicate that matches all packages whose associated GitHub repositories were created

Sources/UnidocQueryTests/Tests/SymbolQueries.swift

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,10 @@ struct SymbolQueries:UnidocDatabaseTestBattery
195195
tests.expect(tree.rows ..?
196196
[
197197
.init(
198-
shoot: .init(stem: "BarbieCore Getting-Started"),
198+
shoot: .init(stem: "BarbieCore External-links"),
199+
type: .text("External links")),
200+
.init(
201+
shoot: .init(stem: "BarbieCore Getting-started"),
199202
type: .text("Getting started")),
200203
.init(
201204
shoot: .init(stem: "BarbieCore Barbie"),
@@ -231,7 +234,7 @@ struct SymbolQueries:UnidocDatabaseTestBattery
231234
for case (let tests?, let path, let expected) in
232235
[
233236
(
234-
tests / "Barbie" / "Dreamhouse",
237+
tests / "Barbie" / "Dreamhouse" as TestGroup?,
235238
["barbiecore", "barbie", "dreamhouse"][...],
236239
[
237240
"Int": [1],
@@ -243,7 +246,7 @@ struct SymbolQueries:UnidocDatabaseTestBattery
243246
] as [String: [Int]]
244247
),
245248
(
246-
tests / "Barbie" / "Dreamhouse" / "Keys",
249+
tests / "Barbie" / "Dreamhouse" / "Keys" as TestGroup?,
247250
["barbiecore", "barbie", "dreamhouse", "keys"][...],
248251
[
249252
"Int": [1],
@@ -331,15 +334,14 @@ struct SymbolQueries:UnidocDatabaseTestBattery
331334
}
332335
}
333336
}
334-
/// The symbol graph linker should have mangled the space in `Getting Started.md`
337+
/// The symbol graph linker should have mangled the space in `Getting started.md`
335338
/// into a hyphen.
336339
if let tests:TestGroup = tests / "Barbie" / "GettingStarted"
337340
{
338341
let query:Unidoc.VertexQuery<Unidoc.LookupAdjacent> = .init(
339342
"swift-malibu", ["barbiecore", "getting-started"])
340343
await tests.do
341344
{
342-
343345
if let output:Unidoc.VertexOutput = tests.expect(
344346
value: try await session.query(database: unidoc.id, with: query)),
345347
let _:Unidoc.AnyVertex = tests.expect(
@@ -349,6 +351,44 @@ struct SymbolQueries:UnidocDatabaseTestBattery
349351
}
350352
}
351353

354+
if let tests:TestGroup = tests / "Barbie" / "ExternalLinks"
355+
{
356+
let query:Unidoc.VertexQuery<Unidoc.LookupAdjacent> = .init(
357+
"swift-malibu", ["barbiecore", "external-links"])
358+
await tests.do
359+
{
360+
if let output:Unidoc.VertexOutput = tests.expect(
361+
value: try await session.query(database: unidoc.id, with: query)),
362+
let vertex:Unidoc.AnyVertex = tests.expect(
363+
value: output.principal?.vertex),
364+
let prose:Unidoc.Passage = tests.expect(
365+
value: vertex.overview)
366+
{
367+
// TODO: We should be optimizing away duplicate outlines.
368+
tests.expect(prose.outlines.count ==? 4)
369+
tests.expect(prose.outlines[..<3] ..? [
370+
.link(https: "en.wikipedia.org/wiki/Main_Page", safe: true),
371+
.link(https: "en.wikipedia.org/wiki/Main_Page", safe: true),
372+
.link(https: "liberationnews.org", safe: false),
373+
])
374+
375+
guard
376+
case .path(let text, let path) = prose.outlines[3]
377+
else
378+
{
379+
tests.expect(value: nil as [Unidoc.Scalar]?)
380+
return
381+
}
382+
383+
// TODO: This is not a bug, but it is a missed optimization opportunity.
384+
// We do not need to resolve the two leading path components, as they will
385+
// never be shown to the user.
386+
tests.expect(text ==? "swift string index")
387+
tests.expect(path.count ==? 3)
388+
}
389+
}
390+
}
391+
352392
if let tests:TestGroup = tests / "SeeAlso"
353393
{
354394
if let test:TestCase = .init(tests / "GeneratedFromTopics",

TestPackages/swift-malibu/Sources/BarbieCore/documentation/BarbieCore.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
This is a module called `BarbieCore`. It declares a namespace called ``Barbie``.
44

5-
Get started with `BarbieCore` by reading <doc:Getting-Started>. You can’t
5+
Get started with `BarbieCore` by reading <doc:Getting-started>. You can’t
66
get started by reading <doc:GettingStarted> because it doesn’t exist.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# External links
2+
3+
This is a test page for external links.
4+
Here is an external link in autolink form: <https://en.wikipedia.org/wiki/Main_Page>
5+
Here is an external link to a whitelisted domain: [Wikipedia](https://en.wikipedia.org/wiki/Main_Page)
6+
Here is an external link to a graylisted domain: [Liberation News](https://liberationnews.org)
7+
Here is an external link that can be translated into a local link: [`String.Index`](https://developer.apple.com/documentation/swift/string/index)

TestPackages/swift-malibu/Sources/BarbieCore/documentation/Getting Started.md renamed to TestPackages/swift-malibu/Sources/BarbieCore/documentation/Getting started.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
This article demonstrates how to link to documentation in other modules.
44

5-
Here is an external link to wikipedia: [Wikipedia](https://en.wikipedia.org/wiki/Main_Page)
6-
7-
Here is an external link in autolink form: <https://en.wikipedia.org/wiki/Main_Page>
8-
95
## Codelinks
106

117
You can link to a module with a codelink: ``BarbieCore``.

0 commit comments

Comments
 (0)