feat: more completeness of spec and start at embedding

This commit is contained in:
2026-04-24 07:57:28 +02:00
parent d04117abdc
commit 88f966dae9
10 changed files with 1105 additions and 32 deletions

View File

@@ -0,0 +1,106 @@
defmodule BDS.EmbeddingsTest do
use ExUnit.Case, async: false
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
temp_dir = Path.join(System.tmp_dir!(), "bds-embeddings-#{System.unique_integer([:positive])}")
File.mkdir_p!(temp_dir)
on_exit(fn -> File.rm_rf(temp_dir) end)
{:ok, project} = BDS.Projects.create_project(%{name: "Embeddings", data_path: temp_dir})
%{project: project}
end
test "embeddings index published posts when semantic similarity is enabled and support similarity, duplicates, dismissals, and tag suggestions",
%{project: project} do
assert {:ok, _metadata} =
BDS.Metadata.update_project_metadata(project.id, %{semantic_similarity_enabled: true})
assert {:ok, alpha} =
BDS.Posts.create_post(%{
project_id: project.id,
title: "Space Travel",
content: "space rocket launch orbit mission galaxy",
tags: ["space", "science"],
language: "en"
})
assert {:ok, beta} =
BDS.Posts.create_post(%{
project_id: project.id,
title: "Rocket Mission",
content: "rocket launch mission orbit space station",
tags: ["space", "mission"],
language: "en"
})
assert {:ok, gamma} =
BDS.Posts.create_post(%{
project_id: project.id,
title: "Bread Baking",
content: "flour yeast dough oven loaf kitchen",
tags: ["food"],
language: "en"
})
assert {:ok, alpha} = BDS.Posts.publish_post(alpha.id)
assert {:ok, beta} = BDS.Posts.publish_post(beta.id)
assert {:ok, gamma} = BDS.Posts.publish_post(gamma.id)
assert {:ok, indexed} = BDS.Embeddings.index_unindexed(project.id)
assert Enum.sort(indexed) == Enum.sort([alpha.id, beta.id, gamma.id])
assert {:ok, similar} = BDS.Embeddings.find_similar(alpha.id, 2)
assert length(similar) == 2
assert hd(similar).post_id == beta.id
assert hd(similar).score > List.last(similar).score
assert {:ok, scores} = BDS.Embeddings.compute_similarities(alpha.id, [beta.id, gamma.id])
assert scores[beta.id] > scores[gamma.id]
assert {:ok, suggestions} = BDS.Embeddings.suggest_tags(alpha.id, "rocket orbit mission")
assert "space" in suggestions
assert {:ok, duplicates} = BDS.Embeddings.find_duplicates(project.id)
assert Enum.any?(duplicates, fn pair ->
MapSet.new([pair.post_id_a, pair.post_id_b]) == MapSet.new([alpha.id, beta.id])
end)
assert {:ok, dismissal} = BDS.Embeddings.dismiss_duplicate_pair(alpha.id, beta.id)
assert dismissal.project_id == project.id
assert {:ok, filtered_duplicates} = BDS.Embeddings.find_duplicates(project.id)
refute Enum.any?(filtered_duplicates, fn pair ->
MapSet.new([pair.post_id_a, pair.post_id_b]) == MapSet.new([alpha.id, beta.id])
end)
assert {:ok, alpha} = BDS.Posts.update_post(alpha.id, %{content: "kitchen flour dough loaf"})
assert {:ok, alpha} = BDS.Posts.publish_post(alpha.id)
assert {:ok, updated_scores} = BDS.Embeddings.compute_similarities(alpha.id, [beta.id, gamma.id])
assert updated_scores[gamma.id] > updated_scores[beta.id]
assert {:ok, :deleted} = BDS.Posts.delete_post(gamma.id)
assert {:ok, after_delete} = BDS.Embeddings.compute_similarities(alpha.id, [beta.id, gamma.id])
refute Map.has_key?(after_delete, gamma.id)
end
test "embedding queries are gated off when semantic similarity is disabled", %{project: project} do
assert {:ok, post} =
BDS.Posts.create_post(%{
project_id: project.id,
title: "Disabled",
content: "space rocket mission"
})
assert {:ok, post} = BDS.Posts.publish_post(post.id)
assert {:ok, []} = BDS.Embeddings.find_similar(post.id, 5)
assert {:ok, []} = BDS.Embeddings.find_duplicates(project.id)
assert {:ok, %{}} = BDS.Embeddings.compute_similarities(post.id, [post.id])
end
end

