Files
firehose/blogex/test/blogex/link_validator_test.exs
T
Firehose Bot c5db0cbda8 Fix compile-time link validation and add tag page links
- Restore HTML link extraction in LinkValidator (removed in a83634d
  under the false premise that post bodies are raw markdown; they
  are HTML rendered by NimblePublisher at compile time). The missing
  regex made extract_links/1 find zero links, silently disabling
  compile-time validation.
- Support /blog/{blog_id}/tag/{tag} links: validate blog ID,
  require non-empty tag (tags are user-defined, e.g. pi.dev).
- Fix invalid links in two posts: tag/Pi.dev -> tag/pi.dev,
  2026-07-13-synthetic-tdd.md -> synthetic-tdd.
- Fix test warnings: use Plug.Test deprecation, unused import,
  runtime-defined TestBlogValid module.
- Add regression tests for HTML extraction and tag page links.
2026-10-08 08:43:51 +01:00

272 lines
8.5 KiB
Elixir

defmodule Blogex.LinkValidatorTest do
use ExUnit.Case
alias Blogex.LinkValidator
describe "extract_links/1" do
test "extracts internal blog links from markdown body" do
body =
"Check out [hello world](/blog/engineering/hello-world) and [release v1](/blog/releases/v1-0-0)."
assert LinkValidator.extract_links(body) == [
"/blog/engineering/hello-world",
"/blog/releases/v1-0-0"
]
end
test "extracts internal blog links from HTML body" do
body =
~s(<p>Check out <a href="/blog/engineering/hello-world">hello world</a> and <a href="/blog/releases/v1-0-0">release v1</a>.</p>)
assert LinkValidator.extract_links(body) == [
"/blog/engineering/hello-world",
"/blog/releases/v1-0-0"
]
end
test "extracts tag page links from HTML body" do
body = ~s(<p>See <a href="/blog/engineering/tag/pi.dev">pi.dev posts</a>.</p>)
assert LinkValidator.extract_links(body) == ["/blog/engineering/tag/pi.dev"]
end
test "ignores external links" do
body = "See [GitHub](https://github.com) and [internal](/blog/engineering/post)."
assert LinkValidator.extract_links(body) == ["/blog/engineering/post"]
end
test "ignores non-blog internal links" do
body = "See [/about](/about) and [/blog/engineering/post](/blog/engineering/post)."
assert LinkValidator.extract_links(body) == ["/blog/engineering/post"]
end
test "returns empty list when no internal blog links" do
body = "Just external links: [GitHub](https://github.com)."
assert LinkValidator.extract_links(body) == []
end
test "handles multiple links on one line" do
body = "[a](/blog/engineering/a) [b](/blog/releases/b) [c](/blog/engineering/c)"
assert LinkValidator.extract_links(body) == [
"/blog/engineering/a",
"/blog/releases/b",
"/blog/engineering/c"
]
end
test "handles links with query strings" do
body = "[link](/blog/engineering/post?foo=bar)"
assert LinkValidator.extract_links(body) == ["/blog/engineering/post?foo=bar"]
end
test "handles links with anchor fragments" do
body = "[link](/blog/engineering/post#section)"
assert LinkValidator.extract_links(body) == ["/blog/engineering/post#section"]
end
test "handles empty body" do
assert LinkValidator.extract_links("") == []
end
end
describe "validate_link/1" do
test "validates correct engineering link" do
assert LinkValidator.validate_link("/blog/engineering/my-post") == :ok
end
test "validates correct releases link" do
assert LinkValidator.validate_link("/blog/releases/v1-0-0") == :ok
end
test "rejects unknown blog ID" do
assert LinkValidator.validate_link("/blog/unknown/post") ==
{:error, "unknown blog ID: unknown"}
end
test "rejects uppercase blog ID" do
assert LinkValidator.validate_link("/blog/Engineering/post") ==
{:error, "unknown blog ID: Engineering"}
end
test "rejects empty slug" do
assert LinkValidator.validate_link("/blog/engineering/") ==
{:error, "empty slug"}
end
test "rejects slug with uppercase letters" do
assert LinkValidator.validate_link("/blog/engineering/My-Post") ==
{:error, "slug must be lowercase alphanumeric with hyphens: My-Post"}
end
test "rejects slug with special characters" do
assert LinkValidator.validate_link("/blog/engineering/hello@world") ==
{:error, "slug must be lowercase alphanumeric with hyphens: hello@world"}
end
test "rejects slug with spaces" do
assert LinkValidator.validate_link("/blog/engineering/hello world") ==
{:error, "slug must be lowercase alphanumeric with hyphens: hello world"}
end
test "allows single-word slug" do
assert LinkValidator.validate_link("/blog/engineering/hello") == :ok
end
test "allows hyphenated slug" do
assert LinkValidator.validate_link("/blog/engineering/my-cool-post") == :ok
end
test "allows slug with numbers" do
assert LinkValidator.validate_link("/blog/releases/v1-2-3") == :ok
end
test "rejects slug starting with hyphen" do
assert LinkValidator.validate_link("/blog/engineering/-post") ==
{:error, "slug must be lowercase alphanumeric with hyphens: -post"}
end
test "rejects slug ending with hyphen" do
assert LinkValidator.validate_link("/blog/engineering/post-") ==
{:error, "slug must be lowercase alphanumeric with hyphens: post-"}
end
test "rejects consecutive hyphens" do
assert LinkValidator.validate_link("/blog/engineering/post--name") ==
{:error, "slug must be lowercase alphanumeric with hyphens: post--name"}
end
test "returns :ok for link with query string and valid slug" do
assert LinkValidator.validate_link("/blog/engineering/post?foo=bar") == :ok
end
test "returns :ok for link with anchor fragment and valid slug" do
assert LinkValidator.validate_link("/blog/engineering/post#section") == :ok
end
test "allows tag page links" do
assert LinkValidator.validate_link("/blog/engineering/tag/pi.dev") == :ok
assert LinkValidator.validate_link("/blog/releases/tag/v1") == :ok
end
test "allows tag page links with fragments" do
assert LinkValidator.validate_link("/blog/engineering/tag/pi.dev#top") == :ok
end
test "rejects tag page link with empty tag" do
assert LinkValidator.validate_link("/blog/engineering/tag/") ==
{:error, "empty tag in tag link"}
end
test "rejects unknown blog ID in tag page link" do
assert LinkValidator.validate_link("/blog/unknown/tag/pi.dev") ==
{:error, "unknown blog ID: unknown"}
end
test "rejects non-blog path" do
assert LinkValidator.validate_link("/about") ==
{:error, "not a blog link: /about"}
end
test "rejects malformed link" do
assert LinkValidator.validate_link("not-a-url") ==
{:error, "not a blog link: not-a-url"}
end
end
describe "validate_links/1" do
test "returns :ok when all links are valid" do
links = [
"/blog/engineering/hello-world",
"/blog/releases/v1-0-0"
]
assert LinkValidator.validate_links(links) == :ok
end
test "returns errors for invalid links" do
links = [
"/blog/engineering/hello-world",
"/blog/unknown/post",
"/blog/releases/My-Post"
]
assert LinkValidator.validate_links(links) == {
:error,
[
{2, "/blog/unknown/post", "unknown blog ID: unknown"},
{3, "/blog/releases/My-Post",
"slug must be lowercase alphanumeric with hyphens: My-Post"}
]
}
end
test "returns :ok for empty list" do
assert LinkValidator.validate_links([]) == :ok
end
test "reports line numbers correctly" do
links = [
"/blog/engineering/ok",
"/blog/bad/slug",
"/blog/releases/ok"
]
assert LinkValidator.validate_links(links) == {
:error,
[{2, "/blog/bad/slug", "unknown blog ID: bad"}]
}
end
end
describe "validate_body/2" do
test "returns :ok when body has no internal blog links" do
body = "Just text, no links."
assert LinkValidator.validate_body(body, :engineering) == :ok
end
test "returns :ok when all links are valid" do
body = "[link](/blog/engineering/post)"
assert LinkValidator.validate_body(body, :engineering) == :ok
end
test "returns errors with post context" do
body = "[link](/blog/unknown/post)"
assert LinkValidator.validate_body(body, :engineering) == {
:error,
[
{
1,
"/blog/unknown/post",
"unknown blog ID: unknown",
post_id: nil
}
]
}
end
test "includes post_id in error tuples when provided" do
body = "[link](/blog/unknown/post)"
assert LinkValidator.validate_body(body, :engineering, post_id: "test-post") == {
:error,
[
{
1,
"/blog/unknown/post",
"unknown blog ID: unknown",
post_id: "test-post"
}
]
}
end
end
end