fix: deep copy settings when duplicating an engine (#907)
This commit is contained in:
1 parent
3b3d5ed500
commit
01c5621cdb
3 files changed
+59
-5
No files matched your search
@@ -69,6 +69,14 @@ export type RemoteEngine = z.output<typeof remoteEngineSchema>;
|
||||
export const engineSchema = z.union([localEngineSchema, remoteEngineSchema]);
|
||||
export type Engine = z.output<typeof engineSchema>;
|
||||
|
||||
export function duplicateEngine(engine: LocalEngine): LocalEngine {
|
||||
return {
|
||||
...structuredClone(engine),
|
||||
id: crypto.randomUUID(),
|
||||
name: `${engine.name} (Copy)`,
|
||||
};
|
||||
}
|
||||
|
||||
export function stopEngine(engine: LocalEngine, tab: string): Promise<void> {
|
||||
return commands.stopEngine(engine.id, tab).then((r) => {
|
||||
unwrap(r);
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import { expect, test } from "vitest";
|
||||
import { duplicateEngine, type LocalEngine } from "../engines";
|
||||
|
||||
const stockfish: () => LocalEngine = () => ({
|
||||
type: "local",
|
||||
id: "stockfish",
|
||||
name: "Stockfish",
|
||||
version: "17",
|
||||
path: "/usr/bin/stockfish",
|
||||
go: { t: "Depth", c: 20 },
|
||||
settings: [
|
||||
{ name: "MultiPV", value: 1 },
|
||||
{ name: "Threads", value: 4 },
|
||||
{ name: "Hash", value: 16 },
|
||||
],
|
||||
});
|
||||
|
||||
test("duplicateEngine gives the copy a new id and name", () => {
|
||||
const original = stockfish();
|
||||
const copy = duplicateEngine(original);
|
||||
|
||||
expect(copy.id).not.toBe(original.id);
|
||||
expect(copy.name).toBe("Stockfish (Copy)");
|
||||
expect(copy.settings).toEqual(original.settings);
|
||||
expect(copy.go).toEqual(original.go);
|
||||
});
|
||||
|
||||
test("duplicateEngine does not share settings with the original", () => {
|
||||
const original = stockfish();
|
||||
const copy = duplicateEngine(original);
|
||||
|
||||
copy.settings!.find((s) => s.name === "Threads")!.value = 8;
|
||||
copy.settings!.push({ name: "Ponder", value: true });
|
||||
|
||||
expect(original.settings).toEqual([
|
||||
{ name: "MultiPV", value: 1 },
|
||||
{ name: "Threads", value: 4 },
|
||||
{ name: "Hash", value: 16 },
|
||||
]);
|
||||
});
|
||||
|
||||
test("duplicateEngine does not share the go mode with the original", () => {
|
||||
const original = stockfish();
|
||||
const copy = duplicateEngine(original);
|
||||
|
||||
(copy.go as { c: number }).c = 30;
|
||||
|
||||
expect(original.go).toEqual({ t: "Depth", c: 20 });
|
||||
});
|
||||
Reference in new issue
Block a user