Skip to content

Commit

Permalink
AI Chat: migrate default model key pref from "chat-default" to "chat-…
Browse files Browse the repository at this point in the history
…basic"
  • Loading branch information
petemill committed Jan 16, 2024
1 parent 2d31274 commit b7be646
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
15 changes: 15 additions & 0 deletions components/ai_chat/core/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
}
}
14 changes: 13 additions & 1 deletion components/ai_chat/core/common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

#include "brave/components/ai_chat/core/common/pref_names.h"

#include <string>

#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"
Expand Down Expand Up @@ -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) {
Expand Down
33 changes: 33 additions & 0 deletions components/ai_chat/core/common/pref_names_unittest.cc
Original file line number Diff line number Diff line change
@@ -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 <string>

#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
1 change: 1 addition & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
}

Expand Down

0 comments on commit b7be646

Please sign in to comment.