@@ -546,3 +546,135 @@ describe("Cohort API - Database Operations", () => {
546546 } ) ;
547547 } ) ;
548548} ) ;
549+
550+ describe ( "Refresh Messages - Validation Logic" , ( ) => {
551+ let db : Database . Database ;
552+
553+ beforeEach ( ( ) => {
554+ db = createTestDatabase ( ) ;
555+ } ) ;
556+
557+ afterEach ( ( ) => {
558+ db . close ( ) ;
559+ } ) ;
560+
561+ describe ( "cohortId parameter validation" , ( ) => {
562+ it ( "rejects non-numeric cohort ID" , ( ) => {
563+ const parsed = parseInt ( "abc" , 10 ) ;
564+ expect ( isNaN ( parsed ) ) . toBe ( true ) ;
565+ } ) ;
566+
567+ it ( "accepts valid numeric cohort ID" , ( ) => {
568+ const parsed = parseInt ( "5" , 10 ) ;
569+ expect ( isNaN ( parsed ) ) . toBe ( false ) ;
570+ expect ( parsed ) . toBe ( 5 ) ;
571+ } ) ;
572+ } ) ;
573+
574+ describe ( "cohort lookup" , ( ) => {
575+ it ( "returns undefined for non-existent cohort" , ( ) => {
576+ const stmt = db . prepare ( "SELECT * FROM cohorts WHERE id = ?" ) ;
577+ const cohort = stmt . get ( 9999 ) ;
578+ expect ( cohort ) . toBeUndefined ( ) ;
579+ } ) ;
580+
581+ it ( "finds existing cohort by ID" , ( ) => {
582+ const created = createTestCohort ( db , "Sp2026" ) ;
583+
584+ const stmt = db . prepare ( "SELECT * FROM cohorts WHERE id = ?" ) ;
585+ const cohort = stmt . get ( created . id ) as { id : number ; name : string } ;
586+
587+ expect ( cohort ) . toBeDefined ( ) ;
588+ expect ( cohort . name ) . toBe ( "Sp2026" ) ;
589+ } ) ;
590+ } ) ;
591+
592+ describe ( "start_date validation" , ( ) => {
593+ it ( "rejects cohort with no start_date" , ( ) => {
594+ const cohort = createTestCohort ( db , "NoDate" ) ;
595+
596+ const stmt = db . prepare ( "SELECT start_date FROM cohorts WHERE id = ?" ) ;
597+ const row = stmt . get ( cohort . id ) as { start_date : string | null } ;
598+
599+ expect ( row . start_date ) . toBeNull ( ) ;
600+ } ) ;
601+
602+ it ( "accepts cohort with start_date" , ( ) => {
603+ const cohort = createTestCohort ( db , "HasDate" ) ;
604+ db . prepare ( "UPDATE cohorts SET start_date = ? WHERE id = ?" )
605+ . run ( "2026-02-02" , cohort . id ) ;
606+
607+ const stmt = db . prepare ( "SELECT start_date FROM cohorts WHERE id = ?" ) ;
608+ const row = stmt . get ( cohort . id ) as { start_date : string } ;
609+
610+ expect ( row . start_date ) . toBe ( "2026-02-02" ) ;
611+ } ) ;
612+
613+ it ( "parses start_date into a valid Date" , ( ) => {
614+ const since = new Date ( "2026-02-02" ) ;
615+ expect ( since . getTime ( ) ) . not . toBeNaN ( ) ;
616+ expect ( since . toISOString ( ) ) . toBe ( "2026-02-02T00:00:00.000Z" ) ;
617+ } ) ;
618+ } ) ;
619+
620+ describe ( "message upsert on refresh" , ( ) => {
621+ it ( "inserts new messages into the database" , ( ) => {
622+ const channel = createTestChannel ( db , "ch-1" , "eod" ) ;
623+ const user = createTestUser ( db , "user-1" , "testuser" ) ;
624+
625+ db . prepare ( `
626+ INSERT INTO messages (discord_message_id, channel_id, author_id, content, created_at)
627+ VALUES (?, ?, ?, ?, ?)
628+ ` ) . run ( "msg-1" , channel . channel_id , user . author_id , "Original EOD" , "2026-02-06T18:00:00.000Z" ) ;
629+
630+ const stmt = db . prepare ( "SELECT content FROM messages WHERE discord_message_id = ?" ) ;
631+ const row = stmt . get ( "msg-1" ) as { content : string } ;
632+ expect ( row . content ) . toBe ( "Original EOD" ) ;
633+ } ) ;
634+
635+ it ( "upserts edited messages to update content" , ( ) => {
636+ const channel = createTestChannel ( db , "ch-1" , "eod" ) ;
637+ const user = createTestUser ( db , "user-1" , "testuser" ) ;
638+
639+ // Insert original
640+ db . prepare ( `
641+ INSERT INTO messages (discord_message_id, channel_id, author_id, content, created_at)
642+ VALUES (?, ?, ?, ?, ?)
643+ ` ) . run ( "msg-1" , channel . channel_id , user . author_id , "Original EOD" , "2026-02-06T18:00:00.000Z" ) ;
644+
645+ // Upsert with updated content (same discord_message_id)
646+ db . prepare ( `
647+ INSERT INTO messages (discord_message_id, channel_id, author_id, content, created_at)
648+ VALUES (?, ?, ?, ?, ?)
649+ ON CONFLICT(discord_message_id) DO UPDATE SET content = excluded.content
650+ ` ) . run ( "msg-1" , channel . channel_id , user . author_id , "Updated EOD with 9 PRs" , "2026-02-06T18:00:00.000Z" ) ;
651+
652+ const stmt = db . prepare ( "SELECT content FROM messages WHERE discord_message_id = ?" ) ;
653+ const row = stmt . get ( "msg-1" ) as { content : string } ;
654+ expect ( row . content ) . toBe ( "Updated EOD with 9 PRs" ) ;
655+ } ) ;
656+
657+ it ( "does not create duplicate rows on upsert" , ( ) => {
658+ const channel = createTestChannel ( db , "ch-1" , "eod" ) ;
659+ const user = createTestUser ( db , "user-1" , "testuser" ) ;
660+
661+ const insertStmt = db . prepare ( `
662+ INSERT INTO messages (discord_message_id, channel_id, author_id, content, created_at)
663+ VALUES (?, ?, ?, ?, ?)
664+ ON CONFLICT(discord_message_id) DO UPDATE SET content = excluded.content
665+ ` ) ;
666+
667+ insertStmt . run ( "msg-1" , channel . channel_id , user . author_id , "Version 1" , "2026-02-06T18:00:00.000Z" ) ;
668+ insertStmt . run ( "msg-1" , channel . channel_id , user . author_id , "Version 2" , "2026-02-06T18:00:00.000Z" ) ;
669+ insertStmt . run ( "msg-1" , channel . channel_id , user . author_id , "Version 3" , "2026-02-06T18:00:00.000Z" ) ;
670+
671+ const count = db . prepare ( "SELECT COUNT(*) as count FROM messages WHERE discord_message_id = ?" )
672+ . get ( "msg-1" ) as { count : number } ;
673+ expect ( count . count ) . toBe ( 1 ) ;
674+
675+ const row = db . prepare ( "SELECT content FROM messages WHERE discord_message_id = ?" )
676+ . get ( "msg-1" ) as { content : string } ;
677+ expect ( row . content ) . toBe ( "Version 3" ) ;
678+ } ) ;
679+ } ) ;
680+ } ) ;
0 commit comments