Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/lib/__tests__/source-detection.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect, vi } from "vitest"
import { detectSourceType, SOURCE_TYPES } from "../source-detection"
import { detectSourceType, extractNameFromSource, SOURCE_TYPES } from "../source-detection"

// Mock fetch for RSS probe (checkIfRSS) - default to non-RSS
vi.stubGlobal("fetch", vi.fn().mockResolvedValue({
Expand Down Expand Up @@ -106,6 +106,48 @@ describe("detectSourceType", () => {
})
})

describe("Twitter Handle URLs", () => {
it("detects x.com/handle (no slash) as TWITTER_HANDLE", async () => {
expect(await detectSourceType("https://x.com/sama")).toBe(SOURCE_TYPES.TWITTER_HANDLE)
})

it("detects x.com/handle/ (trailing slash) as TWITTER_HANDLE", async () => {
expect(await detectSourceType("https://x.com/sama/")).toBe(SOURCE_TYPES.TWITTER_HANDLE)
})

it("detects twitter.com/handle (no slash) as TWITTER_HANDLE", async () => {
expect(await detectSourceType("https://twitter.com/sama")).toBe(SOURCE_TYPES.TWITTER_HANDLE)
})

it("detects twitter.com/handle/ (trailing slash) as TWITTER_HANDLE", async () => {
expect(await detectSourceType("https://twitter.com/sama/")).toBe(SOURCE_TYPES.TWITTER_HANDLE)
})

it("detects x.com/@handle (no slash) as TWITTER_HANDLE", async () => {
expect(await detectSourceType("https://x.com/@sama")).toBe(SOURCE_TYPES.TWITTER_HANDLE)
})

it("detects x.com/@handle/ (trailing slash) as TWITTER_HANDLE", async () => {
expect(await detectSourceType("https://x.com/@sama/")).toBe(SOURCE_TYPES.TWITTER_HANDLE)
})

it("detects x.com/handle/?ref=x (trailing slash + query) as TWITTER_HANDLE", async () => {
expect(await detectSourceType("https://x.com/sama/?ref=x")).toBe(SOURCE_TYPES.TWITTER_HANDLE)
})

it("detects x.com/handle/status/123 as TWEET (regression guard)", async () => {
expect(await detectSourceType("https://x.com/sama/status/123456")).toBe(SOURCE_TYPES.TWEET)
})

it("extractNameFromSource returns @sama for no-slash URL", () => {
expect(extractNameFromSource("https://x.com/sama", SOURCE_TYPES.TWITTER_HANDLE)).toBe("@sama")
})

it("extractNameFromSource returns @sama for trailing-slash URL", () => {
expect(extractNameFromSource("https://x.com/sama/", SOURCE_TYPES.TWITTER_HANDLE)).toBe("@sama")
})
})

describe("arXiv Paper URLs", () => {
it("detects arxiv.org/abs/<id> as ARXIV_PAPER", async () => {
expect(await detectSourceType("https://arxiv.org/abs/2301.00001")).toBe(SOURCE_TYPES.ARXIV_PAPER)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/source-detection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const SOURCE_TYPES = {
export type SourceType = (typeof SOURCE_TYPES)[keyof typeof SOURCE_TYPES]

const twitterHandlePattern =
/\b(?:twitter\.com|x\.com)\/(?:@)?([\w_]+)(?:$|\?[^/]*$)/
/\b(?:twitter\.com|x\.com)\/(?:@)?([\w_]+)\/?(?:$|\?[^/]*$)/
const youtubeRegex =
/(https?:\/\/)?(www\.)?youtube\.com\/watch\?v=([A-Za-z0-9_-]+)/
const youtubeLiveRegex =
Expand Down
Loading