-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
210 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,102 @@ | ||
import fetch from "@/lib/data"; | ||
import { describe, expect, test } from "bun:test"; | ||
import { DetectedTechnology } from "./parsers/types"; | ||
import type { DetectedTechnology } from "./parsers/types"; | ||
|
||
const DOMAIN_TO_UNEXPECTED_DATA: Record<string, DetectedTechnology[]> = { | ||
"changelog.com": [ | ||
{ | ||
identifier: "subdomain", | ||
metadata: { | ||
value: "op3.dev", | ||
}, | ||
}, | ||
], | ||
"changelog.com": [ | ||
{ | ||
identifier: "subdomain", | ||
metadata: { | ||
value: "op3.dev", | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
const DOMAIN_TO_EXPECTED_DATA: Record<string, DetectedTechnology[]> = { | ||
"formkeep.com": [ | ||
{ | ||
identifier: "github", | ||
metadata: { | ||
username: "formkeep.js", | ||
}, | ||
}, | ||
{ | ||
identifier: "linkedin", | ||
metadata: { username: "formkeep" }, | ||
}, | ||
], | ||
"savvycal.com": [ | ||
{ | ||
identifier: "twitter", | ||
metadata: { username: "savvycal" }, | ||
}, | ||
{ | ||
identifier: "rss", | ||
metadata: { url: "https://savvycal.com/feed.xml" }, | ||
}, | ||
{ | ||
identifier: "rewardful", | ||
metadata: { value: "rewardful", via: "URL" }, | ||
}, | ||
], | ||
"buttondown.email": [ | ||
{ | ||
identifier: "github", | ||
metadata: { username: "buttondown" }, | ||
}, | ||
], | ||
"zed.dev": [ | ||
{ | ||
identifier: "twitter", | ||
metadata: { username: "zeddotdev" }, | ||
}, | ||
], | ||
"bytereview.co.uk": [ | ||
{ | ||
identifier: "tiktok", | ||
metadata: { username: "@bytereview" }, | ||
}, | ||
{ | ||
identifier: "twitter", | ||
metadata: { username: "bytereview" }, | ||
}, | ||
], | ||
"lastwatchdog.com": [ | ||
{ | ||
identifier: "rss", | ||
metadata: { | ||
url: "https://www.lastwatchdog.com/feed/", | ||
}, | ||
}, | ||
], | ||
"formkeep.com": [ | ||
{ | ||
identifier: "github", | ||
metadata: { | ||
username: "formkeep.js", | ||
}, | ||
}, | ||
{ | ||
identifier: "linkedin", | ||
metadata: { username: "formkeep" }, | ||
}, | ||
], | ||
"savvycal.com": [ | ||
{ | ||
identifier: "twitter", | ||
metadata: { username: "savvycal" }, | ||
}, | ||
{ | ||
identifier: "rss", | ||
metadata: { url: "https://savvycal.com/feed.xml" }, | ||
}, | ||
{ | ||
identifier: "rewardful", | ||
metadata: { value: "rewardful", via: "URL" }, | ||
}, | ||
], | ||
"buttondown.email": [ | ||
{ | ||
identifier: "github", | ||
metadata: { username: "buttondown" }, | ||
}, | ||
], | ||
"zed.dev": [ | ||
{ | ||
identifier: "twitter", | ||
metadata: { username: "zeddotdev" }, | ||
}, | ||
], | ||
"bytereview.co.uk": [ | ||
{ | ||
identifier: "tiktok", | ||
metadata: { username: "@bytereview" }, | ||
}, | ||
{ | ||
identifier: "twitter", | ||
metadata: { username: "bytereview" }, | ||
}, | ||
], | ||
}; | ||
|
||
describe("fetching", () => { | ||
Object.entries(DOMAIN_TO_EXPECTED_DATA).forEach(([domain, expectedData]) => { | ||
expectedData.forEach((data) => { | ||
test(`fetches ${data.identifier} for ${domain}`, async () => { | ||
const { detected_technologies } = await fetch(domain); | ||
expect(detected_technologies).toContainEqual(data); | ||
}); | ||
}); | ||
}); | ||
Object.entries(DOMAIN_TO_EXPECTED_DATA).forEach(([domain, expectedData]) => { | ||
expectedData.forEach((data) => { | ||
test(`fetches ${data.identifier} for ${domain}`, async () => { | ||
const { detected_technologies } = await fetch(domain); | ||
expect(detected_technologies).toContainEqual(data); | ||
}); | ||
}); | ||
}); | ||
|
||
Object.entries(DOMAIN_TO_UNEXPECTED_DATA).forEach(([domain, unexpectedData]) => { | ||
unexpectedData.forEach((data) => { | ||
test(`does not fetch ${data.identifier} for ${domain}`, async () => { | ||
const { detected_technologies } = await fetch(domain); | ||
expect(detected_technologies).not.toContainEqual(data); | ||
}); | ||
}); | ||
}); | ||
Object.entries(DOMAIN_TO_UNEXPECTED_DATA).forEach( | ||
([domain, unexpectedData]) => { | ||
unexpectedData.forEach((data) => { | ||
test(`does not fetch ${data.identifier} for ${domain}`, async () => { | ||
const { detected_technologies } = await fetch(domain); | ||
expect(detected_technologies).not.toContainEqual(data); | ||
}); | ||
}); | ||
}, | ||
); | ||
|
||
test("deduping identical records", async () => { | ||
const { detected_technologies } = await fetch("zed.dev"); | ||
expect(detected_technologies.filter((tech) => tech.identifier === "twitter")).toHaveLength(1); | ||
}); | ||
test("deduping identical records", async () => { | ||
const { detected_technologies } = await fetch("zed.dev"); | ||
expect( | ||
detected_technologies.filter((tech) => tech.identifier === "twitter"), | ||
).toHaveLength(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,41 @@ | ||
import fetch from "@/lib/data"; | ||
import type fetch from "@/lib/data"; | ||
import { db } from "@/lib/db/connection"; | ||
|
||
export const reify = async (domain: string, data: Awaited<ReturnType<typeof fetch>>) => { | ||
await db | ||
.insertInto("domains") | ||
.values({ | ||
domain: domain, | ||
data: JSON.stringify(data), | ||
}) | ||
.execute(); | ||
export const reify = async ( | ||
domain: string, | ||
data: Awaited<ReturnType<typeof fetch>>, | ||
) => { | ||
await db | ||
.insertInto("domains") | ||
.values({ | ||
domain: domain, | ||
data: JSON.stringify(data), | ||
}) | ||
.execute(); | ||
|
||
const existingTechnologies = await db | ||
.selectFrom("detected_technologies") | ||
.select("technology") | ||
.where("domain", "=", domain) | ||
.execute(); | ||
const existingTechnologies = await db | ||
.selectFrom("detected_technologies") | ||
.select("technology") | ||
.where("domain", "=", domain) | ||
.execute(); | ||
|
||
const existingTechSet = new Set( | ||
existingTechnologies.map((tech) => tech.technology) | ||
); | ||
const existingTechSet = new Set( | ||
existingTechnologies.map((tech) => tech.technology), | ||
); | ||
|
||
const newTechnologies = data.detected_technologies | ||
.filter( | ||
(technology) => | ||
!existingTechSet.has(technology.identifier) | ||
) | ||
.map((technology) => ({ | ||
domain: domain, | ||
technology: technology.identifier, | ||
data: JSON.stringify(technology.metadata), | ||
creation_date: new Date().toISOString(), | ||
})); | ||
const newTechnologies = data.detected_technologies | ||
.filter((technology) => !existingTechSet.has(technology.identifier)) | ||
.map((technology) => ({ | ||
domain: domain, | ||
technology: technology.identifier, | ||
data: JSON.stringify(technology.metadata), | ||
creation_date: new Date().toISOString(), | ||
})); | ||
|
||
if (newTechnologies.length > 0) { | ||
await db | ||
.insertInto("detected_technologies") | ||
.values(newTechnologies) | ||
.execute(); | ||
} | ||
if (newTechnologies.length > 0) { | ||
await db | ||
.insertInto("detected_technologies") | ||
.values(newTechnologies) | ||
.execute(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.