<% Class PurpleEnvelopeReport_Tests Private m_tempDbPath Public Sub Setup m_tempDbPath = CreateDisposableDatabaseCopy() UseDisposableDatabase m_tempDbPath EnsureDeliveryLabelPageTable End Sub Public Sub Teardown ResetDisposableDatabase End Sub Public Function TestCaseNames TestCaseNames = Array( _ "Test_FormatElectionLabel_Returns_Mon_YYYY_For_Valid_Date", _ "Test_FormatElectionLabel_Returns_Trimmed_Raw_Value_For_Invalid_Date", _ "Test_SortPrecinctColorRows_Handles_Zero_Padded_And_Lettered_Precincts", _ "Test_SortPrecinctBallotRangeRows_Preserves_Ranges_While_Sorting", _ "Test_GetPrecinctBallotRangesByKitId_Uses_Seeded_Data", _ "Test_UpdateColorForKit_Updates_All_Seeded_Rows_For_The_Target_Kit", _ "Test_UpdateColorForPrecinct_Updates_Only_The_Targeted_Precinct", _ "Test_SwitchBoardPurpleEnvelopeEditFindById_Returns_Seeded_Header_Data", _ "Test_SaveDeliveryLabelPages_Persists_Seeded_Page_Data", _ "Test_SaveDeliveryLabelPages_Replaces_Existing_Pages_For_The_Kit", _ "Test_DeleteDeliveryLabelPages_Removes_All_Staged_Pages_For_The_Kit", _ "Test_KitController_Post_Actions_Still_Delegate_To_Color_Update_Repositories", _ "Test_Report_View_Keeps_Print_Only_CSS_Contract", _ "Test_Report_View_Keeps_No_Data_Row_And_Page_Spacer", _ "Test_Report_View_Only_Shows_Delivery_Paperwork_Link_When_Kit_Is_Done", _ "Test_KitController_Declares_PrintDeliveryPaperwork_Action" _ ) End Function Private Sub Destroy(ByRef o) On Error Resume Next o.Close On Error GoTo 0 Set o = Nothing End Sub Private Function CreatePrecinctColorRecordset(ByVal rows) dim rs : set rs = Server.CreateObject("ADODB.Recordset") With rs.Fields .Append "PRECINCT", adVarChar, 50 .Append "ColorId", adVarChar, 50 End With rs.Open dim i For i = 0 To UBound(rows) rs.AddNew rs("PRECINCT") = rows(i)(0) If IsNull(rows(i)(1)) Then rs("ColorId") = Null Else rs("ColorId") = rows(i)(1) End If rs.Update Next rs.MoveFirst set CreatePrecinctColorRecordset = rs End Function Private Function CreateBallotRangeRecordset(ByVal rows) dim rs : set rs = Server.CreateObject("ADODB.Recordset") With rs.Fields .Append "PRECINCT", adVarChar, 50 .Append "LowBallotNumber", adInteger .Append "HighBallotNumber", adInteger End With rs.Open dim i For i = 0 To UBound(rows) rs.AddNew rs("PRECINCT") = rows(i)(0) rs("LowBallotNumber") = rows(i)(1) rs("HighBallotNumber") = rows(i)(2) rs.Update Next rs.MoveFirst set CreateBallotRangeRecordset = rs End Function Private Function PrecinctListToCsv(ByVal list) dim values() dim idx : idx = -1 dim it : set it = list.Iterator() dim row Do While it.HasNext set row = it.GetNext() idx = idx + 1 ReDim Preserve values(idx) values(idx) = row.PRECINCT Loop If idx = -1 Then PrecinctListToCsv = "" Else PrecinctListToCsv = Join(values, ",") End If End Function Private Function ReadAllText(ByVal relativePath) dim fso : set fso = Server.CreateObject("Scripting.FileSystemObject") dim fileHandle : set fileHandle = fso.OpenTextFile(Server.MapPath(relativePath), 1, False) ReadAllText = fileHandle.ReadAll() fileHandle.Close Set fileHandle = Nothing Set fso = Nothing End Function Private Function CreateDisposableDatabaseCopy() dim fso : set fso = Server.CreateObject("Scripting.FileSystemObject") dim tempFolderPath : tempFolderPath = Server.MapPath("Temp") If Not fso.FolderExists(tempFolderPath) Then fso.CreateFolder tempFolderPath End If dim sourcePath : sourcePath = Server.MapPath("../Data/webdata - Copy.mdb") dim guidValue : guidValue = Mid(CreateObject("Scriptlet.TypeLib").Guid, 2, 36) guidValue = Replace(guidValue, "-", "") CreateDisposableDatabaseCopy = tempFolderPath & "\purple-envelope-tests-" & guidValue & ".mdb" fso.CopyFile sourcePath, CreateDisposableDatabaseCopy, True Set fso = Nothing End Function Private Sub UseDisposableDatabase(ByVal dbPath) Set DAL__Singleton = Nothing set DAL__Singleton = new Database_Class DAL__Singleton.Initialize "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Engine Type=5;Data Source=" & dbPath & ";" End Sub Private Sub EnsureDeliveryLabelPageTable() On Error Resume Next DAL.Execute "CREATE TABLE [DeliveryLabelPage] (" & _ "[ID] COUNTER CONSTRAINT [PrimaryKey] PRIMARY KEY, " & _ "[KitID] LONG NOT NULL, " & _ "[PageNum] INTEGER NOT NULL, " & _ "[Label1] MEMO, " & _ "[Label2] MEMO, " & _ "[Label3] MEMO, " & _ "[Label4] MEMO, " & _ "[Label5] MEMO, " & _ "[Label6] MEMO);", empty Err.Clear On Error GoTo 0 End Sub Private Sub ResetDisposableDatabase() dim tempPath : tempPath = m_tempDbPath Set DAL__Singleton = Nothing If Len(tempPath) > 0 Then dim fso : set fso = Server.CreateObject("Scripting.FileSystemObject") If fso.FileExists(tempPath) Then fso.DeleteFile tempPath, True End If Set fso = Nothing End If m_tempDbPath = "" End Sub Private Function NextSeedKey(ByVal prefix) NextSeedKey = prefix & Replace(Replace(Replace(CStr(Now()), "/", ""), ":", ""), " ", "") & CStr(Int((Rnd() * 1000000) + 1)) End Function Private Function NextJurisdictionCode() dim guidValue : guidValue = Mid(CreateObject("Scriptlet.TypeLib").Guid, 2, 8) NextJurisdictionCode = "J" & Replace(guidValue, "-", "") End Function Private Function SeedJurisdiction(ByVal jCode, ByVal name) DAL.Execute "INSERT INTO [Jurisdiction] ([JCode], [Name], [Mailing_Address], [CSZ], [IMB], [IMB_Digits]) VALUES (?,?,?,?,?,?)", _ Array(jCode, name, "123 Test St", "Lansing, MI 48933", "IMB", "123456789") SeedJurisdiction = jCode End Function Private Function SeedPurpleEnvelopeKit(ByVal jobNumber, ByVal jCode, ByVal status) DAL.Execute "INSERT INTO [Kit] ([JobNumber], [Jcode], [CreatedOn], [InkJetJob], [JobType], [Cass], [Status], [OutboundSTID], [InboundSTID], [OfficeCopiesAmount]) VALUES (?,?,?,?,?,?,?,?,?,?)", _ Array(jobNumber, jCode, Now(), False, "Purple Envelopes", False, status, "", "", Null) SeedPurpleEnvelopeKit = QueryScalar("SELECT TOP 1 [ID] FROM [Kit] WHERE [JobNumber] = ? ORDER BY [ID] DESC", jobNumber) End Function Private Sub SeedInkjetRecord(ByVal kitId, ByVal precinct, ByVal ballotNumber, ByVal colorId) DAL.Execute "INSERT INTO [InkjetRecords] ([KitID], [PRECINCT], [BALLOT_NUMBER], [ColorId]) VALUES (?,?,?,?)", _ Array(kitId, precinct, ballotNumber, colorId) End Sub Private Function QueryScalar(ByVal sql, ByVal params) dim rs : set rs = DAL.Query(sql, params) QueryScalar = rs(0).Value Destroy rs End Function Private Function QueryPrecinctColorMap(ByVal kitId) dim map : set map = Server.CreateObject("Scripting.Dictionary") dim rs : set rs = DAL.Query("SELECT [PRECINCT], [ColorId] FROM [InkjetRecords] WHERE [KitID] = ? ORDER BY [PRECINCT]", kitId) Do Until rs.EOF If IsNull(rs("ColorId")) Then map(rs("PRECINCT") & "") = "" Else map(rs("PRECINCT") & "") = CStr(rs("ColorId")) End If rs.MoveNext Loop Destroy rs set QueryPrecinctColorMap = map End Function Private Function BuildDeliveryLabelPage(ByVal pageNum, ByVal label1, ByVal label2, ByVal label3, ByVal label4, ByVal label5, ByVal label6) dim page : set page = new DeliveryLabelPage_Class page.PageNum = pageNum page.Label1 = label1 page.Label2 = label2 page.Label3 = label3 page.Label4 = label4 page.Label5 = label5 page.Label6 = label6 set BuildDeliveryLabelPage = page End Function Public Sub Test_FormatElectionLabel_Returns_Mon_YYYY_For_Valid_Date(T) T.AssertEqual "May-2026", PurpleEnvelopeReportHelper().FormatElectionLabel("5/26/2026"), "Expected valid dates to render as Mon-YYYY." End Sub Public Sub Test_FormatElectionLabel_Returns_Trimmed_Raw_Value_For_Invalid_Date(T) T.AssertEqual "not a date", PurpleEnvelopeReportHelper().FormatElectionLabel(" not a date "), "Expected invalid election dates to fall back to trimmed raw text." T.AssertEqual "", PurpleEnvelopeReportHelper().FormatElectionLabel(""), "Expected empty election dates to stay empty." End Sub Public Sub Test_SortPrecinctColorRows_Handles_Zero_Padded_And_Lettered_Precincts(T) dim rs : set rs = CreatePrecinctColorRecordset(Array( _ Array("12B", "2"), _ Array("0003", ""), _ Array("A1", "4"), _ Array("12A", "3"), _ Array("3", "5"), _ Array("0001", "1"), _ Array("12", "6") _ )) dim sorted : set sorted = PurpleEnvelopeReportHelper().SortPrecinctColorRows(rs) T.AssertEqual "0001,0003,3,12,12A,12B,A1", PrecinctListToCsv(sorted), "Expected mixed-format precincts to sort in the current ascending order." Destroy rs Destroy sorted End Sub Public Sub Test_SortPrecinctBallotRangeRows_Preserves_Ranges_While_Sorting(T) dim rs : set rs = CreateBallotRangeRecordset(Array( _ Array("12A", 20, 29), _ Array("0003", 1, 10), _ Array("12", 11, 19), _ Array("A1", 30, 39) _ )) dim sorted : set sorted = PurpleEnvelopeReportHelper().SortPrecinctBallotRangeRows(rs) dim it : set it = sorted.Iterator() dim row T.AssertEqual "0003,12,12A,A1", PrecinctListToCsv(sorted), "Expected report rows to follow the same precinct order as the color table." set row = it.GetNext() T.AssertEqual 1, row.LowBallotNumber, "Expected the low ballot number to stay attached to precinct 0003." T.AssertEqual 10, row.HighBallotNumber, "Expected the high ballot number to stay attached to precinct 0003." Destroy rs Destroy sorted End Sub Public Sub Test_GetPrecinctBallotRangesByKitId_Uses_Seeded_Data(T) Randomize dim jCode : jCode = NextJurisdictionCode() dim jobNumber : jobNumber = NextSeedKey("JOB") Call SeedJurisdiction(jCode, "City of Lansing") dim kitId : kitId = SeedPurpleEnvelopeKit(jobNumber, jCode, "Ready To Assign STIDS") SeedInkjetRecord kitId, "12A", "29", Null SeedInkjetRecord kitId, "0003", "10", Null SeedInkjetRecord kitId, "12A", "20", Null SeedInkjetRecord kitId, "0003", "1", Null SeedInkjetRecord kitId, "A1", "30", Null dim rs : set rs = InkjetRecordsRepository.GetPrecinctBallotRangesByKitId(kitId) dim sorted : set sorted = PurpleEnvelopeReportHelper().SortPrecinctBallotRangeRows(rs) dim it : set it = sorted.Iterator() dim firstRow, secondRow, thirdRow set firstRow = it.GetNext() set secondRow = it.GetNext() set thirdRow = it.GetNext() T.AssertEqual "0003,12A,A1", PrecinctListToCsv(sorted), "Expected seeded ballot ranges to sort in the current report order." T.AssertEqual 1, firstRow.LowBallotNumber, "Expected precinct 0003 to use the seeded minimum ballot number." T.AssertEqual 10, firstRow.HighBallotNumber, "Expected precinct 0003 to use the seeded maximum ballot number." T.AssertEqual 20, secondRow.LowBallotNumber, "Expected precinct 12A to use the seeded minimum ballot number." T.AssertEqual 29, secondRow.HighBallotNumber, "Expected precinct 12A to use the seeded maximum ballot number." T.AssertEqual 30, thirdRow.LowBallotNumber, "Expected single-row precincts to keep their ballot number as the low value." T.AssertEqual 30, thirdRow.HighBallotNumber, "Expected single-row precincts to keep their ballot number as the high value." Destroy rs Destroy sorted Set firstRow = Nothing Set secondRow = Nothing Set thirdRow = Nothing End Sub Public Sub Test_UpdateColorForKit_Updates_All_Seeded_Rows_For_The_Target_Kit(T) Randomize dim jCode : jCode = NextJurisdictionCode() Call SeedJurisdiction(jCode, "City of Lansing") dim targetKitId : targetKitId = SeedPurpleEnvelopeKit(NextSeedKey("JOB"), jCode, "Ready To Assign STIDS") dim otherKitId : otherKitId = SeedPurpleEnvelopeKit(NextSeedKey("JOB"), jCode, "Ready To Assign STIDS") SeedInkjetRecord targetKitId, "0003", "1", 1 SeedInkjetRecord targetKitId, "12A", "2", Null SeedInkjetRecord otherKitId, "9", "3", 4 InkjetRecordsRepository.UpdateColorForKit targetKitId, 7 dim targetMap : set targetMap = QueryPrecinctColorMap(targetKitId) dim otherMap : set otherMap = QueryPrecinctColorMap(otherKitId) T.AssertEqual "7", targetMap("0003"), "Expected kit-wide color updates to touch every row in the targeted kit." T.AssertEqual "7", targetMap("12A"), "Expected kit-wide color updates to touch every row in the targeted kit." T.AssertEqual "4", otherMap("9"), "Expected kit-wide color updates to leave other kits unchanged." Set targetMap = Nothing Set otherMap = Nothing End Sub Public Sub Test_UpdateColorForPrecinct_Updates_Only_The_Targeted_Precinct(T) Randomize dim jCode : jCode = NextJurisdictionCode() Call SeedJurisdiction(jCode, "City of Lansing") dim kitId : kitId = SeedPurpleEnvelopeKit(NextSeedKey("JOB"), jCode, "Ready To Assign STIDS") SeedInkjetRecord kitId, "0003", "1", 1 SeedInkjetRecord kitId, "12A", "2", 2 SeedInkjetRecord kitId, "A1", "3", Null InkjetRecordsRepository.UpdateColorForPrecinct kitId, "12A", 9 dim colorMap : set colorMap = QueryPrecinctColorMap(kitId) T.AssertEqual "1", colorMap("0003"), "Expected non-targeted precincts to keep their original color." T.AssertEqual "9", colorMap("12A"), "Expected the targeted precinct to receive the new color." T.AssertEqual "", colorMap("A1"), "Expected non-targeted blank colors to remain blank." Set colorMap = Nothing End Sub Public Sub Test_SwitchBoardPurpleEnvelopeEditFindById_Returns_Seeded_Header_Data(T) Randomize dim jCode : jCode = NextJurisdictionCode() dim jobNumber : jobNumber = NextSeedKey("JOB") Call SeedJurisdiction(jCode, "City of Lansing") dim kitId : kitId = SeedPurpleEnvelopeKit(jobNumber, jCode, "Ready To Assign STIDS") SeedInkjetRecord kitId, "0003", "1", Null SeedInkjetRecord kitId, "12A", "2", Null dim model : set model = KitRepository.SwitchBoardPurpleEnvelopeEditFindById(kitId) T.AssertEqual kitId, model.ID, "Expected the seeded purple-envelope kit to be returned." T.AssertEqual jobNumber, model.JobNumber, "Expected the seeded job number to be returned." T.AssertEqual jCode, model.JCode, "Expected the seeded jurisdiction code to be returned." T.AssertEqual "City of Lansing", model.Jurisdiction, "Expected the seeded jurisdiction name to be returned." T.AssertEqual 2, model.LabelCount, "Expected the seeded inkjet row count to flow into the label count." T.AssertEqual "Ready To Assign STIDS", model.Status, "Expected the seeded status to be returned." Set model = Nothing End Sub Public Sub Test_SaveDeliveryLabelPages_Persists_Seeded_Page_Data(T) Randomize dim jCode : jCode = NextJurisdictionCode() Call SeedJurisdiction(jCode, "City of Lansing") dim kitId : kitId = SeedPurpleEnvelopeKit(NextSeedKey("JOB"), jCode, "Done") dim pages : set pages = new LinkedList_Class pages.Push BuildDeliveryLabelPage(1, "Jurisdiction A" & vbCrLf & "Box 1 of 2", "Jurisdiction A" & vbCrLf & "Box 2 of 2", "", "", "", "") KitRepository.SaveDeliveryLabelPages kitId, pages T.AssertEqual 1, QueryScalar("SELECT COUNT(*) FROM [DeliveryLabelPage] WHERE [KitID] = ?", kitId), "Expected one staged page row for the seeded kit." dim rs : set rs = DAL.Query("SELECT [PageNum], [Label1], [Label2], [Label3], [Label4], [Label5], [Label6] FROM [DeliveryLabelPage] WHERE [KitID] = ? ORDER BY [PageNum]", kitId) T.Assert Not rs.EOF, "Expected the seeded staged-page row to be present." T.AssertEqual 1, rs("PageNum"), "Expected staged page numbers to persist." T.AssertEqual "Jurisdiction A" & vbCrLf & "Box 1 of 2", rs("Label1"), "Expected Label1 to persist exactly." T.AssertEqual "Jurisdiction A" & vbCrLf & "Box 2 of 2", rs("Label2"), "Expected Label2 to persist exactly." T.AssertEqual "", rs("Label3"), "Expected unused label slots to persist as empty strings." Destroy rs Set pages = Nothing End Sub Public Sub Test_SaveDeliveryLabelPages_Replaces_Existing_Pages_For_The_Kit(T) Randomize dim jCode : jCode = NextJurisdictionCode() Call SeedJurisdiction(jCode, "City of Lansing") dim kitId : kitId = SeedPurpleEnvelopeKit(NextSeedKey("JOB"), jCode, "Done") dim firstRunPages : set firstRunPages = new LinkedList_Class firstRunPages.Push BuildDeliveryLabelPage(1, "Run1-Page1-Label1", "", "", "", "", "") firstRunPages.Push BuildDeliveryLabelPage(2, "Run1-Page2-Label1", "", "", "", "", "") KitRepository.SaveDeliveryLabelPages kitId, firstRunPages dim secondRunPages : set secondRunPages = new LinkedList_Class secondRunPages.Push BuildDeliveryLabelPage(1, "Run2-OnlyPage-Label1", "Run2-OnlyPage-Label2", "", "", "", "") KitRepository.SaveDeliveryLabelPages kitId, secondRunPages T.AssertEqual 1, QueryScalar("SELECT COUNT(*) FROM [DeliveryLabelPage] WHERE [KitID] = ?", kitId), "Expected the second save to replace all prior staged rows for the same kit." dim rs : set rs = DAL.Query("SELECT [PageNum], [Label1], [Label2] FROM [DeliveryLabelPage] WHERE [KitID] = ? ORDER BY [PageNum]", kitId) T.Assert Not rs.EOF, "Expected a replacement staged row after the second save." T.AssertEqual 1, rs("PageNum"), "Expected replacement rows to keep their explicit page numbers." T.AssertEqual "Run2-OnlyPage-Label1", rs("Label1"), "Expected replacement data to overwrite prior Label1 values." T.AssertEqual "Run2-OnlyPage-Label2", rs("Label2"), "Expected replacement data to overwrite prior Label2 values." Destroy rs Set firstRunPages = Nothing Set secondRunPages = Nothing End Sub Public Sub Test_DeleteDeliveryLabelPages_Removes_All_Staged_Pages_For_The_Kit(T) Randomize dim jCode : jCode = NextJurisdictionCode() Call SeedJurisdiction(jCode, "City of Lansing") dim kitId : kitId = SeedPurpleEnvelopeKit(NextSeedKey("JOB"), jCode, "Done") dim pages : set pages = new LinkedList_Class pages.Push BuildDeliveryLabelPage(1, "Delete-Label1", "", "", "", "", "") pages.Push BuildDeliveryLabelPage(2, "Delete-Label2", "", "", "", "", "") KitRepository.SaveDeliveryLabelPages kitId, pages KitRepository.DeleteDeliveryLabelPages kitId T.AssertEqual 0, QueryScalar("SELECT COUNT(*) FROM [DeliveryLabelPage] WHERE [KitID] = ?", kitId), "Expected DeleteDeliveryLabelPages to remove all staged rows for the kit." Set pages = Nothing End Sub Public Sub Test_KitController_Post_Actions_Still_Delegate_To_Color_Update_Repositories(T) dim controllerSource : controllerSource = ReadAllText("../App/Controllers/Kit/KitController.asp") T.Assert InStr(controllerSource, "Public Sub AssignKitColorPost") > 0, "Expected the kit-wide color POST action to remain present." T.Assert InStr(controllerSource, "InkjetRecordsRepository.UpdateColorForKit CLng(ID), CLng(Request.Form(""KitColorId""))") > 0, "Expected AssignKitColorPost to keep delegating to the kit-wide color repository update." T.Assert InStr(controllerSource, "Public Sub AssignPrecinctColorsPost") > 0, "Expected the precinct color POST action to remain present." T.Assert InStr(controllerSource, "InkjetRecordsRepository.UpdateColorForPrecinct CLng(ID), precinct, CLng(colorId)") > 0, "Expected AssignPrecinctColorsPost to keep delegating to the precinct-specific color repository update." End Sub Public Sub Test_Report_View_Keeps_Print_Only_CSS_Contract(T) dim viewSource : viewSource = ReadAllText("../App/Views/Kit/SwitchBoardPurpleEnvelopeEdit.asp") T.Assert InStr(viewSource, "id=""purple-envelope-report-print""") > 0, "Expected the report print wrapper id to remain in the view." T.Assert InStr(viewSource, "body * {") > 0, "Expected print CSS to hide non-report content." T.Assert InStr(viewSource, "font-size: 10pt;") > 0, "Expected the reduced report font size to remain unchanged." T.Assert InStr(viewSource, "padding: 0.45in 0.25in 0.25in 0.25in;") > 0, "Expected the print top buffer padding to remain unchanged." End Sub Public Sub Test_Report_View_Keeps_No_Data_Row_And_Page_Spacer(T) dim viewSource : viewSource = ReadAllText("../App/Views/Kit/SwitchBoardPurpleEnvelopeEdit.asp") T.Assert InStr(viewSource, "class=""print-page-spacer""") > 0, "Expected the repeated-page spacer row to remain in the report header." T.Assert InStr(viewSource, "No precinct ballot data found for this kit.") > 0, "Expected the empty-state report message to remain unchanged." End Sub Public Sub Test_Report_View_Only_Shows_Delivery_Paperwork_Link_When_Kit_Is_Done(T) dim viewSource : viewSource = ReadAllText("../App/Views/Kit/SwitchBoardPurpleEnvelopeEdit.asp") T.Assert InStr(viewSource, "Model.Kit.Status = ""Done""") > 0, "Expected the delivery paperwork button to remain gated by Done status." T.Assert InStr(viewSource, "PrintDeliveryPaperwork") > 0, "Expected the delivery paperwork link target to remain in the view." End Sub Public Sub Test_KitController_Declares_PrintDeliveryPaperwork_Action(T) dim controllerSource : controllerSource = ReadAllText("../App/Controllers/Kit/KitController.asp") T.Assert InStr(controllerSource, "Public Sub PrintDeliveryPaperwork") > 0, "Expected KitController to expose the PrintDeliveryPaperwork action." End Sub End Class %>