using System; using System.Collections.Generic; namespace HanvonF710XAttendanceService { /// /// Lightweight unit tests for DB_TO_DEVICE planning logic (no MSTest dependency). /// internal static class DbToDevicePlanningTests { public static int RunAll() { int failed = 0; failed += Run("1 DB_TO_DEVICE ignores source device", TestDbToDeviceIgnoresSource); failed += Run("2 DEVICE_TO_DB requires source device", TestDeviceToDbRequiresSource); failed += Run("3 DB_TO_DEVICE allows empty SourceIp planning", TestDbToDeviceAllowsEmptySourceIp); failed += Run("4 EmpIds only selection", TestEmpIdsFilter); failed += Run("5 DepartmentIds selection", TestDepartmentIdsFallback); failed += Run("6 empty EmpIds uses registered employees", TestEmptyEmpIdsUsesRegistered); failed += Run("7 normalize employee ids", TestNormalizeEmployeeIds); failed += Run("8 DEVICE_TO_DEVICE requires source device", TestDeviceToDeviceRequiresSource); failed += Run("9 template load batches for 673 employees", TestTemplateLoadBatchCount673); return failed; } private static int Run(string name, Func test) { try { if (!test()) { Console.WriteLine("FAIL: " + name); return 1; } Console.WriteLine("PASS: " + name); return 0; } catch (Exception ex) { Console.WriteLine("FAIL: " + name + " err=" + ex.Message); return 1; } } private static bool TestDbToDeviceIgnoresSource() { return DbToDevicePlanning.ShouldIgnoreSourceDevice(TemplateTransferMode.DB_TO_DEVICE); } private static bool TestDeviceToDbRequiresSource() { return !DbToDevicePlanning.ShouldIgnoreSourceDevice(TemplateTransferMode.DEVICE_TO_DB); } private static bool TestDeviceToDeviceRequiresSource() { return !DbToDevicePlanning.ShouldIgnoreSourceDevice(TemplateTransferMode.DEVICE_TO_DEVICE); } private static bool TestDbToDeviceAllowsEmptySourceIp() { var job = new TemplateTransferJob("1", true, "", new List { "192.168.90.223" }, new List { "5" }, null); var ids = DbToDevicePlanning.ResolveEmployeeIds(job, _ => new List(), () => new List { "5" }); return ids.Count == 1 && ids[0] == "5"; } private static bool TestOtherModesRequireSource() { return TestDeviceToDbRequiresSource() && TestDeviceToDeviceRequiresSource(); } private static bool TestEmpIdsFilter() { var job = new TemplateTransferJob("1", true, "10.0.1.3", new List { "192.168.90.223" }, new List { "5", "55", "96" }, null); var ids = DbToDevicePlanning.ResolveEmployeeIds( job, _ => new List { "999" }, () => new List { "999" }); return ids.Count == 3 && ids.Contains("5") && ids.Contains("55") && ids.Contains("96"); } private static bool TestDepartmentIdsFallback() { var job = new TemplateTransferJob("1", true, "", new List { "192.168.90.223" }, null, new List { "10", "20" }); bool deptCalled = false; var ids = DbToDevicePlanning.ResolveEmployeeIds( job, departmentIds => { deptCalled = true; return new List { "100", "200" }; }, () => new List { "300" }); return deptCalled && ids.Count == 2 && ids.Contains("100") && ids.Contains("200"); } private static bool TestEmptyEmpIdsUsesRegistered() { var job = new TemplateTransferJob("1", true, "", new List { "192.168.90.223" }, null, null); var ids = DbToDevicePlanning.ResolveEmployeeIds( job, _ => new List(), () => new List { "5", "55" }); return ids.Count == 2 && ids.Contains("5") && ids.Contains("55"); } private static bool TestNormalizeEmployeeIds() { var ids = DbToDevicePlanning.NormalizeEmployeeIds(new[] { " 5 ", "5", "", null, "96" }); return ids.Count == 2 && ids[0] == "5" && ids[1] == "96"; } private static bool TestTemplateLoadBatchCount673() { return DbToDevicePlanning.ComputeTemplateLoadBatchCount(673) == 14 && DbToDevicePlanning.ComputeLastTemplateLoadBatchSize(673) == 23; } } }