using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using NUnit.Framework;
using TKS.OutGame;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;
using UnityEngine.UI;
namespace TKS.Tests
{
///
/// 외형(코스튬 · 2026-11-20 개발요청_코스튬 §4) — 착용 · 벗기 · 저장 · 다른 영웅과 공용 보유 · 미보유 미리보기 → 적용 막힘 · 외형 칸 잠김 · 기준점 대체(관우 외) ·
/// 세이브 다시 불러오기 · 창 흐름 · 전투 레이어. 기대값은 표에서 읽는다(C45).
///
public class CostumeTests
{
static readonly DateTime T0 = new DateTime(2026, 11, 18, 3, 0, 0, DateTimeKind.Utc);
static OutGameData Db
{
get { return OutGameData.Default; }
}
[TearDown]
public void TearDown()
{
SaveStore.UseMemory(false);
}
static CostumeDb.Item First(string slot)
{
foreach (var it in Db.Costume.items)
if (it.slot == slot) return it;
return null;
}
[Test]
public void Tables_Import_SlotsItemsAnchors_SkinLocked()
{
var dir = Path.Combine(Application.dataPath, OutGameTables.Folder);
var texts = new string[OutGameTables.Files.Length];
for (int i = 0; i < texts.Length; i++) texts[i] = File.ReadAllText(Path.Combine(dir, OutGameTables.Files[i]), System.Text.Encoding.UTF8);
var r = OutGameTables.Parse(texts);
Assert.AreEqual(0, r.errors.Count, string.Join("\n", r.errors.ToArray()));
Assert.AreEqual(r.data.Costume.items.Count, Db.Costume.items.Count, "꾸미기 = 자산(TKS/Import Costume)");
Assert.AreEqual(r.data.Costume.slots.Count, Db.Costume.slots.Count);
var skin = Db.Costume.SlotOf("skin");
Assert.IsNotNull(skin);
Assert.IsFalse(skin.open, "외형 = 잠김(추후)");
foreach (var it in Db.Costume.items) Assert.IsNotNull(CostumeRules.Art(it), it.id + " 그림");
Assert.Greater(Db.Costume.frames.Count, 0, "유닛 알파 경계");
OutGameTables.Destroy(r);
}
[Test]
public void Wear_Unwear_SharedOwnership_UnownedPreviewBlocked_SkinBad_Reload()
{
var cdb = Db.Costume;
var d = HeroRules.NewSave(Db, 5, T0);
var heroes = new List(d.heroes);
Assert.GreaterOrEqual(heroes.Count, 2);
var face = First("face");
var head = First("head");
Assert.IsTrue(CostumeRules.Grant(cdb, d, face.id));
Assert.IsTrue(d.costumeNew, "새 꾸미기 = 빨간 점");
Assert.IsTrue(CostumeRules.Grant(cdb, d, head.id));
Assert.IsFalse(CostumeRules.Grant(cdb, d, face.id), "이미 보유");
// 착용 · 두 영웅이 같은 꾸미기(계정 공용)
var map = new Dictionary { { "face", face.id }, { "head", head.id } };
Assert.IsNull(CostumeRules.Apply(cdb, d, heroes[0], map));
Assert.IsNull(CostumeRules.Apply(cdb, d, heroes[1], new Dictionary { { "face", face.id } }));
Assert.AreEqual(face.id, CostumeRules.Worn(cdb, heroes[0])["face"]);
Assert.AreEqual(face.id, CostumeRules.Worn(cdb, heroes[1])["face"], "같은 꾸미기를 여러 영웅이");
// 미보유 미리보기 = 적용 막힘(바뀌지 않음)
CostumeDb.Item unowned = null;
foreach (var it in cdb.items)
if (!CostumeRules.Owns(d, it.id) && it.slot == "face") unowned = it;
Assert.IsNotNull(unowned);
var pv = new Dictionary(CostumeRules.Worn(cdb, heroes[0]));
pv["face"] = unowned.id;
Assert.AreEqual("unowned", CostumeRules.Apply(cdb, d, heroes[0], pv), "미보유 = 적용 안 됨");
Assert.AreEqual(face.id, CostumeRules.Worn(cdb, heroes[0])["face"], "그대로");
// 외형 칸 = 잠김
Assert.AreEqual("bad", CostumeRules.CanApply(cdb, d, new Dictionary { { "skin", face.id } }));
// 벗기
Assert.IsNull(CostumeRules.Apply(cdb, d, heroes[0], new Dictionary { { "face", "" }, { "head", head.id } }));
Assert.IsFalse(CostumeRules.Worn(cdb, heroes[0]).ContainsKey("face"), "벗기");
Assert.AreEqual(head.id, CostumeRules.Worn(cdb, heroes[0])["head"]);
// 세이브 다시 불러오기
var back = JsonUtility.FromJson(JsonUtility.ToJson(d));
HeroRules.Sanitize(Db, back, 5);
Assert.AreEqual(d.costumes.Count, back.costumes.Count, "보유 유지");
Assert.AreEqual(head.id, CostumeRules.Worn(cdb, back.Hero(heroes[0].id))["head"], "착용 유지");
Assert.AreEqual(face.id, CostumeRules.Worn(cdb, back.Hero(heroes[1].id))["face"]);
}
[Test]
public void Anchor_Table_Fallback_FrameShift_BackHidesFace()
{
var cdb = Db.Costume;
var face = cdb.SlotOf("face");
var headSlot = cdb.SlotOf("head");
var weapon = cdb.SlotOf("weapon");
// 표 기준점(관우)
var a = cdb.anchors[0];
var h = Db.Heroes.Get(a.unit);
var baseF = cdb.FrameOf(h.art, "front_idle", 0);
Assert.IsNotNull(baseF, a.unit + " 경계");
var sl = cdb.SlotOf(a.anchor) ?? face;
foreach (var s in cdb.slots)
if (s.anchor == a.anchor) sl = s;
var p = CostumeRules.Resolve(cdb, a.unit, h.art, sl, "front_idle", 0);
Assert.IsTrue(p.show);
Assert.AreEqual(a.x * baseF.w, p.x, 0.01f, "x = f_x × 그림 폭");
Assert.AreEqual(a.y * baseF.h, p.y, 0.01f, "y = f_y × 그림 높이");
Assert.AreEqual(baseF.w * a.scale, p.width, 0.01f, "폭 = 그림 폭 × f_scale");
// 다른 프레임 = 윗선 이동만큼
CostumeDb.Frame other = null;
foreach (var f in cdb.frames)
if (f.art == h.art && f.top != baseF.top && !f.clip.StartsWith("back")) { other = f; break; }
if (other != null)
{
var q = CostumeRules.Resolve(cdb, a.unit, h.art, sl, other.clip, other.index);
Assert.AreEqual(p.y + (other.top - baseF.top), q.y, 0.01f, "윗선 이동만큼 따라감");
}
// 뒷모습 = 얼굴 숨김
Assert.IsFalse(CostumeRules.Resolve(cdb, a.unit, h.art, face, "back_idle", 0).show, "뒷모습 얼굴 숨김");
// 대체(표에 줄이 없는 영웅) = 머리 위 중앙 · 얼굴 위에서 38 % · 무기 숨김
HeroDb.Hero o = null;
foreach (var x in Db.Heroes.heroes)
if (!x.isSoldier && cdb.AnchorOf(x.id, "head") == null && cdb.FrameOf(x.art, "front_idle", 0) != null) { o = x; break; }
Assert.IsNotNull(o, "기준점 표에 없는 영웅");
var of = cdb.FrameOf(o.art, "front_idle", 0);
var ph = CostumeRules.Resolve(cdb, o.id, o.art, headSlot, "front_idle", 0);
Assert.IsTrue(ph.show);
Assert.AreEqual((of.left + of.right) * 0.5f, ph.x, 0.01f, "머리 = 위 중앙");
Assert.AreEqual(of.top, ph.y, 0.01f);
var pf = CostumeRules.Resolve(cdb, o.id, o.art, face, "front_idle", 0);
Assert.AreEqual(of.top + (of.bottom - of.top) * 0.38f, pf.y, 0.01f, "얼굴 = 위에서 38 %");
Assert.IsFalse(CostumeRules.Resolve(cdb, o.id, o.art, weapon, "front_idle", 0).show, "무기 기준점 없음 = 숨김");
}
static IEnumerator Boot()
{
WorldProgress.Reset();
SaveStore.UseMemory(true);
SceneManager.LoadScene("Battle");
yield return null;
yield return null;
}
static Button Find(OutGameRoot og, string name)
{
foreach (var b in og.TopRoot.GetComponentsInChildren