Files
ai-agent/backend/test_meta_resources.py
gabrielvidalandClaude Opus 5.5 618b17f6c2 feat(look): Linear projects — the card's colour and icon, a project pill
A resource plugin can declare a `hueRole: group` kind — what an issue
belongs to (a Linear project); its issue kind names it with `groupBy`.
The linear plugin's `linear-project` uses it (plugin 1.1.0, ai-agent-plugins
branch linear-projects). Decided in form linear-projects-icons-colour_yfnahd.

- Lookups only: get_project and list_projects (a new extractor field,
  `itemsPath: projects[]`, reads each item of a listing without tagging
  the conversation) teach Linear's icon and colour; `vendor_icons` turns
  `Brush` / `:heavy_plus_sign:` into glyph keys (`brush`, `emoji:➕`,
  gemoji table vendored). PARSER_VERSION 47 re-mines past lookups.
- Colour (`cardColour: group`, the default): a card wears its issue's
  project colour, each issue of the project a shade (`LookColors.group_color`,
  `shade_hex` ≡ the PWA's `shadeHex`); a palette hue when Linear's is
  unknown; an issue with no known project keeps issue + PR colours.
  A Settings → Tags colour on `linear:project/<name>` wins (the PWA
  re-shades it with `colorGroup` / `colorShade`); *Colour…* on such a card
  writes that tag.
- Icon (`iconOrder: group`): project › label › team; the project's tag
  icon, else Linear's own.
- Pill (`groupPills`): the issue's project beside it, in its own icon and
  colour (`Resource.icon` / `color`; the glyph is tinted, not the pill).
- `groupLooks` off: only what Settings → Tags sets.
- Emoji tag icons (`emoji:<char>`), Linear's look as a project tag's
  derived default in Settings → Tags, the mock seeds two projects.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 15:50:11 +01:00

302 lines
17 KiB
Python

"""Recorded resources in the metadata sidecar (meta.py) and the card view
(conversations/resource_view.py) over the ``github`` / ``linear`` / ``slack``
resource plugins (fixtures/resource_plugins/): merge by kind:id, the
exclusive primary, hiding, who may set the title, each plugin's own scope
(``workers`` / ``cwdGlobs`` / ``notifPills``), bare-key teams, cross-links,
labels and the generic resource title.
``python -m pytest test_meta_resources.py`` from ``backend/``."""
import pathlib
import tempfile
import types
import unittest
import meta as meta_mod
from conversations import resource_view as rv
from fixtures.resource_plugins.kit import WORK_CWD, FakePluginStore, registry
from plugins import resources as plugin_resources
PR = "pr:orus-tech/orus-monorepo#"
MAC = "a8ed565255ed"
ORUS_CWD = "/Users/gabrielvidal/WORK/ORUS/orus"
REG = registry()
def ctx(teams=("INS", "TECH"), channels=None, reg=None, xrefs=None):
return rv.Context(reg or REG, {"linear": set(teams)}, channels or {}, {}, {}, xrefs or {})
class TestMetaStore(unittest.TestCase):
def setUp(self):
self.d = tempfile.TemporaryDirectory()
self.m = meta_mod.MetaStore(str(pathlib.Path(self.d.name) / "m.json"))
def tearDown(self):
self.d.cleanup()
def test_merge_by_key_and_exclusive_primary(self):
self.m.update("s", {"resources": [
{"kind": "pr", "id": "o/r#1", "label": "#1", "primary": True,
"firstSeen": "2026-10-02T00:00:00Z"}]})
cur = self.m.update("s", {"resources": [
{"kind": "pr", "id": "o/r#1", "title": "Fix", "firstSeen": "2026-10-03T00:00:00Z"},
{"kind": "linear", "id": "INS-1", "label": "INS-1", "primary": True}]})
rs = {f"{r['kind']}:{r['id']}": r for r in cur["resources"]}
self.assertEqual(rs["pr:o/r#1"]["title"], "Fix")
self.assertEqual(rs["pr:o/r#1"]["firstSeen"], "2026-10-02T00:00:00Z")
self.assertNotIn("primary", rs["pr:o/r#1"])
self.assertTrue(rs["linear:INS-1"]["primary"])
def test_remove_hides_and_re_adding_unhides(self):
self.m.update("s", {"resources": [{"kind": "pr", "id": "o/r#1"}]})
cur = self.m.update("s", {"resourcesRemove": ["pr:o/r#1", "branch:x"]})
self.assertEqual(cur["resources"], [])
self.assertEqual(cur["resourcesHidden"], ["pr:o/r#1", "branch:x"])
cur = self.m.update("s", {"resources": [{"kind": "branch", "id": "x"}]})
self.assertEqual(cur["resourcesHidden"], ["pr:o/r#1"])
def test_title_sources(self):
cur = self.m.update("s", {"title": "INS-1 · A", "titleSource": "resources"})
self.assertEqual((cur["title"], cur["titleSource"]), ("INS-1 · A", "resources"))
# a stamp never replaces a title
cur = self.m.update("s", {"title": "INS-2 · B", "titleSource": "resources"})
self.assertEqual(cur["title"], "INS-1 · A")
# an agent title replaces a stamp, the owner's rename replaces anything…
cur = self.m.update("s", {"title": "Agent", "titleSource": "agent"})
self.assertEqual(cur["title"], "Agent")
cur = self.m.update("s", {"title": "Mine", "titleSource": "user"})
self.assertEqual((cur["title"], cur["titleSource"]), ("Mine", "user"))
# …and an agent can't take it back
cur = self.m.update("s", {"title": "Agent 2", "titleSource": "agent"})
self.assertEqual(cur["title"], "Mine")
# reset to auto clears the source too
cur = self.m.update("s", {"title": "", "titleSource": "user"})
self.assertEqual((cur["title"], cur["titleSource"]), ("", None))
class TestScope(unittest.TestCase):
"""Each plugin's settings say where its kinds show — Profile.resources is gone."""
def test_workers_and_cwd_globs_per_plugin(self):
reg = registry(scope={"workers": [MAC], "cwdGlobs": [WORK_CWD]})
self.assertEqual(reg.scoped_kinds(MAC, ORUS_CWD), {"linear", "linear-project", "pr", "slack"})
self.assertEqual(reg.scoped_kinds(MAC, ORUS_CWD + "/.claude/worktrees/x"),
{"linear", "linear-project", "pr", "slack"})
self.assertEqual(reg.scoped_kinds(MAC, ORUS_CWD + "-pr-1"), {"linear", "linear-project", "pr", "slack"})
self.assertEqual(reg.scoped_kinds(None, ORUS_CWD), set()) # the lab
self.assertEqual(reg.scoped_kinds(MAC, "/home/gabrielvidal/projects/x"), set())
def test_empty_scope_matches_every_conversation(self):
self.assertEqual(REG.scoped_kinds(None, "/anywhere"), {"linear", "linear-project", "pr", "slack"})
self.assertEqual(REG.scoped_kinds("local", None), {"linear", "linear-project", "pr", "slack"})
def test_one_plugin_scoped_the_other_not(self):
store = FakePluginStore()
store.configure("slack", workers=["local"])
reg = plugin_resources.registry(types.SimpleNamespace(plugin_store=store))
self.assertEqual(reg.scoped_kinds(MAC, "/x"), {"linear", "linear-project", "pr"})
self.assertEqual(reg.scoped_kinds(None, "/x"), {"linear", "linear-project", "pr", "slack"})
def test_settings_as_text(self):
# Settings → a `list` field may arrive as text lines.
reg = registry(scope={"workers": "a8ed565255ed\nlocal", "cwdGlobs": "~/work/*"})
self.assertEqual(reg.scoped_kinds("local", __import__("os").path.expanduser("~/work/x")),
{"linear", "linear-project", "pr", "slack"})
def test_notif_pills_per_plugin(self):
store = FakePluginStore()
reg = plugin_resources.registry(types.SimpleNamespace(plugin_store=store))
self.assertFalse(reg.notif_pills(MAC, ORUS_CWD))
store.configure("linear", notifPills=True, workers=[MAC])
reg = plugin_resources.registry(types.SimpleNamespace(plugin_store=store))
self.assertTrue(reg.notif_pills(MAC, ORUS_CWD))
self.assertFalse(reg.notif_pills(None, ORUS_CWD))
def test_conversation_out_of_every_scope_shows_only_what_it_recorded(self):
reg = registry(scope={"workers": [MAC]})
deps = types.SimpleNamespace(
plugin_store=FakePluginStore(scope={"workers": [MAC]}),
store=types.SimpleNamespace(summaries_version=1, all_summaries=lambda: []),
meta_store=types.SimpleNamespace(version=1, items=lambda: []))
summary = {"resourcesAuto": [{"kind": "url", "id": "x", "label": "x",
"url": "https://github.com/o/r/pull/1"}], "cwd": "/x"}
self.assertEqual(rv.for_conversation(deps, summary, {}, None), ([], None))
rs, _ = rv.for_conversation(deps, summary, {"resources": [
{"kind": "branch", "id": "feat-x", "source": "agent"}]}, None)
self.assertEqual([r["kind"] for r in rs], ["branch"])
rs, _ = rv.for_conversation(deps, summary, {}, MAC)
self.assertEqual([r["id"] for r in rs], ["o/r#1"])
del reg
class TestView(unittest.TestCase):
def view(self, auto, recorded=(), hidden=(), c=None, scoped=None):
c = c or ctx()
return rv.merge(list(auto), list(recorded), list(hidden), c,
c.registry.kinds if scoped is None else frozenset(scoped), True)
def test_bare_keys_need_a_known_team(self):
rs = self.view([{"kind": "linear", "id": "ISO-8601", "bare": True, "team": "ISO"},
{"kind": "linear", "id": "INS-2175", "bare": True, "team": "INS"}])
self.assertEqual([r["id"] for r in rs], ["INS-2175"])
self.assertEqual(rs[0]["url"], "https://linear.app/orus/issue/INS-2175")
def test_repo_less_pr_and_slack_label(self):
rs = self.view([{"kind": "pr", "id": "#13722", "number": 13722, "firstTurn": True},
{"kind": "slack", "id": "C02GDHPF1RR/1791878511.818179",
"channel": "C02GDHPF1RR", "ts": "1791878511.818179"}],
c=ctx(channels={"C02GDHPF1RR": "product-requests"}))
self.assertEqual(rs[0]["id"], "orus-tech/orus-monorepo#13722")
self.assertEqual(rs[0]["label"], "#13722")
self.assertTrue(rs[0]["primary"])
self.assertEqual(rs[1]["label"], "#product-requests")
self.assertEqual(rs[1]["url"], "https://orus-insurance.slack.com/archives/"
"C02GDHPF1RR/p1791878511818179")
# the plugin's own channelMap wins over a learned name
reg = registry(settings={"slack": {"slackWorkspace": "orus-insurance",
"channelMap": "C02GDHPF1RR=prod-req"}})
rs = self.view(rs, c=ctx(reg=reg, channels={"C02GDHPF1RR": "product-requests"}))
self.assertEqual([r["label"] for r in rs if r["kind"] == "slack"], ["#prod-req"])
def test_recorded_primary_scope_hidden(self):
auto = [{"kind": "pr", "id": "orus-tech/orus-monorepo#1", "firstTurn": True,
"source": "auto"},
{"kind": "branch", "id": "feat-x", "source": "auto"},
{"kind": "url", "id": "abc", "url": "https://x.io/a", "label": "x.io/a"}]
rec = [{"kind": "linear", "id": "INS-1", "title": "Thing", "primary": True,
"source": "agent"}]
rs = self.view(auto, rec, hidden=["url:abc"])
self.assertEqual([r["kind"] for r in rs], ["linear", "pr", "branch"])
self.assertTrue(rs[0]["primary"])
# github out of scope: its PR goes, what the session recorded stays
rs = self.view(auto, rec, scoped={"linear", "slack"})
self.assertEqual([r["kind"] for r in rs], ["linear", "branch", "url"])
def test_issue_from_a_prs_head_branch_across_plugins(self):
c = ctx(xrefs={PR + "13722": ["ins-2229-separate-tenant"]})
rs = self.view([{"kind": "pr", "id": "orus-tech/orus-monorepo#13722", "firstTurn": True,
"state": "merged"}], c=c)
self.assertEqual([(r["kind"], r.get("state")) for r in rs],
[("linear", None), ("pr", "merged")])
self.assertTrue(rs[0]["primary"] and rs[0]["firstTurn"])
self.assertEqual(rs[0]["title"], "Separate tenant") # the branch's slug
self.assertEqual(rv.title_for(rs, "AI title", REG), "INS-2229 · Separate tenant · #13722")
# …and only while linear is in scope
rs = self.view([{"kind": "pr", "id": "orus-tech/orus-monorepo#13722", "firstTurn": True}],
c=c, scoped={"pr"})
self.assertEqual([r["kind"] for r in rs], ["pr"])
def test_notion_is_a_plain_link(self):
rs = self.view([{"kind": "url", "id": "n1", "label": "app.notion.com/p/orus-team/x",
"url": "https://app.notion.com/p/orus-team/Ouverture-3cf0ab24de31814bb058d2aa9d124dac"}])
self.assertEqual([r["kind"] for r in rs], ["url"])
def test_vendor_links_the_plugin_ignores(self):
rs = self.view([{"kind": "url", "id": "g1", "label": "github.com/o/r/blob/x",
"url": "https://github.com/o/r/blob/main/x.py"},
{"kind": "url", "id": "s1", "label": "x.slack.com/team",
"url": "https://orus-insurance.slack.com/team/U1"}])
self.assertEqual(rs, [])
# without the plugins they are plain links again
rs = self.view([{"kind": "url", "id": "g1", "label": "github.com/o/r/blob/x",
"url": "https://github.com/o/r/blob/main/x.py"}],
c=ctx(reg=registry(plugins=("linear",))))
self.assertEqual([r["kind"] for r in rs], ["url"])
class TestTitles(unittest.TestCase):
def t(self, rs, fallback="Orus Insurance Slack thread", reg=REG):
return rv.title_for(rs, fallback, reg)
def test_formats(self):
lin = {"kind": "linear", "id": "INS-2229", "label": "INS-2229", "title": "Separate tenant",
"firstTurn": True}
pr = {"kind": "pr", "id": "o/r#13722", "label": "#13722", "title": "feat(mrp): x",
"firstTurn": True}
sl = {"kind": "slack", "id": "C02GDHPF1RR/1791878511.818179", "label": "#product-requests",
"firstTurn": True}
self.assertEqual(self.t([lin, pr]), "INS-2229 · Separate tenant · #13722")
self.assertEqual(self.t([pr, lin]), "INS-2229 · Separate tenant · #13722") # by priority
self.assertEqual(self.t([lin]), "INS-2229 · Separate tenant")
self.assertEqual(self.t([lin, sl]), "INS-2229 · Separate tenant") # slack is no shade
self.assertEqual(self.t([pr]), "#13722 · feat(mrp): x")
self.assertEqual(self.t([pr, sl]), "#13722 · feat(mrp): x")
self.assertEqual(self.t([sl]), "#product-requests · Orus Insurance Slack thread")
self.assertIsNone(self.t([{**sl, "id": "D0C1NBMQ9GS/1791973101.789639", "label": "DM"}]))
self.assertIsNone(self.t([{**lin, "firstTurn": False}]))
# a primary the agent recorded counts as the first turn's
self.assertEqual(self.t([{**lin, "firstTurn": False, "primary": True, "source": "agent"},
pr]), "INS-2229 · Separate tenant · #13722")
# core kinds alone make no title; no plugin, no title
self.assertIsNone(self.t([{"kind": "branch", "id": "x", "label": "x", "firstTurn": True}]))
self.assertIsNone(self.t([lin, pr], reg=plugin_resources.EMPTY))
def test_ai_title_that_already_names_it_is_kept(self):
pr = {"kind": "pr", "id": "o/r#13749", "label": "#13749", "firstTurn": True}
self.assertIsNone(self.t([pr], "Orus-monorepo PR 13749 vs template"))
self.assertEqual(self.t([pr], "Template diff"), "#13749 · Template diff")
class TestRecording(unittest.TestCase):
def test_clean_recorded(self):
out = rv.clean_recorded([{"kind": "pr", "id": "o/r#1", "url": "https://github.com/o/r/pull/1",
"title": "x" * 400, "primary": 1, "account": "personal"}], reg=REG)
self.assertEqual(len(out[0]["title"]), 300)
self.assertEqual(out[0]["source"], "agent")
self.assertTrue(out[0]["primary"])
self.assertNotIn("account", out[0])
for bad in ([{"kind": "jira", "id": "x"}], [{"kind": "pr", "id": ""}],
[{"kind": "url", "id": "x", "url": "javascript:alert(1)"}], "nope",
[{"kind": "Bad Kind!", "id": "x"}], [{"ref": "two words"}]):
with self.assertRaises(ValueError, msg=bad):
rv.clean_recorded(bad, reg=REG)
def test_active_plugin_kinds_only(self):
with self.assertRaises(ValueError):
rv.clean_recorded([{"kind": "pr", "id": "o/r#1"}]) # no plugin active
self.assertEqual(rv.clean_recorded([{"kind": "branch", "id": "x"}])[0]["kind"], "branch")
def test_raw_links_and_refs_are_read_hub_side(self):
out = rv.clean_recorded([
{"kind": "url", "id": "h", "url": "https://github.com/o/r/pull/9/files"},
{"ref": "INS-2229", "label": "INS-2229"},
{"ref": "#13722"},
{"ref": "https://linear.app/orus/issue/INS-7/slug", "kind": "linear"},
{"ref": "feature/foo"}], reg=REG)
self.assertEqual([f"{r['kind']}:{r['id']}" for r in out],
["pr:o/r#9", "linear:INS-2229", "pr:orus-tech/orus-monorepo#13722",
"linear:INS-7", "branch:feature/foo"])
self.assertEqual(out[0]["url"], "https://github.com/o/r/pull/9")
def test_clean_remove(self):
self.assertEqual(rv.clean_remove(["pr:o/r#1", "INS-2", "https://github.com/o/r/pull/3"], REG),
["pr:o/r#1", "linear:INS-2", "pr:o/r#3",
"url:" + __import__("resource_urls").parse_url(
"https://github.com/o/r/pull/3")["id"]])
with self.assertRaises(ValueError):
rv.clean_remove(["two words"], REG)
if __name__ == "__main__":
unittest.main()
class TestViewExtras(unittest.TestCase):
def test_repeated_channel_threads_get_their_date(self):
rs = rv.merge([{"kind": "slack", "id": "C03AQQ21WFL/1791878511.818179"},
{"kind": "slack", "id": "C03AQQ21WFL/1789140166.348379"},
{"kind": "slack", "id": "C02GDHPF1RR/1791878511.818179"}],
[], [], ctx(channels={"C03AQQ21WFL": "tech-alerts",
"C02GDHPF1RR": "product-requests"}),
REG.kinds, True)
labels = sorted(r["label"] for r in rs)
self.assertEqual(labels, ["#product-requests", "#tech-alerts · 11 Sep",
"#tech-alerts · 13 Oct"])
def test_a_title_found_elsewhere_beats_a_slug(self):
c = ctx()
c.titles = {"linear:INS-1961": "Add DROM-COM pricing to RCPH"}
rs = rv.merge([{"kind": "linear", "id": "INS-1961", "slugTitle": "Drom com rcph review",
"firstTurn": True}], [], [], c, REG.kinds, True)
self.assertEqual(rs[0]["title"], "Add DROM-COM pricing to RCPH")