fix: issue #14 wrong sorting in category and tag archives

This commit is contained in:
2026-07-05 16:09:33 +02:00
parent 7dfcd78abd
commit bb82f1d4a1
5 changed files with 109 additions and 5 deletions

View File

@@ -0,0 +1,39 @@
defmodule BDS.Generation.ArchiveOrderingTest do
@moduledoc """
Category, tag and date archive listings must render newest first. The
generation post index is built by prepending, so this guards against the
regression where grouped archive lists came out in an unstable/oldest-first
order.
"""
use ExUnit.Case, async: true
alias BDS.Generation.Data
defp post(slug, created_at, categories, tags) do
%{
id: slug,
slug: slug,
created_at: created_at,
published_at: created_at,
categories: categories,
tags: tags
}
end
test "categories, tags and dates are ordered newest first regardless of input order" do
older = post("older", 1_700_000_000_000, ["article"], ["elixir"])
newest = post("newest", 1_700_000_200_000, ["article"], ["elixir"])
middle = post("middle", 1_700_000_100_000, ["article"], ["elixir"])
# Deliberately unordered input.
index = Data.build_generation_post_index([older, newest, middle])
expected = ["newest", "middle", "older"]
assert Enum.map(index.posts_by_category["article"], & &1.slug) == expected
assert Enum.map(index.posts_by_tag["elixir"], & &1.slug) == expected
{year, _month, _day} = BDS.Generation.Paths.local_date_parts!(newest.created_at)
assert Enum.map(index.posts_by_year[year], & &1.slug) == expected
end
end

View File

@@ -246,7 +246,7 @@ defmodule BDS.RenderingTest do
title: "Render Day Blocks",
kind: :list,
content:
"range={{ min_date }}-{{ max_date }}|heading={{ show_archive_range_heading }}|blocks={% for block in day_blocks %}[{{ block.date_label }}:{{ block.posts.size }}:{{ block.show_date_marker }}]{% endfor %}|archive={{ archive_context.kind }}:{{ archive_context.year }}:{{ archive_context.month }}"
"range={{ min_date }}-{{ max_date }}|heading={{ show_archive_range_heading }}|blocks={% for block in day_blocks %}[{{ block.date_label }}:{% for p in block.posts %}{{ p.slug }};{% endfor %}:{{ block.show_date_marker }}]{% endfor %}|archive={{ archive_context.kind }}:{{ archive_context.year }}:{{ archive_context.month }}"
})
assert {:ok, published_list_template} = BDS.Templates.publish_template(list_template.id)
@@ -289,6 +289,19 @@ defmodule BDS.RenderingTest do
tags: [],
categories: [],
href: "/2024/04/01/second/"
},
%{
id: "second-b",
slug: "second-b",
title: "Second B",
excerpt: "two-b",
language: "en",
created_at: second_day + 3_600,
updated_at: second_day + 3_600,
published_at: second_day + 3_600,
tags: [],
categories: [],
href: "/2024/04/01/second-b/"
}
]
@@ -311,9 +324,9 @@ defmodule BDS.RenderingTest do
})
assert rendered =~ "heading=true"
assert rendered =~ "blocks=[2024-03-31:1:true][2024-04-01:1:true]"
assert rendered =~ "blocks=[2024-04-01:second-b;second;:true][2024-03-31:first;:true]"
assert rendered =~ "archive=date:2024:4"
assert rendered =~ "range=1711843200-1711929600"
assert rendered =~ "range=1711843200-1711933200"
end
test "render_post_page falls back to bundled starter template when the published default template file is missing",