RynthSuite/Tools/RynthOracle.Tests/WikiFeedTests.cs

153 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using RynthCore.Plugin.RynthOracle;
using RynthCore.Plugin.RynthOracle.Data;
using RynthCore.Plugin.Shared;
namespace RynthOracle.Tests
{
/// <summary>
/// The wiki page reader (Plugins/Shared/QuestWikiText.cs) and the AC Community Wiki feed's pure parts
/// (2026-11-27). The pages here are made up in the wiki's template style; no wiki text is copied in.
/// The feed is off by default and never fetches in these tests.
/// </summary>
internal static partial class Program
{
private const string TestWiki = "https://wiki.example/wiki/";
/// <summary>A made-up quest page: summary box, overview, numbered steps with stamps and items, a stamp listed apart, rewards.</summary>
internal const string SampleQuestPage = @"{{Quest Summary
| Level = 50+
| Recommended Level = 80+
| Quest Type = Solo
| Starts With = [[Sample Questgiver]]
| Starts At = [[Sampletown]]
| Repeat Timer = 20 Hours
}}
<!-- an editor's note that isn't shown -->
== Overview ==
Sample Questgiver wants a '''fang''' from the [[Sample Beast|beast]] in the hills.<ref>a footnote</ref>
== Walkthrough ==
# Talk to [[Sample Questgiver]] in Sampletown (12.3N, 45.6E).
#* {{stamped|TestErrandStarted}}
# Travel to the hills at 20.1s 30.2w and kill the Sample Beast to loot a {{Itemlink|Sample Fang}}.
#* It hits hard.
# Hand the {{Itemlink|Sample Fang}} to [[Sample Questgiver]].
== QB Stamps ==
{{stamped|TestErrandFang}} {{stamped|TestErrandDone on first completion}}
== Rewards ==
{{Rewards Table
| Quest Task = Hand in the fang
| Item Reward = * {{Itemlink|Sample Trinket}} (x2)
| XP Reward = 1,000,000
}}
[[Category:Quests]]
[[Category:Sample Quests]]";
private const string SampleKillPage = @"{{Quest Summary | Level = 30+ | Kill Task = Yes | Repeat Timer = Non Repeatable | Starts With = [[Sample Hunter]] }}
== Walkthrough ==
* Kill 50 [[Sample Beast]]s for the hunter. {{stamped|KillTaskSampleBeasts_0101 on first completion}}
* Return to the hunter.";
private const string SampleContractPage = @"{{Intro | Related Quests = [[Test Daily Errand]], [[Test Story Quest|the story]] }}
{{Item | Name = Contract for Test Daily Errand | Description = Starts the errand again. | Recommended Level = 50 }}
== Notes ==
* Purchased from [[Sample Barkeep]] in the Sampletown inn.
[[Category:Contracts]]";
private static void WikiFeed()
{
// ── The page reader ──
Check(QuestWikiText.IsQuestPage(SampleQuestPage) && !QuestWikiText.IsQuestPage("just text"), "a quest page has the summary box");
GuideQuest q = QuestWikiText.ParseQuest("Test Daily Errand", SampleQuestPage, 77, TestWiki);
Check(q.Url == "https://wiki.example/wiki/Test_Daily_Errand" && q.Rev == 77, "the page's address and revision: " + q.Url);
Check(q.Level == "50+" && q.RecommendedLevel == "80+" && q.Type == "Solo" && q.StartsWith == "Sample Questgiver" && q.StartsAt == "Sampletown",
"the summary box: level, type, where it starts");
Check(q.RepeatSec == 20 * 3600 && q.IsDaily && !q.IsOneTime, "a 20 hour timer is a daily");
Check(q.Overview == "Sample Questgiver wants a fang from the beast in the hills.", "the overview, links and markup cleaned: " + q.Overview);
Check(q.Steps.Count == 3, $"three numbered steps ({q.Steps.Count})");
Check(q.Steps[0].Coords.Count == 1 && q.Steps[0].Coords[0] == "12.3N, 45.6E" && q.Steps[0].Stamps[0] == "TestErrandStarted",
"step 1: its coordinates and stamp");
Check(q.Steps[1].Coords[0] == "20.1S, 30.2W" && q.Steps[1].Notes.Count == 1 && q.Steps[1].Items[0] == "Sample Fang",
"step 2: lower-case coordinates, its tip and its item");
Check(q.Steps[1].Stamps.Contains("TestErrandFang"), "a stamp listed apart goes with the step that names it");
Check(q.Steps[2].Stamps.Contains("TestErrandDone"), "the last stamp left goes to the last stampless step, cut to its flag");
Check(q.ExtraStamps.Count == 0 && q.AllStamps().Count == 3, "every stamp tied to a step");
Check(q.Loot.Count == 1 && q.Loot[0] == "Sample Fang", "loot: the item a kill step names, not the hand-in");
Check(q.Rewards.Count == 3 && q.Rewards[0] == "For Hand in the fang:" && q.Rewards[1] == "Sample Trinket (x2)" && q.Rewards[2] == "XP: 1,000,000",
"the rewards table: " + string.Join(" | ", q.Rewards));
Check(q.Categories.Contains("Sample Quests"), "the categories");
GuideQuest k = QuestWikiText.ParseQuest("Sample Beast Kill Task", SampleKillPage, 5, TestWiki);
Check(k.IsKillTaskPage && k.KillTarget == 50 && k.KillCreatureKey == "samplebeast", "a kill task: the box, the count, the creature");
Check(k.RepeatSec == 0 && k.IsOneTime, "\"Non Repeatable\" is one-time");
Check(k.Steps.Count == 2 && k.Steps[0].Stamps[0] == "KillTaskSampleBeasts_0101", "bullet steps, the counter stamp cut to its flag");
Check(QuestWikiText.IsContractPage("Contract for Test Daily Errand", SampleContractPage), "a contract page");
GuideContract c = QuestWikiText.ParseContract("Contract for Test Daily Errand", SampleContractPage, 9, TestWiki);
Check(c.Name == "Contract for Test Daily Errand" && c.Level == "50" && c.Description == "Starts the errand again.", "the contract's item box");
Check(c.Quests.Count == 2 && c.Quests[0] == "Test Daily Errand" && c.Quests[1] == "Test Story Quest", "the quests it is for");
Check(c.Npc == "Sample Barkeep" && c.Where.StartsWith("Purchased from Sample Barkeep"), "where it comes from: " + c.Where);
// Paragraph walkthroughs, repeat timers.
GuideQuest para = QuestWikiText.ParseQuest("P", "{{Quest Summary}}\n== Walkthrough ==\nFirst go here.\nStill the first.\n\nThen go there 1.0N 2.0E.", 0, TestWiki);
Check(para.Steps.Count == 2 && para.Steps[1].Coords[0] == "1.0N, 2.0E", "paragraphs are steps");
Check(QuestWikiText.RepeatSeconds("7 days") == 7 * 86400 && QuestWikiText.RepeatSeconds("3d 12h") == 3 * 86400 + 12 * 3600
&& QuestWikiText.RepeatSeconds("Weekly") == 7 * 86400 && QuestWikiText.RepeatSeconds("None") == 0
&& QuestWikiText.RepeatSeconds("whenever") == -1, "repeat timers");
Check(QuestWikiText.Clean("[[File:x.png|thumb|a [[link]]]] [[Foo|bar]] {{Itemlink|Baz}} &amp; <br/>end") == "bar Baz & end", "markup cleaned");
// ── The API's answers ──
var members = QuestWikiText.Members(
"{\"continue\":{\"cmcontinue\":\"page|X\",\"continue\":\"-||\"},\"query\":{\"categorymembers\":[{\"ns\":0,\"title\":\"A\"},{\"ns\":14,\"title\":\"Category:B\"},{\"ns\":6,\"title\":\"File:C\"}]}}",
"categorymembers");
Check(members.Pages.Count == 1 && members.Pages[0] == "A" && members.SubCats.Count == 1 && members.Continue.Count == 2,
"category members: pages, sub-categories, continue");
Dictionary<string, long> revs = QuestWikiText.LastRevisions("{\"query\":{\"pages\":[{\"title\":\"A\",\"lastrevid\":12},{\"title\":\"Gone\",\"missing\":true}]}}");
Check(revs.Count == 1 && revs["A"] == 12, "page revisions, missing pages left out");
var contents = QuestWikiText.Contents("{\"query\":{\"pages\":[{\"title\":\"A\",\"revisions\":[{\"revid\":12,\"slots\":{\"main\":{\"content\":\"hello\"}}}]}]}}");
Check(contents.Count == 1 && contents[0].Rev == 12 && contents[0].Text == "hello", "page contents");
Check(QuestWikiText.Members("not json", "x").Pages.Count == 0 && QuestWikiText.LastRevisions("[").Count == 0, "junk answers are empty");
var titles = new List<string>();
for (int i = 0; i < 120; i++) titles.Add("T" + i);
Check(QuestWikiText.Batches(titles).Count == 3, "titles in batches of 50");
string url = QuestWikiText.ApiUrl(CommunityWikiFeed.Api, ("action", "query"), ("titles", "A B|C&D"));
Check(url.StartsWith("https://asheron.fandom.com/api.php?format=json&formatversion=2&action=query&titles=A%20B%7CC%26D"), "the API address, values escaped: " + url);
// ── The feed's pure parts ──
Check(CommunityWikiFeed.RankOf("Category:Society Quests: Knight Rank") == "Knight" && CommunityWikiFeed.RankOf("Category:Joining a Society Quests") == "Joining"
&& CommunityWikiFeed.RankOf("Category:Other") == "", "society ranks from category names");
Check(CommunityWikiFeed.SkipCategory("Category:Quest Items") && CommunityWikiFeed.SkipCategory("Category:Quest NPCs")
&& !CommunityWikiFeed.SkipCategory("Category:Society Quests"), "item and NPC lists aren't followed");
var old = new QuestGuide { Reader = QuestWikiText.ReaderVersion, UpdatedUtc = DateTime.UtcNow.AddHours(-1) };
old.Quests.Add(new GuideQuest { Title = "Same", Rev = 1 });
old.Quests.Add(new GuideQuest { Title = "Edited", Rev = 1 });
old.Quests.Add(new GuideQuest { Title = "Dropped", Rev = 1 });
var listed = new Dictionary<string, long> { ["Same"] = 1, ["Edited"] = 2, ["New"] = 3 };
List<string> toRead = CommunityWikiFeed.PagesToRead(old, listed);
Check(toRead.Count == 2 && toRead.Contains("Edited") && toRead.Contains("New"), "only new and edited pages are read");
Check(CommunityWikiFeed.PagesToRead(new QuestGuide { Reader = 0, Quests = { new GuideQuest { Title = "Same", Rev = 1 } } }, new Dictionary<string, long> { ["Same"] = 1 }).Count == 1,
"a guide from an older reader is read again whole");
var fresh = new Dictionary<string, GuideQuest> { ["Edited"] = new GuideQuest { Title = "Edited", Rev = 2 }, ["New"] = new GuideQuest { Title = "New", Rev = 3 } };
QuestGuide merged = CommunityWikiFeed.Merge(old, listed, fresh, new Dictionary<string, GuideContract>(), DateTime.UtcNow);
Check(merged.Quests.Count == 3 && merged.Find("Edited")!.Rev == 2 && merged.Find("Same") != null && merged.Find("Dropped") == null
&& merged.Source == CommunityWikiFeed.Host, "the merge: fresh pages, kept pages, dropped pages gone");
Check(!CommunityWikiFeed.Due(old, DateTime.UtcNow) && CommunityWikiFeed.Due(old, DateTime.UtcNow.AddDays(1)) && CommunityWikiFeed.Due(new QuestGuide(), DateTime.UtcNow),
"read again once a day old (or when there is none)");
// ── Off by default; it says what it reads and from where ──
Check(!new OracleSettings().WikiFeedOn, "the feed is off by default");
var feed = new CommunityWikiFeed(System.IO.Path.Combine(System.IO.Path.GetTempPath(), "RynthOracleTests-" + Guid.NewGuid().ToString("N") + "-feed.json"));
Check(!feed.Start(on: false) && !feed.Running && !feed.LoginRead(on: false, daily: true, DateTime.UtcNow), "off: no read starts");
Check(CommunityWikiFeed.WhatItReads.Contains("https://asheron.fandom.com/api.php") && CommunityWikiFeed.WhatItReads.Contains("no login"),
"the disclosure names the address");
OracleContext ctx = Context();
ctx.Settings.WikiFeedOn = false;
var (readOk, readText) = ctx.ReadWikiNow();
Check(!readOk && readText.Contains("off") && readText.Contains("asheron.fandom.com/api.php") && !ctx.Wiki.Running, "Read now with the feed off fetches nothing and says why");
Check(ctx.SetWikiFeed(false).Text.Contains("already off"), "turning it off when off says so");
Check(OracleCommand.Parse("wiki on") is { Verb: OracleVerb.Wiki, Word: "on" } && OracleCommand.Parse("wiki") is { Verb: OracleVerb.Wiki, Word: "status" }
&& OracleCommand.Parse("wiki read") is { Verb: OracleVerb.Wiki, Word: "read" }, "/ro wiki words");
}
}
}