refactor: jest test

This commit is contained in:
手瓜一十雪
2024-07-22 10:24:55 +08:00
parent 24d3b52e0b
commit 0522ba35fe
12 changed files with 137 additions and 92 deletions

View File

@@ -1,63 +1,30 @@
import { Peer } from '@/core';
import crypto from 'crypto';
class CircularQueue<T> {
private items: T[] = [];
private front: number = 0;
private maxSize: number;
constructor(maxSize: number) {
this.maxSize = maxSize;
}
enqueue(item: T): void {
if (this.items.length == this.maxSize) {
this.front = (this.front + 1) % this.maxSize;
}
this.items[(this.front + this.items.length) % this.maxSize] = item;
}
dequeue(): T | undefined {
return this.items.length == 0 ? undefined : this.items[(this.front + 1) % this.items.length];
}
size(): number {
return this.items.length;
}
}
class LimitedHashTable<K, V> {
private keyToValue: Map<K, V> = new Map();
private valueToKey: Map<V, K> = new Map();
private keyQueue: CircularQueue<K> = new CircularQueue<K>(0);
private valueQueue: CircularQueue<V> = new CircularQueue<V>(0);
private maxSize: number;
constructor(maxSize: number) {
this.maxSize = maxSize;
}
private removeFromQueues(key: K, value: V): void {
this.keyQueue.dequeue();
this.valueQueue.dequeue();
}
set(key: K, value: V): void {
let isExist = this.keyToValue.get(key);
const isExist = this.keyToValue.get(key);
if (isExist && isExist === value) {
return;
}
this.keyToValue.set(key, value);
this.valueToKey.set(value, key);
this.keyQueue.enqueue(key);
this.valueQueue.enqueue(value);
if (this.keyQueue.size() > this.maxSize || this.valueQueue.size() > this.maxSize) {
const removedKey = this.keyQueue.dequeue();
const removedValue = this.valueQueue.dequeue();
if (removedKey !== undefined && removedValue !== undefined) {
this.keyToValue.delete(removedKey);
this.valueToKey.delete(removedValue);
this.removeFromQueues(removedKey, removedValue);
}
while (this.keyToValue.size !== this.valueToKey.size){
console.log('keyToValue.size !== valueToKey.size');
}
while (this.keyToValue.size > this.maxSize || this.valueToKey.size > this.maxSize) {
//删除旧的值
const oldestKey = this.keyToValue.keys().next().value;
this.keyToValue.delete(oldestKey);
this.valueToKey.delete(oldestKey);
}
}
@@ -69,12 +36,11 @@ class LimitedHashTable<K, V> {
return this.valueToKey.get(value);
}
deleteByValue(value: V) {
deleteByValue(value: V): void {
const key = this.valueToKey.get(value);
if (key !== undefined) {
this.keyToValue.delete(key);
this.valueToKey.delete(value);
this.removeFromQueues(key, value);
}
}
@@ -83,41 +49,47 @@ class LimitedHashTable<K, V> {
if (value !== undefined) {
this.keyToValue.delete(key);
this.valueToKey.delete(value);
this.removeFromQueues(key, value);
}
}
}
class MessageUniqueWrapper {
private msgIdMap: LimitedHashTable<string, number>;
constructor(MaxMap: number = 1000) {
this.msgIdMap = new LimitedHashTable<string, number>(MaxMap);
constructor(maxMap: number = 1000) {
this.msgIdMap = new LimitedHashTable<string, number>(maxMap);
}
createMsg(MsgId: string) {
const hash = crypto.createHash('sha256');
hash.update(MsgId);
const ShortId = parseInt(hash.digest('hex').slice(0, 8), 16);
let isExist = this.msgIdMap.getKey(ShortId)
if (isExist && isExist === MsgId) {
return true;
createMsg(peer: Peer, msgId: string): number | undefined {
const key = `${msgId}|${peer.chatType}|${peer.peerUid}`;
const hash = crypto.createHash('sha1').update(key);
const shortId = parseInt(hash.digest('hex').slice(0, 8), 16);
const isExist = this.msgIdMap.getKey(shortId);
if (isExist && isExist === msgId) {
return undefined;
}
return this.msgIdMap.set(MsgId, ShortId);
this.msgIdMap.set(key, shortId);
return shortId;
}
getMsgIdByShortId(ShortId: number) {
return this.msgIdMap.getKey(ShortId);
getMsgIdAndPeerByShortId(shortId: number): { MsgId: string; Peer: Peer } | undefined {
const data = this.msgIdMap.getKey(shortId);
if (data) {
const [msgId, chatTypeStr, peerUid] = data.split('|');
const peer: Peer = {
chatType: parseInt(chatTypeStr),
peerUid,
guildId: '',
};
return { MsgId: msgId, Peer: peer };
}
return undefined;
}
getShortIdByMsgId(MsgId: string) {
return this.msgIdMap.getValue(MsgId);
}
CreateMsgIdList(MsgIdList: string[]) {
return MsgIdList.map((MsgId) => {
return this.createMsg(MsgId);
});
getShortIdByMsgId(msgId: string): number | undefined {
return this.msgIdMap.getValue(msgId);
}
}
export const MessageUnique = new MessageUniqueWrapper();
export const MessageUnique = new MessageUniqueWrapper();