Add legacy color code support in NameFormatService and implement unit tests for colored name parsing.

This commit is contained in:
akastijn 2026-08-01 22:52:50 +02:00
parent 5be3072b26
commit 819739ff60
4 changed files with 68 additions and 11 deletions

View File

@ -0,0 +1,37 @@
import {TestBed} from '@angular/core/testing';
import {NameFormatService} from './name-format.service';
import {ChatService} from './chat.service';
describe('NameFormatService', () => {
let service: NameFormatService;
let chatServiceSpy: jasmine.SpyObj<ChatService>;
beforeEach(() => {
chatServiceSpy = jasmine.createSpyObj('ChatService', ['partieMap', 'userNameMap']);
TestBed.configureTestingModule({
providers: [
NameFormatService,
{provide: ChatService, useValue: chatServiceSpy}
]
});
service = TestBed.inject(NameFormatService);
});
it('should parse legacy color codes', () => {
const input = '&4Hello &cWorld';
const result = (service as any).parseColoredName(input);
expect(result).toEqual([
{text: 'Hello ', color: '#AA0000'},
{text: 'World', color: '#FF5555'}
]);
});
it('should parse mixed color codes', () => {
const input = '{#AA0000<>}Hello &4World';
const result = (service as any).parseColoredName(input);
expect(result).toEqual([
{text: 'Hello ', color: '#AA0000'},
{text: 'World', color: '#AA0000'}
]);
});
});

View File

@ -12,6 +12,25 @@ export class NameFormatService {
return this.parseColoredName(this.getNameFromType(channel)); return this.parseColoredName(this.getNameFromType(channel));
} }
private readonly LEGACY_COLOR_MAP: Record<string, string> = {
'4': '#AA0000',
'c': '#FF5555',
'6': '#FFAA00',
'e': '#FFFF55',
'2': '#00AA00',
'a': '#55FF55',
'b': '#55FFFF',
'3': '#00AAAA',
'1': '#0000AA',
'9': '#5555FF',
'd': '#FF55FF',
'5': '#AA00AA',
'f': '#FFFFFF',
'7': '#AAAAAA',
'8': '#555555',
'0': '#000000'
};
private getNameFromType(channel: ChatChannel): string { private getNameFromType(channel: ChatChannel): string {
if (channel.type === 'PARTY') { if (channel.type === 'PARTY') {
return this.chatService.partieMap().get(channel.name) ?? channel.name; return this.chatService.partieMap().get(channel.name) ?? channel.name;
@ -23,7 +42,7 @@ export class NameFormatService {
} }
private parseColoredName(name: string): ColoredNameSegment[] { private parseColoredName(name: string): ColoredNameSegment[] {
const markerRegex = /\{#([0-9a-fA-F]{6})(<>|[<>]?)}/g; const markerRegex = /\{#([0-9a-fA-F]{6})(<>|[<>]?)\}|&([0-9a-fA-F])/g;
const segments: ColoredNameSegment[] = []; const segments: ColoredNameSegment[] = [];
let currentColor: string | undefined; let currentColor: string | undefined;
let gradientStartColor: string | undefined; let gradientStartColor: string | undefined;
@ -34,8 +53,9 @@ export class NameFormatService {
while ((match = markerRegex.exec(name)) !== null) { while ((match = markerRegex.exec(name)) !== null) {
this.addColoredTextSegment(segments, name.slice(lastIndex, match.index), currentColor); this.addColoredTextSegment(segments, name.slice(lastIndex, match.index), currentColor);
const markerColor = `#${match[1]}`; const isLegacy = !!match[3];
const markerType = match[2]; const markerColor = isLegacy ? this.LEGACY_COLOR_MAP[match[3]] : `#${match[1]}`;
const markerType = isLegacy ? undefined : match[2];
if (markerType === '>') { if (markerType === '>') {
currentColor = markerColor; currentColor = markerColor;

View File

@ -6,7 +6,7 @@ export function getServerColor(server: string) {
} else if (server === 'creative') { } else if (server === 'creative') {
return "#FFA500"; return "#FFA500";
} }
return "#0290e8"; return "#8a9197";
} }
export function getServerTextColor(server: string) { export function getServerTextColor(server: string) {

View File

@ -1,18 +1,18 @@
import {ComponentFixture, TestBed} from '@angular/core/testing'; import {ComponentFixture, TestBed} from '@angular/core/testing';
import { LoginComponent } from './login.component'; import {LoginDialogComponent} from './login.component';
describe('LoginComponent', () => { describe('LoginComponent', () => {
let component: LoginComponent; let component: LoginDialogComponent;
let fixture: ComponentFixture<LoginComponent>; let fixture: ComponentFixture<LoginDialogComponent>;
beforeEach(async () => { beforeEach(async () => {
await TestBed.configureTestingModule({ await TestBed.configureTestingModule({
imports: [LoginComponent] imports: [LoginDialogComponent]
}) })
.compileComponents(); .compileComponents();
fixture = TestBed.createComponent(LoginComponent); fixture = TestBed.createComponent(LoginDialogComponent);
component = fixture.componentInstance; component = fixture.componentInstance;
fixture.detectChanges(); fixture.detectChanges();
}); });