// Package miot_test provides black-box tests for web_pages.go page templates. package miot_test import ( "strings" "testing" miot "xiaomihome/miot" ) // ============================================================================ // OAuthRedirectPage // ============================================================================ func TestOAuthRedirectPage_Success(t *testing.T) { html := miot.OAuthRedirectPage("My Title", "Welcome!", "Close", true) if html == "" { t.Fatal("OAuthRedirectPage returned empty string") } if !strings.Contains(html, "My Title") { t.Error("HTML should contain 'My Title'") } if !strings.Contains(html, "Welcome!") { t.Error("HTML should contain 'Welcome!'") } if !strings.Contains(html, "Close") { t.Error("HTML should contain 'Close'") } // STATUS_PLACEHOLDER should be "true" if !strings.Contains(html, "true") { t.Error("HTML should contain 'true' for success") } // Note: TITLE_PLACEHOLDER appears twice in template but strings.Replace(...,1) // only replaces the first occurrence (in ). The second occurrence // (in <a id="titleArea">) remains. // CONTENT_PLACEHOLDER and BUTTON_PLACEHOLDER each appear once and are replaced. if strings.Contains(html, "CONTENT_PLACEHOLDER") { t.Error("CONTENT_PLACEHOLDER should have been replaced") } if strings.Contains(html, "BUTTON_PLACEHOLDER") { t.Error("BUTTON_PLACEHOLDER should have been replaced") } if strings.Contains(html, "STATUS_PLACEHOLDER") { t.Error("STATUS_PLACEHOLDER should have been replaced") } } func TestOAuthRedirectPage_Failure(t *testing.T) { html := miot.OAuthRedirectPage("Error", "Auth failed", "Retry", false) if html == "" { t.Fatal("OAuthRedirectPage returned empty string") } // STATUS_PLACEHOLDER should be "false" if !strings.Contains(html, "false") { t.Error("HTML should contain 'false' for failure") } } func TestOAuthRedirectPage_EmptyValues(t *testing.T) { html := miot.OAuthRedirectPage("", "", "", false) if html == "" { t.Fatal("OAuthRedirectPage with empty values should still return HTML") } // Note: TITLE_PLACEHOLDER appears twice, only first is replaced. // CONTENT_PLACEHOLDER, BUTTON_PLACEHOLDER, STATUS_PLACEHOLDER should be gone. if strings.Contains(html, "CONTENT_PLACEHOLDER") { t.Error("CONTENT_PLACEHOLDER should not remain") } if strings.Contains(html, "BUTTON_PLACEHOLDER") { t.Error("BUTTON_PLACEHOLDER should not remain") } if strings.Contains(html, "STATUS_PLACEHOLDER") { t.Error("STATUS_PLACEHOLDER should not remain") } } func TestOAuthRedirectPage_SpecialCharacters(t *testing.T) { title := "Test <>&\"' Title" html := miot.OAuthRedirectPage(title, "content", "button", true) if html == "" { t.Fatal("OAuthRedirectPage returned empty string") } if !strings.Contains(html, title) { t.Error("HTML should contain the special char title") } } func TestOAuthRedirectPage_LongContent(t *testing.T) { longTitle := strings.Repeat("A", 1000) longContent := strings.Repeat("B", 5000) html := miot.OAuthRedirectPage(longTitle, longContent, "OK", true) if !strings.Contains(html, longTitle) { t.Error("HTML should contain long title") } if !strings.Contains(html, longContent) { t.Error("HTML should contain long content") } } func TestOAuthRedirectPage_NonExhaustivePlaceholders(t *testing.T) { // Verify placeholder replacement behavior html := miot.OAuthRedirectPage("T", "C", "B", true) // These appear once and should be fully replaced if strings.Contains(html, "CONTENT_PLACEHOLDER") { t.Error("CONTENT_PLACEHOLDER should be replaced") } if strings.Contains(html, "BUTTON_PLACEHOLDER") { t.Error("BUTTON_PLACEHOLDER should be replaced") } if strings.Contains(html, "STATUS_PLACEHOLDER") { t.Error("STATUS_PLACEHOLDER should be replaced") } // Note: TITLE_PLACEHOLDER has 2 occurrences, only first is replaced }