View File

@@ -0,0 +1,98 @@
defmodule BDS.PostLinksTest do
use ExUnit.Case, async: false
alias BDS.Repo
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
temp_dir = Path.join(System.tmp_dir!(), "bds-post-links-#{System.unique_integer([:positive])}")
File.mkdir_p!(temp_dir)
on_exit(fn -> File.rm_rf(temp_dir) end)
{:ok, project} = BDS.Projects.create_project(%{name: "Links", data_path: temp_dir})
%{project: project, temp_dir: temp_dir}
end
test "publishing and updating posts sync outgoing post links and deleting a post removes them", %{
project: project
} do
assert {:ok, target} =
BDS.Posts.create_post(%{
project_id: project.id,
title: "Target Post",
content: "target body"
})
assert {:ok, target} = BDS.Posts.publish_post(target.id)
target_href = canonical_post_href(target)
assert {:ok, source} =
BDS.Posts.create_post(%{
project_id: project.id,
title: "Source Post",
content: "See [Target](#{target_href})"
})
assert {:ok, source} = BDS.Posts.publish_post(source.id)
assert post_links() == [
%{
source_post_id: source.id,
target_post_id: target.id,
link_text: "Target"
}
]
assert {:ok, source} =
BDS.Posts.update_post(source.id, %{
content: "A revised body without a post reference"
})
assert source.status == :draft
assert post_links() == []
assert {:ok, source} =
BDS.Posts.update_post(source.id, %{
content: "Now [Target Again](#{target_href})"
})
assert {:ok, source} = BDS.Posts.publish_post(source.id)
assert post_links() == [
%{
source_post_id: source.id,
target_post_id: target.id,
link_text: "Target Again"
}
]
assert {:ok, :deleted} = BDS.Posts.delete_post(target.id)
assert post_links() == []
end
defp canonical_post_href(post) do
datetime = DateTime.from_unix!(post.created_at)
Path.join([
"",
Integer.to_string(datetime.year),
String.pad_leading(Integer.to_string(datetime.month), 2, "0"),
String.pad_leading(Integer.to_string(datetime.day), 2, "0"),
post.slug,
""
])
end
defp post_links do
Repo.query!(
"SELECT source_post_id, target_post_id, link_text FROM post_links ORDER BY source_post_id, target_post_id",
[]
).rows
|> Enum.map(fn [source_post_id, target_post_id, link_text] ->
%{source_post_id: source_post_id, target_post_id: target_post_id, link_text: link_text}
end)
end
end

View File

