settingregistry.spec.ts 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. DefaultSchemaValidator,
  5. ISettingRegistry,
  6. SettingRegistry,
  7. Settings
  8. } from '@jupyterlab/settingregistry';
  9. import { StateDB } from '@jupyterlab/statedb';
  10. import { signalToPromise } from '@jupyterlab/testutils';
  11. import { JSONObject } from '@lumino/coreutils';
  12. class TestConnector extends StateDB {
  13. schemas: { [key: string]: ISettingRegistry.ISchema } = {};
  14. async fetch(id: string): Promise<ISettingRegistry.IPlugin | undefined> {
  15. const fetched = await super.fetch(id);
  16. if (!fetched && !this.schemas[id]) {
  17. return undefined;
  18. }
  19. const schema: ISettingRegistry.ISchema = this.schemas[id] || {
  20. type: 'object'
  21. };
  22. const composite = {};
  23. const user = {};
  24. const raw = (fetched as string) || '{ }';
  25. const version = 'test';
  26. return { id, data: { composite, user }, raw, schema, version };
  27. }
  28. async list(): Promise<any> {
  29. return Promise.reject('list method not implemented');
  30. }
  31. }
  32. describe('@jupyterlab/settingregistry', () => {
  33. describe('DefaultSchemaValidator', () => {
  34. describe('#constructor()', () => {
  35. it('should create a new schema validator', () => {
  36. const validator = new DefaultSchemaValidator();
  37. expect(validator).toBeInstanceOf(DefaultSchemaValidator);
  38. });
  39. });
  40. describe('#validateData()', () => {
  41. it('should validate data against a schema', () => {
  42. const id = 'foo';
  43. const validator = new DefaultSchemaValidator();
  44. const schema: ISettingRegistry.ISchema = {
  45. additionalProperties: false,
  46. properties: {
  47. bar: { type: 'string' }
  48. },
  49. type: 'object'
  50. };
  51. const composite = {};
  52. const user = {};
  53. const raw = '{ "bar": "baz" }';
  54. const version = 'test';
  55. const plugin = { id, data: { composite, user }, raw, schema, version };
  56. const errors = validator.validateData(plugin);
  57. expect(errors).toBe(null);
  58. });
  59. it('should return errors if the data fails to validate', () => {
  60. const id = 'foo';
  61. const validator = new DefaultSchemaValidator();
  62. const schema: ISettingRegistry.ISchema = {
  63. additionalProperties: false,
  64. properties: {
  65. bar: { type: 'string' }
  66. },
  67. type: 'object'
  68. };
  69. const composite = {};
  70. const user = {};
  71. const raw = '{ "baz": "qux" }';
  72. const version = 'test';
  73. const plugin = { id, data: { composite, user }, raw, schema, version };
  74. const errors = validator.validateData(plugin);
  75. expect(errors).not.toBe(null);
  76. });
  77. it('should populate the composite data', () => {
  78. const id = 'foo';
  79. const validator = new DefaultSchemaValidator();
  80. const schema: ISettingRegistry.ISchema = {
  81. additionalProperties: false,
  82. properties: {
  83. bar: { type: 'string', default: 'baz' }
  84. },
  85. type: 'object'
  86. };
  87. const composite = {} as JSONObject;
  88. const user = {} as JSONObject;
  89. const raw = '{ }';
  90. const version = 'test';
  91. const plugin = { id, data: { composite, user }, raw, schema, version };
  92. const errors = validator.validateData(plugin);
  93. expect(errors).toBe(null);
  94. expect(plugin.data.user.bar).toBeUndefined();
  95. expect(plugin.data.composite.bar).toBe(schema.properties!.bar.default);
  96. });
  97. });
  98. });
  99. describe('SettingRegistry', () => {
  100. const connector = new TestConnector();
  101. const timeout = 500;
  102. let registry: SettingRegistry;
  103. afterEach(() => {
  104. connector.schemas = {};
  105. return connector.clear();
  106. });
  107. beforeEach(() => {
  108. registry = new SettingRegistry({ connector, timeout });
  109. });
  110. describe('#constructor()', () => {
  111. it('should create a new setting registry', () => {
  112. expect(registry).toBeInstanceOf(SettingRegistry);
  113. });
  114. });
  115. describe('#pluginChanged', () => {
  116. it('should emit when a plugin changes', async () => {
  117. const id = 'foo';
  118. const key = 'bar';
  119. const value = 'baz';
  120. connector.schemas[id] = { type: 'object' };
  121. let called = false;
  122. registry.pluginChanged.connect((sender: any, plugin: string) => {
  123. expect(id).toBe(plugin);
  124. called = true;
  125. });
  126. await registry.load(id);
  127. await registry.set(id, key, value);
  128. expect(called).toBe(true);
  129. });
  130. });
  131. describe('#plugins', () => {
  132. it('should return a list of registered plugins in registry', async () => {
  133. const one = 'foo';
  134. const two = 'bar';
  135. expect(Object.keys(registry.plugins)).toHaveLength(0);
  136. connector.schemas[one] = { type: 'object' };
  137. connector.schemas[two] = { type: 'object' };
  138. await registry.load(one);
  139. expect(Object.keys(registry.plugins)).toHaveLength(1);
  140. await registry.load(two);
  141. expect(Object.keys(registry.plugins)).toHaveLength(2);
  142. });
  143. });
  144. describe('#get()', () => {
  145. it('should get a setting item from a loaded plugin', async () => {
  146. const id = 'foo';
  147. const key = 'bar';
  148. const value = 'baz';
  149. connector.schemas[id] = { type: 'object' };
  150. await connector.save(id, JSON.stringify({ [key]: value }));
  151. (await registry.load(id)) as Settings;
  152. const saved = await registry.get(id, key);
  153. expect(saved.user).toBe(value);
  154. });
  155. it('should get a setting item from a plugin that is not loaded', async () => {
  156. const id = 'alpha';
  157. const key = 'beta';
  158. const value = 'gamma';
  159. connector.schemas[id] = { type: 'object' };
  160. await connector.save(id, JSON.stringify({ [key]: value }));
  161. const saved = await registry.get(id, key);
  162. expect(saved.composite).toBe(value);
  163. });
  164. it('should use schema default if user data not available', async () => {
  165. const id = 'alpha';
  166. const key = 'beta';
  167. const value = 'gamma';
  168. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  169. type: 'object',
  170. properties: {
  171. [key]: {
  172. type: typeof value as ISettingRegistry.Primitive,
  173. default: value
  174. }
  175. }
  176. });
  177. const saved = await registry.get(id, key);
  178. expect(saved.composite).toBe(schema.properties![key].default);
  179. expect(saved.composite).not.toBe(saved.user);
  180. });
  181. it('should let user value override schema default', async () => {
  182. const id = 'alpha';
  183. const key = 'beta';
  184. const value = 'gamma';
  185. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  186. type: 'object',
  187. properties: {
  188. [key]: {
  189. type: typeof value as ISettingRegistry.Primitive,
  190. default: 'delta'
  191. }
  192. }
  193. });
  194. await connector.save(id, JSON.stringify({ [key]: value }));
  195. const saved = await registry.get(id, key);
  196. expect(saved.composite).toBe(value);
  197. expect(saved.user).toBe(value);
  198. expect(saved.composite).not.toBe(schema.properties![key].default);
  199. expect(saved.user).not.toBe(schema.properties![key].default);
  200. });
  201. it('should reject if a plugin does not exist', async () => {
  202. let failed = false;
  203. try {
  204. await registry.get('foo', 'bar');
  205. } catch (e) {
  206. failed = true;
  207. }
  208. expect(failed).toBe(true);
  209. });
  210. it('should resolve `undefined` if a key does not exist', async () => {
  211. const id = 'foo';
  212. const key = 'bar';
  213. connector.schemas[id] = { type: 'object' };
  214. const saved = await registry.get(id, key);
  215. expect(saved.composite).toBeUndefined();
  216. expect(saved.user).toBeUndefined();
  217. });
  218. });
  219. describe('#load()', () => {
  220. it(`should resolve a registered plugin's settings`, async () => {
  221. const id = 'foo';
  222. expect(Object.keys(registry.plugins)).toHaveLength(0);
  223. connector.schemas[id] = { type: 'object' };
  224. const settings = (await registry.load(id)) as Settings;
  225. expect(settings.id).toBe(id);
  226. });
  227. it(`should reject if a plugin transformation times out`, async () => {
  228. const id = 'foo';
  229. let failed = false;
  230. connector.schemas[id] = {
  231. 'jupyter.lab.transform': true,
  232. type: 'object'
  233. };
  234. try {
  235. await registry.load(id);
  236. } catch (e) {
  237. failed = true;
  238. }
  239. expect(failed).toBe(true);
  240. });
  241. it('should reject if a plugin does not exist', async () => {
  242. let failed = false;
  243. try {
  244. await registry.load('foo');
  245. } catch (e) {
  246. failed = true;
  247. }
  248. expect(failed).toBe(true);
  249. });
  250. });
  251. describe('#reload()', () => {
  252. it(`should load a registered plugin's settings`, async () => {
  253. const id = 'foo';
  254. expect(Object.keys(registry.plugins)).toHaveLength(0);
  255. connector.schemas[id] = { type: 'object' };
  256. const settings = await registry.reload(id);
  257. expect(settings.id).toBe(id);
  258. });
  259. it(`should replace a registered plugin's settings`, async () => {
  260. const id = 'foo';
  261. const first = 'Foo';
  262. const second = 'Bar';
  263. expect(Object.keys(registry.plugins)).toHaveLength(0);
  264. connector.schemas[id] = { type: 'object', title: first };
  265. let settings = await registry.reload(id);
  266. expect(settings.schema.title).toBe(first);
  267. await Promise.resolve(undefined);
  268. connector.schemas[id].title = second;
  269. settings = await registry.reload(id);
  270. expect(settings.schema.title).toBe(second);
  271. });
  272. it('should reject if a plugin does not exist', async () => {
  273. let failed = false;
  274. try {
  275. await registry.reload('foo');
  276. } catch (e) {
  277. failed = true;
  278. }
  279. expect(failed).toBe(true);
  280. });
  281. });
  282. describe('#transform()', () => {
  283. it(`should transform a plugin during the fetch phase`, async () => {
  284. const id = 'foo';
  285. const version = 'transform-test';
  286. expect(Object.keys(registry.plugins)).toHaveLength(0);
  287. connector.schemas[id] = {
  288. 'jupyter.lab.transform': true,
  289. type: 'object'
  290. };
  291. registry.transform(id, {
  292. fetch: plugin => {
  293. plugin.version = version;
  294. return plugin;
  295. }
  296. });
  297. expect((await registry.load(id)).version).toBe(version);
  298. });
  299. it(`should transform a plugin during the compose phase`, async () => {
  300. const id = 'foo';
  301. const composite = { a: 1 };
  302. expect(Object.keys(registry.plugins)).toHaveLength(0);
  303. connector.schemas[id] = {
  304. 'jupyter.lab.transform': true,
  305. type: 'object'
  306. };
  307. registry.transform(id, {
  308. compose: plugin => {
  309. plugin.data = { user: plugin.data.user, composite };
  310. return plugin;
  311. }
  312. });
  313. expect((await registry.load(id)).composite).toEqual(composite);
  314. });
  315. it(`should disallow a transform that changes the plugin ID`, async () => {
  316. const id = 'foo';
  317. let failed = false;
  318. expect(Object.keys(registry.plugins)).toHaveLength(0);
  319. connector.schemas[id] = {
  320. 'jupyter.lab.transform': true,
  321. type: 'object'
  322. };
  323. registry.transform(id, {
  324. compose: plugin => {
  325. plugin.id = 'bar';
  326. return plugin;
  327. }
  328. });
  329. try {
  330. await registry.load(id);
  331. } catch (e) {
  332. failed = true;
  333. }
  334. expect(failed).toBe(true);
  335. });
  336. });
  337. });
  338. describe('reconcileMenus', () => {
  339. it('should merge menu tree', () => {
  340. const a: ISettingRegistry.IMenu[] = [
  341. {
  342. id: '1',
  343. items: [{ command: 'a' }]
  344. },
  345. {
  346. id: '2',
  347. items: [{ command: 'b' }]
  348. },
  349. {
  350. id: '4',
  351. items: [
  352. {
  353. type: 'submenu',
  354. submenu: {
  355. id: 'sub',
  356. items: [{ command: 'sub-1' }]
  357. }
  358. }
  359. ]
  360. }
  361. ];
  362. const b: ISettingRegistry.IMenu[] = [
  363. {
  364. id: '2',
  365. items: [
  366. { command: 'b', disabled: true },
  367. { command: 'b', args: { input: 'hello' } },
  368. { command: 'b', args: { input: 'world' } },
  369. { command: 'c' }
  370. ]
  371. },
  372. {
  373. id: '3',
  374. items: [{ command: 'd' }]
  375. },
  376. {
  377. id: '4',
  378. items: [
  379. {
  380. type: 'submenu',
  381. submenu: {
  382. id: 'sub',
  383. items: [
  384. { command: 'sub-1', disabled: true },
  385. { command: 'sub-2' }
  386. ]
  387. }
  388. }
  389. ]
  390. }
  391. ];
  392. const merged = SettingRegistry.reconcileMenus(a, b);
  393. expect(merged).toHaveLength(4);
  394. expect(merged[0].id).toEqual('1');
  395. expect(merged[0].items).toHaveLength(1);
  396. expect(merged[1].id).toEqual('2');
  397. expect(merged[1].items).toHaveLength(4);
  398. expect(merged[1].items![0].command).toEqual('b');
  399. expect(merged[1].items![0].args).toBeUndefined();
  400. expect(merged[1].items![0].disabled).toEqual(true);
  401. expect(merged[1].items![1].command).toEqual('b');
  402. expect(merged[1].items![1].args?.input).toEqual('hello');
  403. expect(merged[1].items![2].command).toEqual('b');
  404. expect(merged[1].items![2].args?.input).toEqual('world');
  405. expect(merged[2].id).toEqual('4');
  406. expect(merged[2].items).toHaveLength(1);
  407. expect(merged[2].items![0].submenu?.items).toHaveLength(2);
  408. expect(merged[2].items![0].submenu?.items![0].command).toEqual('sub-1');
  409. expect(merged[2].items![0].submenu?.items![0].disabled).toEqual(true);
  410. expect(merged[3].id).toEqual('3');
  411. expect(merged[3].items).toHaveLength(1);
  412. });
  413. it('should merge menu tree without adding new items', () => {
  414. const a: ISettingRegistry.IMenu[] = [
  415. {
  416. id: '1',
  417. items: [{ command: 'a' }]
  418. },
  419. {
  420. id: '2',
  421. items: [{ command: 'b' }]
  422. },
  423. {
  424. id: '4',
  425. items: [
  426. {
  427. type: 'submenu',
  428. submenu: {
  429. id: 'sub',
  430. items: [{ command: 'sub-1' }]
  431. }
  432. }
  433. ]
  434. }
  435. ];
  436. const b: ISettingRegistry.IMenu[] = [
  437. {
  438. id: '2',
  439. items: [
  440. { command: 'b', disabled: true },
  441. { command: 'b', args: { input: 'hello' } },
  442. { command: 'b', args: { input: 'world' } },
  443. { command: 'c' }
  444. ]
  445. },
  446. {
  447. id: '3',
  448. items: [{ command: 'd' }]
  449. },
  450. {
  451. id: '4',
  452. items: [
  453. {
  454. type: 'submenu',
  455. submenu: {
  456. id: 'sub',
  457. items: [
  458. { command: 'sub-1', disabled: true },
  459. { command: 'sub-2' }
  460. ]
  461. }
  462. }
  463. ],
  464. disabled: true
  465. }
  466. ];
  467. const merged = SettingRegistry.reconcileMenus(a, b, false, false);
  468. expect(merged).toHaveLength(3);
  469. expect(merged![0].id).toEqual('1');
  470. expect(merged![0].items).toHaveLength(1);
  471. expect(merged![1].id).toEqual('2');
  472. expect(merged![1].items).toHaveLength(1);
  473. expect(merged[1].items![0].command).toEqual('b');
  474. expect(merged[1].items![0].args).toBeUndefined();
  475. expect(merged[1].items![0].disabled).toEqual(true);
  476. expect(merged[2].id).toEqual('4');
  477. expect(merged[2].items).toHaveLength(1);
  478. expect(merged[2].items![0].submenu?.items).toHaveLength(1);
  479. expect(merged[2].items![0].submenu?.items![0].command).toEqual('sub-1');
  480. expect(merged[2].items![0].submenu?.items![0].disabled).toEqual(true);
  481. expect(merged[2].disabled).toEqual(true);
  482. });
  483. });
  484. describe('reconcileItems', () => {
  485. it('should merge items list', () => {
  486. const a: ISettingRegistry.IContextMenuItem[] = [
  487. { command: 'a', selector: '.a' },
  488. { command: 'b', selector: '.b' },
  489. {
  490. type: 'submenu',
  491. submenu: {
  492. id: 'sub',
  493. items: [{ command: 'sub-1' }]
  494. },
  495. selector: '.sub'
  496. }
  497. ];
  498. const b: ISettingRegistry.IContextMenuItem[] = [
  499. { command: 'b', selector: '.b', disabled: true },
  500. { command: 'b', selector: '.bb' },
  501. { command: 'b', selector: '.b1', args: { input: 'hello' } },
  502. { command: 'b', selector: '.b2', args: { input: 'world' } },
  503. { command: 'c', selector: '.c' },
  504. { command: 'd', selector: '.d' },
  505. {
  506. type: 'submenu',
  507. submenu: {
  508. id: 'sub',
  509. items: [{ command: 'sub-1', disabled: true }, { command: 'sub-2' }]
  510. },
  511. selector: '.s'
  512. }
  513. ];
  514. const merged = SettingRegistry.reconcileItems(a, b);
  515. expect(merged).toHaveLength(8);
  516. expect(merged![1].command).toEqual('b');
  517. expect(merged![1].selector).toEqual('.b');
  518. expect(merged![1].disabled).toEqual(true);
  519. expect(merged![2].submenu?.items).toHaveLength(2);
  520. expect(merged![3].command).toEqual('b');
  521. expect(merged![3].selector).toEqual('.bb');
  522. expect(merged![4].command).toEqual('b');
  523. expect(merged![4].args?.input).toEqual('hello');
  524. expect(merged![5].command).toEqual('b');
  525. expect(merged![5].args?.input).toEqual('world');
  526. });
  527. it('should merge items list without adding new ones', () => {
  528. const a: ISettingRegistry.IContextMenuItem[] = [
  529. { command: 'a', selector: '.a' },
  530. { command: 'b', selector: '.b' },
  531. {
  532. type: 'submenu',
  533. submenu: {
  534. id: 'sub',
  535. items: [{ command: 'sub-1' }]
  536. },
  537. selector: '.sub'
  538. }
  539. ];
  540. const b: ISettingRegistry.IContextMenuItem[] = [
  541. { command: 'b', selector: '.b', disabled: true },
  542. { command: 'b', selector: '.b1', args: { input: 'hello' } },
  543. { command: 'b', selector: '.b2', args: { input: 'world' } },
  544. { command: 'c', selector: '.c' },
  545. { command: 'd', selector: '.d' },
  546. {
  547. type: 'submenu',
  548. submenu: {
  549. id: 'sub',
  550. items: [{ command: 'sub-1', disabled: true }, { command: 'sub-2' }]
  551. },
  552. selector: '.s'
  553. }
  554. ];
  555. const merged = SettingRegistry.reconcileItems(a, b, false, false);
  556. expect(merged).toHaveLength(3);
  557. expect(merged![1].command).toEqual('b');
  558. expect(merged![1].selector).toEqual('.b');
  559. expect(merged![1].disabled).toEqual(true);
  560. expect(merged![2].submenu?.items).toHaveLength(1);
  561. expect(merged![2].submenu?.items![0].command).toEqual('sub-1');
  562. expect(merged![2].submenu?.items![0].disabled).toEqual(true);
  563. });
  564. });
  565. describe('filterDisabledItems', () => {
  566. it('should remove disabled menu item', () => {
  567. const a: ISettingRegistry.IContextMenuItem[] = [
  568. { command: 'a', selector: '.a' },
  569. { type: 'separator', selector: '.a' },
  570. { command: 'b', disabled: true, selector: '.a' },
  571. {
  572. type: 'submenu',
  573. submenu: {
  574. id: 'sub',
  575. items: [{ command: 'sub-1', disabled: true }, { command: 'sub-2' }]
  576. },
  577. selector: '.s'
  578. }
  579. ];
  580. const filtered = SettingRegistry.filterDisabledItems(a);
  581. expect(filtered).toHaveLength(3);
  582. expect(filtered[0]?.command).toEqual('a');
  583. expect(filtered[1]?.type).toEqual('separator');
  584. expect(filtered[2]?.type).toEqual('submenu');
  585. expect(filtered[2]?.submenu?.items).toHaveLength(1);
  586. expect(filtered[2]?.submenu?.items![0].command).toEqual('sub-2');
  587. });
  588. });
  589. describe('Settings', () => {
  590. const connector = new TestConnector();
  591. let registry: SettingRegistry;
  592. let settings: Settings | null;
  593. afterEach(() => {
  594. if (settings) {
  595. settings.dispose();
  596. settings = null;
  597. }
  598. connector.schemas = {};
  599. return connector.clear();
  600. });
  601. beforeEach(() => {
  602. registry = new SettingRegistry({ connector });
  603. });
  604. describe('#constructor()', () => {
  605. it('should create a new settings object for a plugin', () => {
  606. const id = 'alpha';
  607. const data = { composite: {}, user: {} };
  608. const schema: ISettingRegistry.ISchema = { type: 'object' };
  609. const raw = '{ }';
  610. const version = 'test';
  611. const plugin = { id, data, raw, schema, version };
  612. settings = new Settings({ plugin, registry });
  613. expect(settings).toBeInstanceOf(Settings);
  614. });
  615. });
  616. describe('#changed', () => {
  617. it('should emit when a plugin changes', async () => {
  618. const id = 'alpha';
  619. const schema: ISettingRegistry.ISchema = { type: 'object' };
  620. connector.schemas[id] = schema;
  621. settings = (await registry.load(id)) as Settings;
  622. const promise = signalToPromise(settings.changed);
  623. await settings.set('foo', 'bar');
  624. await expect(promise).resolves.toContain(settings);
  625. });
  626. });
  627. describe('#composite', () => {
  628. it('should contain the merged user and default data', async () => {
  629. const id = 'alpha';
  630. const key = 'beta';
  631. const value = 'gamma';
  632. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  633. type: 'object',
  634. properties: {
  635. [key]: {
  636. type: typeof value as ISettingRegistry.Primitive,
  637. default: value
  638. }
  639. }
  640. });
  641. connector.schemas[id] = schema;
  642. settings = (await registry.load(id)) as Settings;
  643. expect(settings.composite[key]).toBe(value);
  644. });
  645. it('should privilege user data', async () => {
  646. const id = 'alpha';
  647. const key = 'beta';
  648. const value = 'gamma';
  649. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  650. type: 'object',
  651. properties: {
  652. [key]: {
  653. type: typeof value as ISettingRegistry.Primitive,
  654. default: 'delta'
  655. }
  656. }
  657. });
  658. connector.schemas[id] = schema;
  659. settings = (await registry.load(id)) as Settings;
  660. await settings.set(key, value);
  661. expect(settings.composite[key]).toBe(value);
  662. });
  663. });
  664. describe('#id', () => {
  665. it('should expose the plugin ID', () => {
  666. const id = 'alpha';
  667. const data = { composite: {}, user: {} };
  668. const schema: ISettingRegistry.ISchema = { type: 'object' };
  669. const raw = '{ }';
  670. const version = 'test';
  671. const plugin = { id, data, raw, schema, version };
  672. settings = new Settings({ plugin, registry });
  673. expect(settings.id).toBe(id);
  674. });
  675. });
  676. describe('#isDisposed', () => {
  677. it('should test whether the settings object is disposed', () => {
  678. const id = 'alpha';
  679. const data = { composite: {}, user: {} };
  680. const schema: ISettingRegistry.ISchema = { type: 'object' };
  681. const raw = '{ }';
  682. const version = 'test';
  683. const plugin = { id, data, raw, schema, version };
  684. settings = new Settings({ plugin, registry });
  685. expect(settings.isDisposed).toBe(false);
  686. settings.dispose();
  687. expect(settings.isDisposed).toBe(true);
  688. });
  689. });
  690. describe('#schema', () => {
  691. it('should expose the plugin schema', async () => {
  692. const id = 'alpha';
  693. const schema: ISettingRegistry.ISchema = { type: 'object' };
  694. connector.schemas[id] = schema;
  695. settings = (await registry.load(id)) as Settings;
  696. expect(settings.schema).toEqual(schema);
  697. });
  698. });
  699. describe('#user', () => {
  700. it('should privilege user data', async () => {
  701. const id = 'alpha';
  702. const key = 'beta';
  703. const value = 'gamma';
  704. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  705. type: 'object',
  706. properties: {
  707. [key]: {
  708. type: typeof value as ISettingRegistry.Primitive,
  709. default: 'delta'
  710. }
  711. }
  712. });
  713. connector.schemas[id] = schema;
  714. settings = (await registry.load(id)) as Settings;
  715. await settings.set(key, value);
  716. expect(settings.user[key]).toBe(value);
  717. });
  718. });
  719. describe('#registry', () => {
  720. it('should expose the setting registry', () => {
  721. const id = 'alpha';
  722. const data = { composite: {}, user: {} };
  723. const schema: ISettingRegistry.ISchema = { type: 'object' };
  724. const raw = '{ }';
  725. const version = 'test';
  726. const plugin = { id, data, raw, schema, version };
  727. settings = new Settings({ plugin, registry });
  728. expect(settings.registry).toBe(registry);
  729. });
  730. });
  731. describe('#dispose()', () => {
  732. it('should dispose the settings object', () => {
  733. const id = 'alpha';
  734. const data = { composite: {}, user: {} };
  735. const schema: ISettingRegistry.ISchema = { type: 'object' };
  736. const raw = '{ }';
  737. const version = 'test';
  738. const plugin = { id, data, raw, schema, version };
  739. settings = new Settings({ plugin, registry });
  740. expect(settings.isDisposed).toBe(false);
  741. settings.dispose();
  742. expect(settings.isDisposed).toBe(true);
  743. });
  744. });
  745. describe('#default()', () => {
  746. it('should return a fully extrapolated schema default', async () => {
  747. const id = 'omicron';
  748. const defaults = {
  749. foo: 'one',
  750. bar: 100,
  751. baz: {
  752. qux: 'two',
  753. quux: 'three',
  754. quuz: {
  755. corge: { grault: 200 }
  756. }
  757. }
  758. };
  759. connector.schemas[id] = {
  760. type: 'object',
  761. properties: {
  762. foo: { type: 'string', default: defaults.foo },
  763. bar: { type: 'number', default: defaults.bar },
  764. baz: {
  765. type: 'object',
  766. default: {},
  767. properties: {
  768. qux: { type: 'string', default: defaults.baz.qux },
  769. quux: { type: 'string', default: defaults.baz.quux },
  770. quuz: {
  771. type: 'object',
  772. default: {},
  773. properties: {
  774. corge: {
  775. type: 'object',
  776. default: {},
  777. properties: {
  778. grault: {
  779. type: 'number',
  780. default: defaults.baz.quuz.corge.grault
  781. }
  782. }
  783. }
  784. }
  785. }
  786. }
  787. },
  788. 'nonexistent-default': { type: 'string' }
  789. }
  790. };
  791. settings = (await registry.load(id)) as Settings;
  792. expect(settings.default('nonexistent-key')).toBeUndefined();
  793. expect(settings.default('foo')).toBe(defaults.foo);
  794. expect(settings.default('bar')).toBe(defaults.bar);
  795. expect(settings.default('baz')).toEqual(defaults.baz);
  796. expect(settings.default('nonexistent-default')).toBeUndefined();
  797. });
  798. });
  799. describe('#get()', () => {
  800. it('should get a setting item', async () => {
  801. const id = 'foo';
  802. const key = 'bar';
  803. const value = 'baz';
  804. connector.schemas[id] = { type: 'object' };
  805. await connector.save(id, JSON.stringify({ [key]: value }));
  806. settings = (await registry.load(id)) as Settings;
  807. const saved = settings.get(key);
  808. expect(saved.user).toBe(value);
  809. });
  810. it('should use schema default if user data not available', async () => {
  811. const id = 'alpha';
  812. const key = 'beta';
  813. const value = 'gamma';
  814. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  815. type: 'object',
  816. properties: {
  817. [key]: {
  818. type: typeof value as ISettingRegistry.Primitive,
  819. default: value
  820. }
  821. }
  822. });
  823. settings = (await registry.load(id)) as Settings;
  824. const saved = settings.get(key);
  825. expect(saved.composite).toBe(schema.properties![key].default);
  826. expect(saved.composite).not.toBe(saved.user);
  827. });
  828. it('should let user value override schema default', async () => {
  829. const id = 'alpha';
  830. const key = 'beta';
  831. const value = 'gamma';
  832. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  833. type: 'object',
  834. properties: {
  835. [key]: {
  836. type: typeof value as ISettingRegistry.Primitive,
  837. default: 'delta'
  838. }
  839. }
  840. });
  841. await connector.save(id, JSON.stringify({ [key]: value }));
  842. settings = (await registry.load(id)) as Settings;
  843. const saved = settings.get(key);
  844. expect(saved.composite).toBe(value);
  845. expect(saved.user).toBe(value);
  846. expect(saved.composite).not.toBe(schema.properties![key].default);
  847. expect(saved.user).not.toBe(schema.properties![key].default);
  848. });
  849. it('should be `undefined` if a key does not exist', async () => {
  850. const id = 'foo';
  851. const key = 'bar';
  852. connector.schemas[id] = { type: 'object' };
  853. settings = (await registry.load(id)) as Settings;
  854. const saved = settings.get(key);
  855. expect(saved.composite).toBeUndefined();
  856. expect(saved.user).toBeUndefined();
  857. });
  858. });
  859. describe('#remove()', () => {
  860. it('should remove a setting item', async () => {
  861. const id = 'foo';
  862. const key = 'bar';
  863. const value = 'baz';
  864. connector.schemas[id] = { type: 'object' };
  865. await connector.save(id, JSON.stringify({ [key]: value }));
  866. settings = (await registry.load(id)) as Settings;
  867. let saved = settings.get(key);
  868. expect(saved.user).toBe(value);
  869. await settings.remove(key);
  870. saved = settings.get(key);
  871. expect(saved.composite).toBeUndefined();
  872. expect(saved.user).toBeUndefined();
  873. });
  874. });
  875. describe('#save()', () => {
  876. it('should save user setting data', async () => {
  877. const id = 'foo';
  878. const one = 'one';
  879. const two = 'two';
  880. connector.schemas[id] = { type: 'object' };
  881. settings = (await registry.load(id)) as Settings;
  882. await settings.save(JSON.stringify({ one, two }));
  883. let saved = settings.get('one');
  884. expect(saved.composite).toBe(one);
  885. expect(saved.user).toBe(one);
  886. saved = settings.get('two');
  887. expect(saved.composite).toBe(two);
  888. expect(saved.user).toBe(two);
  889. });
  890. });
  891. describe('#set()', () => {
  892. it('should set a user setting item', async () => {
  893. const id = 'foo';
  894. const one = 'one';
  895. connector.schemas[id] = { type: 'object' };
  896. settings = (await registry.load(id)) as Settings;
  897. await settings.set('one', one);
  898. const saved = settings.get('one');
  899. expect(saved.composite).toBe(one);
  900. expect(saved.user).toBe(one);
  901. });
  902. });
  903. });
  904. });