import csv from io import StringIO from pathlib import Path import sqlite3 import unittest from unittest.mock import patch from discord_trade_parser import (Message, clean_offer_items, clean_request_fields, export_csv, normalize_request_segments, parse_listing, parse_listings, repair_glued_reposts, repair_misfiled_lf_inventory, repair_misfiled_inventory, repair_misfiled_mlf_inventory, repair_standalone_lf_inventory, repair_inline_side_cues, recover_explicit_my_offers, recover_partial_proposal_offers, repair_confirmed_long_lf_inventory, repair_confirmed_proposal_sides, repair_alucard_lf_inventory) class OpenStringIO(StringIO): def close(self): # Let the test inspect the CSV after export_csv closes its context. pass class TradeItemCleanupTests(unittest.TestCase): def test_keeps_stats_broad_options_and_owned_best_offer(self): offer = ("SSS DMG 23.1% | All Gamepasses | ALL SKINS | " "BEST OFFER:Aizen Evo FULL SSS | HIGH OFFER Shiny Homura Evo") self.assertEqual(clean_offer_items(offer), offer) def test_removes_chat_and_ui_fragments_without_damaging_stats(self): offer = ("[ ] | Shiny Elf Mage Evo {SS | A- | S+} | " "Dark Mage Evo S-/S-/S- | no response = nty | 1 • 19:36 | " "Shiny Homura Evo S+ | S | SSS | COME WITH OFFER | " "Please Use My And Yours | Total Value:885 | -1x Gilgamesh Evo") self.assertEqual( clean_offer_items(offer), "Shiny Elf Mage Evo {SS / A- / S+} | Dark Mage Evo S-/S-/S- | " "Shiny Homura Evo S+ / S / SSS | 1x Gilgamesh Evo", ) def test_parse_listing_rejects_channel_only_message(self): self.assertIsNone(parse_listing(Message("a", "Trading: channel for Server Boosters"))) listing = parse_listing(Message("b", "Trading: Aizen Evo | no response = nty\nLF: Homura Evo")) self.assertEqual(listing.offer, "Aizen Evo") def test_request_cleanup_moves_explicit_labels_and_keeps_trade_requests(self): looking, lf, mlf, nlf = clean_request_fields( "PRE | OFFERS | SSS DMG 23% | (MLF: ALL FOR Dio Evo) | (NLF bundles 1-3) | " "No reply = nty | NO WANT GP/ clown offers", "Shiny Hunter", "Skins") self.assertEqual(lf, "PRE | OFFERS | SSS DMG 23%") self.assertEqual(mlf, "Shiny Hunter | ALL FOR Dio Evo") self.assertEqual(nlf, "Skins | bundles 1-3 | GP/ clown offers") self.assertEqual(looking, "PRE | OFFERS | SSS DMG 23% | MLF: Shiny Hunter | ALL FOR Dio Evo") def test_request_cleanup_recovers_missing_mlf_and_removes_ui(self): looking, lf, mlf, nlf = clean_request_fields( "OFFERS | #【】 | My 4 units for one SSS SPA#", "Offers | Nail relic", "Ignore = NTY | Battlepass", "MLF: SSS Stats | Offers | Nail relic") self.assertEqual(lf, "OFFERS | My 4 units for one SSS SPA") self.assertEqual(mlf, "Offers | Nail relic") self.assertEqual(nlf, "Battlepass") self.assertIn("MLF: Offers | Nail relic", looking) _, lf, mlf, _ = clean_request_fields("", "Offers | Nail relic", "", "MLF: SSS Stats | Offers | Nail relic") self.assertEqual((lf, mlf), ("", "SSS Stats | Offers | Nail relic")) self.assertEqual(clean_request_fields("", "", "Rerolls | Ignoring Clowns/Lowballs")[3], "Rerolls | Lowballs") _, lf, _, nlf = clean_request_fields( "Offers", "", "very low value offers; low demand; Only SSS stats (for Homura Evo)") self.assertEqual(lf, "Offers | Only SSS stats (for Homura Evo)") self.assertEqual(nlf, "very low value offers; low demand") self.assertEqual(clean_request_fields( "GOOD OFFERSTrading | Aizen Evo | Homura Evo | GOOD OFFERS", "", "")[1], "GOOD OFFERS") self.assertEqual(clean_request_fields("Keyword:", "", "")[1], "") self.assertEqual(normalize_request_segments("SHINIES:Cid,Dark Mage"), "Shiny Cid Evo,Shiny Dark Mage Evo") def test_glued_trade_label_does_not_put_inventory_in_mlf(self): message = Message("a", "Trade\nShiny Inhuman Evo\nLF offers " "MLF all for Alucard Evo i can add more unitstrade\n" "Shiny Inhuman Evo\nLF offers") listing = parse_listing(message) self.assertEqual(listing.offer, "Shiny Inhuman Evo") self.assertEqual(listing.mlf, "all for Alucard Evo i can add more units") _, _, mlf, _ = clean_request_fields( "offers", "all for Alucard Evo i can add more unitstrade | Shiny Inhuman Evo | " "all for Alucard Evo i can add more units", "") self.assertEqual(mlf, "all for Alucard Evo i can add more units") def test_friend_inventory_is_a_separate_offer(self): message = Message("a", "Trading: Aizen Evo\nMLF: Shiny Chuuni Evo\n" "My Friend is\nShiny Buddha Evo") listings = parse_listings(message) self.assertEqual(len(listings), 2) self.assertEqual(listings[0].mlf, "Shiny Chuuni Evo") self.assertEqual(listings[1].offer, "Shiny Buddha Evo") self.assertEqual(listings[1].looking_for, "") def test_confirmed_misfiled_nlf_inventory_becomes_offer(self): offer, lf, nlf = repair_misfiled_inventory( "Shiny Homura Evo", "OVERPAYS", "Shiny Gilgamesh Evo | x1 FULL SSS Aizen Evo 23%/10.7%/12.1% | " "x1 FULL SSS Homura Evo (LF 6 SSS DMG / Aizen) | " "BUNDLES/RR/ASSASIN UPD UNITS/GAMEPASS | WILL READ ALL DMS ( NO RESPONSE = AFK)") self.assertIn("Shiny Gilgamesh Evo", offer) self.assertIn("x1 FULL SSS Homura Evo", offer) self.assertIn("6 SSS DMG / Aizen", lf) self.assertEqual(nlf, "") def test_confirmed_misfiled_mlf_inventory_becomes_offer(self): offer, lf, mlf = repair_misfiled_mlf_inventory( "Shiny Homura Evo", "OVERPAYS", "OVERPAY | NORMAL UNITS | x1 Aizen Evo (FULL S) | " "x6 Homura Evo ALL GOOD STATS (LF Aizen Evo/ FULL SSS Homura/ FOR MY 2 Homura)") self.assertIn("x1 Aizen Evo (FULL S)", offer) self.assertIn("x6 Homura Evo ALL GOOD STATS", offer) self.assertIn("Aizen Evo/ FULL SSS Homura/ FOR MY 2 Homura", lf) self.assertEqual(mlf, "OVERPAY") def test_glued_reposts_keep_requests_and_recover_offers(self): offer, lf, mlf = repair_glued_reposts( "Nagumo Evo | Homura Evo", "Battlepass | Homura Evo, Battlepass and addTrade | " "Nagumo Evo | Chuuni Evo too | Homura Evo, Battlepass and add", "") self.assertEqual(lf, "Battlepass | Homura Evo, Battlepass and add") self.assertIn("Chuuni Evo", offer) self.assertEqual(mlf, "") offer, lf, mlf = repair_glued_reposts( "Cid Evo", "", "UPGRADEtrading | Buddha Evo with SSS spa 11.7 | " "Cid Evo | UPGRADE | Buddha Evo with SSS spa 11.7") self.assertIn("Buddha Evo with SSS spa 11.7", offer) self.assertEqual((lf, mlf), ("", "UPGRADE")) def test_confirmed_lf_inventory_moves_to_offer(self): cases = [ ("2X SSS DMG FOR MY Shiny Poseidon Evo | Evo MYTIC's | " "6x Dark Mage Evo | SKINS | 2x ERWIN DELINQUENT | " "Shiny POSEI/Chuuni Evo AND ADDS FOR MY ROGER Evo", "6x Dark Mage Evo", "2X SSS DMG FOR MY Shiny Poseidon Evo | " "Shiny POSEI/Chuuni Evo AND ADDS FOR MY ROGER Evo"), ("S stat Fodder Units | 4 Deer Tan Evo | 2 Dark Mage Unevo | " "S stat Fodder Units | 2 Merlin Evo | " "Vip and Shiny hunter(Must be both in 1 trade) for Emilia Evo", "4 Deer Tan Evo", "Vip and Shiny hunter(Must be both in 1 trade) for Emilia Evo"), ("both Homura Evo for Aizen Evo | Shiny UNITS | Shiny esdeath Evo | " "NON-Shiny UNITS | Violentine Evo | " "s tier upgrade for my Shiny units", "Shiny esdeath Evo", "both Homura Evo for Aizen Evo | " "s tier upgrade for my Shiny units"), ] for lf, moved, expected_lf in cases: with self.subTest(moved=moved): offer, result_lf = repair_misfiled_lf_inventory("Aizen Evo", lf) self.assertIn(moved, offer) self.assertEqual(result_lf, expected_lf) def test_inline_side_cues_keep_owned_items_and_move_requests(self): offer, lf, nlf = repair_inline_side_cues( "Shiny Mist Ninja Evo LOOKIN FOR FAIR TRADE NO SCAM | " "Homura Evo sss rng ( for 4 sss dmg/spa) | " "Battlepass (WILL NOT TRADE FOR Dark Mage Evo + ADDS)", "", "") self.assertIn("Shiny Mist Ninja Evo", offer) self.assertIn("Homura Evo sss rng", offer) self.assertIn("Battlepass", offer) self.assertEqual(lf, "FAIR TRADE | 4 sss dmg/spa") self.assertEqual(nlf, "Dark Mage Evo + ADDS") offer, lf, _ = repair_inline_side_cues( "Dark Mage Evo | olf Homura Evo/overpays/Chuuni Evo item(please chooms ) | " "UPGRADES/", "OFFERS", "") self.assertEqual(offer, "Dark Mage Evo") self.assertEqual(lf, "OFFERS | Homura Evo/overpays/Chuuni Evo item | UPGRADES") offer, lf, _ = repair_inline_side_cues( "6x Dark Mage Evo (ALL GOOD STATS) [2x FOR ANY SSS] | " "100+ Fodder Units (9 for 10rr)", "", "") self.assertEqual(offer, "6x Dark Mage Evo (ALL GOOD STATS) | 100+ Fodder Units") self.assertEqual(lf, "ANY SSS for 2 Dark Mage Evo | 10 RR for 9 Fodder Units") def test_owned_standalone_lf_list_moves_to_offer(self): offer, lf = repair_standalone_lf_inventory( "Homura Evo SS/SSS/SSS", "Alucard Evo | Shiny Chuuni Evo | 4x SSS spa | Buddha Evo | " "Aizen Evo/UPGRADES", "my 4x SSS spa + Shiny Somber Evo for Alucard Evo | " "My Alucard Evo + Shiny Dark Mage Evo for Aizen Evo") self.assertIn("Alucard Evo", offer) self.assertIn("4x SSS spa", offer) self.assertIn("Shiny Dark Mage Evo", offer) self.assertEqual(lf, "Aizen Evo/UPGRADES") def test_my_proposal_recovers_missing_owned_variants(self): offer = recover_explicit_my_offers( "Shiny Boros Evo | Dark Mage Unevo", "My Boros Evo for Thorfinn Evo | my Dark Mage Evo for SSS DMG", "") self.assertIn("Boros Evo", offer) self.assertIn("Dark Mage Evo", offer) self.assertEqual(offer.count("Shiny Boros Evo"), 1) self.assertNotIn("Thorfinn Evo", offer) def test_partial_proposal_recovers_confirmed_offered_units(self): offer = recover_partial_proposal_offers( "Shiny Toad Sensei Evo", "Shiny Toad Sensei Evo and Nagumo Evo for Shiny Cid Evo", "") self.assertIn("Nagumo Evo", offer) offer = recover_partial_proposal_offers( "Shiny Nagumo Evo", "", "Chuuni Evo/Evo ITEM FOR Chuuni Evo AND Buddha Unevo OR OFFER") self.assertIn("Chuuni Evo | Evo ITEM", offer) def test_confirmed_proposal_directions(self): offer, lf, _ = repair_confirmed_proposal_sides( "Battlepass", "Shiny Gohan Evo / Gohan Evo / Shiny Android 21 Evo for bundles or RR", "") self.assertIn("Shiny Android 21 Evo", offer) self.assertEqual(lf, "bundles or RR") offer, _, _ = repair_confirmed_proposal_sides( "SSS DMG", "", "Shiny Chuuni Evo for Aizen Evo") self.assertIn("Shiny Chuuni Evo", offer) self.assertNotIn("Aizen Evo", offer) def test_confirmed_long_lf_inventory_moves_to_offer(self): offer, lf = repair_confirmed_long_lf_inventory( "Homura Evo", "Sss for Each | 3Dark Mage Evo | Sss for Each | Chuuni Evo | " "Sss | Boros Evo | Cid Evo | Sss Stats | " "my Homura Evo for Aizen Evo") self.assertIn("3Dark Mage Evo", offer) self.assertIn("Cid Evo", offer) self.assertEqual(lf, "SSS for each | SSS Stats | my Homura Evo for Aizen Evo") def test_alucard_list_preserves_wanted_adds(self): original = ("%29+ high sss dmg or Aizen Evo | HIGH SSS %14+/28+ | " "SSS DMG 21.6 | Shiny Chuuni Evo or some Dark Mage Evo for sss dmg or spa | " "SSS SPA 11.5 | SSS SPA Chuuni Evo | Shiny Killstreak Evo | " "Dark Mage Evo/Buddha Evo adds for my Shiny units | Elf Mage Evo | " "Inhuman Evo S+ spa | Dark Mage Unevo | offers Aizen Evo,Alucard Evo,high sss stats") offer, lf = repair_alucard_lf_inventory("Alucard Evo", original) self.assertIn("SSS SPA 11.5", offer) self.assertIn("Shiny Killstreak Evo", offer) self.assertIn("Elf Mage Evo", offer) self.assertIn("Dark Mage Evo/Buddha Evo adds", lf) self.assertNotIn("SSS SPA 11.5", lf) raw = original.replace("Aizen Evo", "aizen", 1).replace( "Shiny Chuuni Evo or some Dark Mage Evo", "shiny chunni or some dark mage") raw += " | Dm offers" raw_offer, raw_lf = repair_alucard_lf_inventory("Alucard Evo", raw) self.assertIn("SSS SPA 11.5", raw_offer) self.assertNotIn("Dm offers", raw_lf) def test_export_filters_old_database_rows(self): conn = sqlite3.connect(":memory:") conn.execute("""CREATE TABLE trades ( id INTEGER PRIMARY KEY, offer TEXT, looking_for TEXT, lf TEXT, mlf TEXT, nlf TEXT, author_hash TEXT, seen_count INTEGER, update_count INTEGER)""") conn.executemany( "INSERT INTO trades VALUES (?,?,?,?,?,?,?,?,?)", [(1, "channel for Server Boosters", "", "", "", "", "a", 1, 0), (2, "Aizen Evo | no response = nty", "Homura Evo", "Homura Evo", "", "", "b", 1, 0), (3, "ILLUSIONST (TRANSCENDED) FOR BUNDLE 2", "", "", "", "", "c", 1, 0), (4, "Homura Evo", "SSS Stats | Evo mythics", "SSS Stats | Evo mythics", "", "", "d", 1, 0)], ) stream = OpenStringIO(newline="") with patch.object(Path, "open", return_value=stream): count = export_csv(conn, Path("trades.csv"), detailed=True) stream.seek(0) rows = list(csv.DictReader(stream)) self.assertEqual(count, 3) self.assertEqual(rows[0]["offer"], "Aizen Evo") self.assertEqual(rows[0]["lf"], "Homura Evo") self.assertEqual(rows[1]["offer"], "Aizen Evo") self.assertEqual(rows[1]["lf"], "BUNDLE 2") self.assertEqual(rows[2]["lf"], "SSS Stats | Evo mythics") def test_export_recovers_one_friend_row_from_repeated_legacy_listings(self): conn = sqlite3.connect(":memory:") conn.execute("""CREATE TABLE trades ( id INTEGER PRIMARY KEY, offer TEXT, looking_for TEXT, lf TEXT, mlf TEXT, nlf TEXT, author_hash TEXT, seen_count INTEGER, update_count INTEGER, canon_offer TEXT)""") for row_id in (1, 2): conn.execute( "INSERT INTO trades VALUES (?,?,?,?,?,?,?,?,?,?)", (row_id, "Aizen Evo", "MLF: Shiny Chuuni Evo | My Friend is | Shiny Buddha Evo", "", "Shiny Chuuni Evo | My Friend is | Shiny Buddha Evo", "", "parent", 1, 0, "aizen evo"), ) stream = OpenStringIO(newline="") with patch.object(Path, "open", return_value=stream): count = export_csv(conn, Path("trades.csv"), detailed=True) stream.seek(0) rows = list(csv.DictReader(stream)) self.assertEqual(count, 3) self.assertEqual(sum(row["offer"] == "Shiny Buddha Evo" for row in rows), 1) self.assertTrue(all("My Friend is" not in row["mlf"] for row in rows)) def test_export_keeps_stat_condition_inside_recovered_offer(self): conn = sqlite3.connect(":memory:") conn.execute("""CREATE TABLE trades ( id INTEGER PRIMARY KEY, offer TEXT, looking_for TEXT, lf TEXT, mlf TEXT, nlf TEXT, author_hash TEXT, seen_count INTEGER, update_count INTEGER)""") request = ("2X SSS DMG FOR MY Shiny Poseidon Evo | Evo MYTIC's | " "6x Dark Mage Evo (ALL GOOD STATS) [2x FOR ANY SSS] | " "Shiny POSEI/Chuuni Evo AND ADDS FOR MY ROGER Evo") conn.execute("INSERT INTO trades VALUES (?,?,?,?,?,?,?,?,?)", (1, "Shiny Poseidon Evo", request, request, "", "", "a", 1, 0)) stream = OpenStringIO(newline="") with patch.object(Path, "open", return_value=stream): export_csv(conn, Path("trades.csv"), detailed=True) stream.seek(0) row = next(csv.DictReader(stream)) self.assertIn("[2x FOR ANY SSS]", row["offer"]) self.assertNotIn("ANY SSS]", row["lf"]) if __name__ == "__main__": unittest.main()