Files
devplacepy/tests/unit/services/community/dedupe.py
T
retoorandClaude Sonnet 5.5 02ab747cd0 Code Farm: per-plot auto-replant pause and crop-lock check; community sync and node work
Continuous Delivery could never be turned off once a crop was planted. Add a
one-shot per-plot "stop after harvest" flag (POST /game/plot/stop, Devii tool
game_plot_stop, game_plots.stop_after_harvest), cleared by the next plant,
autoreplant claim or Refactor. Autoreplant now also respects crop locks through
the shared _crop_lock predicate. Plan and measurements in happyplanting.md.

Also includes the pending community sync, node network and related work in the
tree; the full suite passed (4999) with all of it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-17 02:23:43 +02:00

93 lines
4.0 KiB
Python

# retoor <retoor@molodetz.nl>
import json
from devplacepy.services.community.generator import CodeGenerator
from devplacepy.services.community.runtime import Collector
from tests.conftest import run_async
from tests.unit.services.community.fakes import FakeFetcher
from tests.unit.services.community.generator import load, no_sleep
def feed(identifier, url, comments="none"):
return {
"id": identifier,
"url": url,
"type": "json_api",
"list_path": "$.items",
"post": {"fields": {"id": ["$.id"], "title": ["$.title"], "content": ["$.body"], "created_at": ["$.at"], "url": ["$.link"]}},
"author": {"fields": {"username": ["$.who"]}},
"comments": {"mode": comments},
}
def schema(limits=None):
return {
"community": "twins",
"origin": "twins.test",
"limits": {"delay_seconds": 0, "retries": 1, **(limits or {})},
"feeds": [feed("latest", "https://twins.test/latest"), feed("top", "https://twins.test/top")],
}
def entry(identifier, link, title="A shared story", at="2026-10-01T10:00:00Z"):
return {"id": identifier, "title": title, "body": "a body that is long enough to sync", "at": at, "who": "alice", "link": link}
def routes(latest, top):
return {"https://twins.test/latest": {"items": latest}, "https://twins.test/top": {"items": top}}
def collect(fetcher_routes, state=None, only=None, limits=None):
return run_async(Collector(FakeFetcher(fetcher_routes), schema(limits), state, sleep=no_sleep).run(only))
def uids(result):
return {record["origin_uid"] for record in result["records"]["posts"]}
def test_the_same_story_in_a_second_feed_in_a_later_run_is_not_a_second_post():
shared = "https://news.test/story/42?utm=a"
sources = routes([entry("L1", shared)], [entry("T9", "https://www.news.test/story/42/?utm=b")])
first = collect(sources, only="latest")
assert len(first["records"]["posts"]) == 1 and first["state"]["links"]
second = collect(sources, state=json.loads(json.dumps(first["state"])), only="top")
assert second["records"]["posts"] == []
def test_without_the_remembered_map_the_twin_would_become_a_post():
shared = "https://news.test/story/42"
sources = routes([entry("L1", shared)], [entry("T9", shared)])
first = collect(sources, only="latest")
assert len(collect(sources, state={"feeds": first["state"]["feeds"]}, only="top")["records"]["posts"]) == 1
def test_one_feed_never_merges_its_own_posts_that_share_a_link():
shared = "https://news.test/root"
result = collect(routes([entry("A", shared, "first"), entry("B", shared, "second")], []))
assert len(result["records"]["posts"]) == 2
def test_the_same_feed_keeps_its_own_post_when_it_changes_in_a_later_run():
shared = "https://news.test/story/7"
first = collect(routes([entry("L1", shared)], []))
changed = routes([entry("L1", shared, "A shared story, edited", "2026-10-05T10:00:00Z")], [])
again = collect(changed, state=first["state"])
assert uids(again) == uids(first) and again["records"]["posts"][0]["title"].endswith("edited")
def test_the_remembered_map_is_bounded_and_keeps_the_newest():
items = [entry(f"L{number}", f"https://news.test/story/{number}") for number in range(40)]
result = collect(routes(items, []), limits={"max_remembered_links": 10})
kept = list(result["state"]["links"])
assert len(kept) == 10 and kept[-1].endswith("/story/39") and not any(key.endswith("/story/0") for key in kept)
def test_a_generated_collector_remembers_links_too(tmp_path):
module = load(CodeGenerator(schema()).write(tmp_path))
shared = "https://news.test/story/42"
sources = routes([entry("L1", shared)], [entry("T9", shared)])
first = run_async(module.collect_all(FakeFetcher(sources), only="latest", sleep=no_sleep))
second = run_async(module.collect_all(FakeFetcher(sources), state=json.loads(json.dumps(first["state"])), only="top", sleep=no_sleep))
assert len(first["records"]["posts"]) == 1 and second["records"]["posts"] == []