59 lines
2.7 KiB
TypeScript
59 lines
2.7 KiB
TypeScript
import Database from 'better-sqlite3';
|
|
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
import { defaultDiscordSettings } from '../../src/lib/discord.js';
|
|
import { migrateDiscord } from '../../src/lib/server/discord/schema.js';
|
|
import { migrateDiscordPublicationDefaults } from '../../src/lib/server/discord/publication-defaults.js';
|
|
import { DiscordStore } from '../../src/lib/server/discord/store.js';
|
|
|
|
let handle: Database.Database;
|
|
let store: DiscordStore;
|
|
beforeEach(() => {
|
|
handle = new Database(':memory:');
|
|
migrateDiscord(handle);
|
|
handle.exec(`
|
|
CREATE TABLE servers (name TEXT PRIMARY KEY, destroyed_at TEXT);
|
|
INSERT INTO servers VALUES ('kino', NULL), ('town', NULL), ('retired', '2026-10-18');
|
|
`);
|
|
store = new DiscordStore(handle);
|
|
});
|
|
afterEach(() => handle.close());
|
|
|
|
function legacySettings(publishedServers: string[]) {
|
|
const value: Record<string, unknown> = { ...defaultDiscordSettings, publishedServers };
|
|
delete value.excludedServers;
|
|
handle
|
|
.prepare('INSERT INTO discord_configuration(id,value) VALUES(1,?)')
|
|
.run(JSON.stringify(value));
|
|
}
|
|
|
|
describe('Discord publication defaults upgrade', () => {
|
|
it('includes all servers on new installations without creating configuration', () => {
|
|
migrateDiscordPublicationDefaults(handle);
|
|
expect(store.settings()).toEqual(defaultDiscordSettings);
|
|
expect(store.settings().excludedServers).toEqual([]);
|
|
expect(handle.prepare('SELECT * FROM discord_configuration').all()).toEqual([]);
|
|
});
|
|
|
|
it('replaces the previously empty default selection without enabling Discord or optional sharing', () => {
|
|
legacySettings([]);
|
|
migrateDiscordPublicationDefaults(handle);
|
|
expect(store.settings()).toEqual(defaultDiscordSettings);
|
|
const value = (
|
|
handle.prepare('SELECT value FROM discord_configuration').get() as { value: string }
|
|
).value;
|
|
expect(JSON.parse(value)).not.toHaveProperty('publishedServers');
|
|
});
|
|
|
|
it('preserves existing nonempty selections as exclusions, and retains later opt-outs on replay', () => {
|
|
legacySettings(['kino']);
|
|
migrateDiscordPublicationDefaults(handle);
|
|
expect(store.settings().excludedServers).toEqual(['town']);
|
|
handle.exec("INSERT INTO servers VALUES ('moon', NULL)");
|
|
expect(store.settings().excludedServers).not.toContain('moon');
|
|
store.saveSettings({ ...store.settings(), excludedServers: ['kino', 'town'] });
|
|
const before = handle.prepare('SELECT * FROM discord_configuration').all();
|
|
migrateDiscordPublicationDefaults(handle);
|
|
expect(handle.prepare('SELECT * FROM discord_configuration').all()).toEqual(before);
|
|
});
|
|
});
|