diff --git a/components/ai_chat/core/common/BUILD.gn b/components/ai_chat/core/common/BUILD.gn index e1080ea7705a..1a3d36a5cb29 100644 --- a/components/ai_chat/core/common/BUILD.gn +++ b/components/ai_chat/core/common/BUILD.gn @@ -21,3 +21,18 @@ static_library("common") { "//components/prefs", ] } + +if (!is_ios) { + source_set("unit_tests") { + testonly = true + sources = [ "pref_names_unittest.cc" ] + + deps = [ + "//base/test:test_support", + "//brave/components/ai_chat/core/common", + "//components/prefs:test_support", + "//content/test:test_support", + "//testing/gtest:gtest", + ] + } +} diff --git a/components/ai_chat/core/common/pref_names.cc b/components/ai_chat/core/common/pref_names.cc index a60a8e0e2d44..6e144200ad2b 100644 --- a/components/ai_chat/core/common/pref_names.cc +++ b/components/ai_chat/core/common/pref_names.cc @@ -5,6 +5,9 @@ #include "brave/components/ai_chat/core/common/pref_names.h" +#include + +#include "base/strings/string_util.h" #include "brave/components/ai_chat/core/common/features.h" #include "components/prefs/pref_registry_simple.h" #include "components/prefs/pref_service.h" @@ -38,7 +41,16 @@ void RegisterProfilePrefsForMigration(PrefRegistrySimple* registry) { void MigrateProfilePrefs(PrefService* profile_prefs) { profile_prefs->ClearPref(kObseleteBraveChatAutoGenerateQuestions); - // TODO(petemill): migrate model key from "chat-default" to "chat-basic" + // migrate model key from "chat-default" to "chat-basic" + static const std::string kDefaultModelBasicFrom = "chat-default"; + static const std::string kDefaultModelBasicTo = "chat-basic"; + if (auto* default_model_value = + profile_prefs->GetUserPrefValue(kDefaultModelKey)) { + if (base::EqualsCaseInsensitiveASCII(default_model_value->GetString(), + kDefaultModelBasicFrom)) { + profile_prefs->SetString(kDefaultModelKey, kDefaultModelBasicTo); + } + } } void RegisterLocalStatePrefs(PrefRegistrySimple* registry) { diff --git a/components/ai_chat/core/common/pref_names_unittest.cc b/components/ai_chat/core/common/pref_names_unittest.cc new file mode 100644 index 000000000000..bcdcc236210e --- /dev/null +++ b/components/ai_chat/core/common/pref_names_unittest.cc @@ -0,0 +1,33 @@ +// Copyright (c) 2024 The Brave Authors. All rights reserved. +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this file, +// You can obtain one at https://mozilla.org/MPL/2.0/. + +#include "brave/components/ai_chat/core/common/pref_names.h" + +#include + +#include "components/prefs/testing_pref_service.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace ai_chat::prefs { + +class AIChatPrefMigrationTest : public ::testing::Test { + public: + void SetUp() override { + RegisterProfilePrefs(pref_service_.registry()); + RegisterProfilePrefsForMigration(pref_service_.registry()); + } + + TestingPrefServiceSimple pref_service_; +}; + +TEST_F(AIChatPrefMigrationTest, ChangeOldDefaultKey) { + pref_service_.SetString(kDefaultModelKey, "chat-default"); + MigrateProfilePrefs(&pref_service_); + + EXPECT_EQ(pref_service_.GetUserPrefValue(kDefaultModelKey)->GetString(), + "chat-basic"); +} + +} // namespace ai_chat::prefs diff --git a/test/BUILD.gn b/test/BUILD.gn index 59489cd991ba..db4124db5d9f 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -361,6 +361,7 @@ test("brave_unit_tests") { deps += [ "//brave/browser/ai_chat:unit_tests", "//brave/components/ai_chat/core/browser:unit_tests", + "//brave/components/ai_chat/core/common:unit_tests", ] }