@@ -72,6 +72,82 @@ defmodule BDS.RenderingTest do
assert rendered =~ "|2|1|#{published_template.slug}|true"
end
test "render_post_page exposes alternate links, backlinks, and post link context", %{
project: project
} do
assert {:ok, _metadata} =
BDS.Metadata.update_project_metadata(project.id, %{
main_language: "en",
blog_languages: ["en", "de"]
})
assert {:ok, template} =
BDS.Templates.create_template(%{
project_id: project.id,
title: "Render Link Context",
kind: :post,
content:
"alts={% for alt in alternate_links %}[{{ alt.hreflang }}={{ alt.href }}]{% endfor %}|backlinks={% for backlink in backlinks %}[{{ backlink.display_slug }}={{ backlink.title }}={{ backlink.path }}]{% endfor %}|outgoing={% for link in post.outgoing_links %}[{{ link.display_slug }}={{ link.title }}={{ link.href }}]{% endfor %}|incoming={% for link in post.incoming_links %}[{{ link.display_slug }}={{ link.title }}={{ link.href }}]{% endfor %}"
})
assert {:ok, published_template} = BDS.Templates.publish_template(template.id)
assert {:ok, target} =
BDS.Posts.create_post(%{
project_id: project.id,
title: "Linked Target",
content: "target body",
language: "en",
template_slug: published_template.slug
})
assert {:ok, _translation} =
BDS.Posts.upsert_post_translation(target.id, "de", %{
title: "Verlinktes Ziel",
content: "zieltext"
})
assert {:ok, target} = BDS.Posts.publish_post(target.id)
assert {:ok, source} =
BDS.Posts.create_post(%{
project_id: project.id,
title: "Linking Source",
content: "See [Target Link](#{canonical_post_href(target)})",
language: "en",
template_slug: published_template.slug
})
assert {:ok, source} = BDS.Posts.publish_post(source.id)
assert {:ok, rendered_target} =
Rendering.render_post_page(project.id, published_template.slug, %{
id: target.id,
title: target.title,
content: target.content || "",
slug: target.slug,
language: "en",
template_slug: published_template.slug
})
assert rendered_target =~ "alts=[en=#{canonical_post_href(target)}]"
assert rendered_target =~ "[de=/de#{canonical_post_href(target)}]"
assert rendered_target =~ "backlinks=[linking-source=Linking Source=#{canonical_post_href(source)}]"
assert rendered_target =~ "incoming=[linking-source=Linking Source=#{canonical_post_href(source)}]"
assert {:ok, rendered_source} =
Rendering.render_post_page(project.id, published_template.slug, %{
id: source.id,
title: source.title,
content: source.content || "",
slug: source.slug,
language: "en",
template_slug: published_template.slug
})
assert rendered_source =~ "outgoing=[linked-target=Linked Target=#{canonical_post_href(target)}]"
end
test "render_list_page exposes pagination and render_not_found_page localizes default copy", %{
project: project
} do
@@ -149,4 +225,95 @@ defmodule BDS.RenderingTest do
assert published_list_template.kind == :list
end
test "render_list_page groups posts into day blocks and exposes archive range fields", %{
project: project
} do
assert {:ok, _metadata} =
BDS.Metadata.update_project_metadata(project.id, %{
main_language: "en",
blog_languages: ["en"]
})
assert {:ok, list_template} =
BDS.Templates.create_template(%{
project_id: project.id,
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 }}"
})
assert {:ok, published_list_template} = BDS.Templates.publish_template(list_template.id)
BDS.Repo.update_all(
from(template in BDS.Templates.Template,
where:
template.project_id == ^project.id and template.kind == :list and
template.id != ^published_list_template.id
),
set: [enabled: false]
)
first_day = 1_711_843_200
second_day = first_day + 86_400
posts = [
%{
id: "first",
slug: "first",
title: "First",
excerpt: "one",
language: "en",
created_at: first_day,
updated_at: first_day,
published_at: first_day,
tags: [],
categories: [],
href: "/2024/03/31/first/"
},
%{
id: "second",
slug: "second",
title: "Second",
excerpt: "two",
language: "en",
created_at: second_day,
updated_at: second_day,
published_at: second_day,
tags: [],
categories: [],
href: "/2024/04/01/second/"
}
]
assert {:ok, rendered} =
Rendering.render_list_page(project.id, %{
language: "en",
page_title: "Archive",
posts: posts,
archive_context: %{kind: "date", year: 2024, month: 4},
pagination: %{
current_page: 1,
total_pages: 1,
total_items: 2,
items_per_page: 10,
has_prev_page: false,
prev_page_href: nil,
has_next_page: false,
next_page_href: nil
}
})
assert rendered =~ "heading=true"
assert rendered =~ "blocks=[2024-03-31:1:true][2024-04-01:1:true]"
assert rendered =~ "archive=date:2024:4"
assert rendered =~ "range=1711843200-1711929600"
end
defp canonical_post_href(post) do
datetime = DateTime.from_unix!(post.created_at)
"/#{datetime.year}/#{String.pad_leading(Integer.to_string(datetime.month), 2, "0")}/#{String.pad_leading(Integer.to_string(datetime.day), 2, "0")}/#{post.slug}/"
end
end