settingregistry.spec.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. // Copyright (c) Jupyter Development Team.
  2. import 'jest';
  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('Settings', () => {
  339. const connector = new TestConnector();
  340. let registry: SettingRegistry;
  341. let settings: Settings | null;
  342. afterEach(() => {
  343. if (settings) {
  344. settings.dispose();
  345. settings = null;
  346. }
  347. connector.schemas = {};
  348. return connector.clear();
  349. });
  350. beforeEach(() => {
  351. registry = new SettingRegistry({ connector });
  352. });
  353. describe('#constructor()', () => {
  354. it('should create a new settings object for a plugin', () => {
  355. const id = 'alpha';
  356. const data = { composite: {}, user: {} };
  357. const schema: ISettingRegistry.ISchema = { type: 'object' };
  358. const raw = '{ }';
  359. const version = 'test';
  360. const plugin = { id, data, raw, schema, version };
  361. settings = new Settings({ plugin, registry });
  362. expect(settings).toBeInstanceOf(Settings);
  363. });
  364. });
  365. describe('#changed', () => {
  366. it('should emit when a plugin changes', async () => {
  367. const id = 'alpha';
  368. const schema: ISettingRegistry.ISchema = { type: 'object' };
  369. connector.schemas[id] = schema;
  370. settings = (await registry.load(id)) as Settings;
  371. const promise = signalToPromise(settings.changed);
  372. await settings.set('foo', 'bar');
  373. await promise;
  374. });
  375. });
  376. describe('#composite', () => {
  377. it('should contain the merged user and default data', async () => {
  378. const id = 'alpha';
  379. const key = 'beta';
  380. const value = 'gamma';
  381. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  382. type: 'object',
  383. properties: {
  384. [key]: {
  385. type: typeof value as ISettingRegistry.Primitive,
  386. default: value
  387. }
  388. }
  389. });
  390. connector.schemas[id] = schema;
  391. settings = (await registry.load(id)) as Settings;
  392. expect(settings.composite[key]).toBe(value);
  393. });
  394. it('should privilege user data', async () => {
  395. const id = 'alpha';
  396. const key = 'beta';
  397. const value = 'gamma';
  398. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  399. type: 'object',
  400. properties: {
  401. [key]: {
  402. type: typeof value as ISettingRegistry.Primitive,
  403. default: 'delta'
  404. }
  405. }
  406. });
  407. connector.schemas[id] = schema;
  408. settings = (await registry.load(id)) as Settings;
  409. await settings.set(key, value);
  410. expect(settings.composite[key]).toBe(value);
  411. });
  412. });
  413. describe('#id', () => {
  414. it('should expose the plugin ID', () => {
  415. const id = 'alpha';
  416. const data = { composite: {}, user: {} };
  417. const schema: ISettingRegistry.ISchema = { type: 'object' };
  418. const raw = '{ }';
  419. const version = 'test';
  420. const plugin = { id, data, raw, schema, version };
  421. settings = new Settings({ plugin, registry });
  422. expect(settings.id).toBe(id);
  423. });
  424. });
  425. describe('#isDisposed', () => {
  426. it('should test whether the settings object is disposed', () => {
  427. const id = 'alpha';
  428. const data = { composite: {}, user: {} };
  429. const schema: ISettingRegistry.ISchema = { type: 'object' };
  430. const raw = '{ }';
  431. const version = 'test';
  432. const plugin = { id, data, raw, schema, version };
  433. settings = new Settings({ plugin, registry });
  434. expect(settings.isDisposed).toBe(false);
  435. settings.dispose();
  436. expect(settings.isDisposed).toBe(true);
  437. });
  438. });
  439. describe('#schema', () => {
  440. it('should expose the plugin schema', async () => {
  441. const id = 'alpha';
  442. const schema: ISettingRegistry.ISchema = { type: 'object' };
  443. connector.schemas[id] = schema;
  444. settings = (await registry.load(id)) as Settings;
  445. expect(settings.schema).toEqual(schema);
  446. });
  447. });
  448. describe('#user', () => {
  449. it('should privilege user data', async () => {
  450. const id = 'alpha';
  451. const key = 'beta';
  452. const value = 'gamma';
  453. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  454. type: 'object',
  455. properties: {
  456. [key]: {
  457. type: typeof value as ISettingRegistry.Primitive,
  458. default: 'delta'
  459. }
  460. }
  461. });
  462. connector.schemas[id] = schema;
  463. settings = (await registry.load(id)) as Settings;
  464. await settings.set(key, value);
  465. expect(settings.user[key]).toBe(value);
  466. });
  467. });
  468. describe('#registry', () => {
  469. it('should expose the setting registry', () => {
  470. const id = 'alpha';
  471. const data = { composite: {}, user: {} };
  472. const schema: ISettingRegistry.ISchema = { type: 'object' };
  473. const raw = '{ }';
  474. const version = 'test';
  475. const plugin = { id, data, raw, schema, version };
  476. settings = new Settings({ plugin, registry });
  477. expect(settings.registry).toBe(registry);
  478. });
  479. });
  480. describe('#dispose()', () => {
  481. it('should dispose the settings object', () => {
  482. const id = 'alpha';
  483. const data = { composite: {}, user: {} };
  484. const schema: ISettingRegistry.ISchema = { type: 'object' };
  485. const raw = '{ }';
  486. const version = 'test';
  487. const plugin = { id, data, raw, schema, version };
  488. settings = new Settings({ plugin, registry });
  489. expect(settings.isDisposed).toBe(false);
  490. settings.dispose();
  491. expect(settings.isDisposed).toBe(true);
  492. });
  493. });
  494. describe('#default()', () => {
  495. it('should return a fully extrapolated schema default', async () => {
  496. const id = 'omicron';
  497. const defaults = {
  498. foo: 'one',
  499. bar: 100,
  500. baz: {
  501. qux: 'two',
  502. quux: 'three',
  503. quuz: {
  504. corge: { grault: 200 }
  505. }
  506. }
  507. };
  508. connector.schemas[id] = {
  509. type: 'object',
  510. properties: {
  511. foo: { type: 'string', default: defaults.foo },
  512. bar: { type: 'number', default: defaults.bar },
  513. baz: {
  514. type: 'object',
  515. default: {},
  516. properties: {
  517. qux: { type: 'string', default: defaults.baz.qux },
  518. quux: { type: 'string', default: defaults.baz.quux },
  519. quuz: {
  520. type: 'object',
  521. default: {},
  522. properties: {
  523. corge: {
  524. type: 'object',
  525. default: {},
  526. properties: {
  527. grault: {
  528. type: 'number',
  529. default: defaults.baz.quuz.corge.grault
  530. }
  531. }
  532. }
  533. }
  534. }
  535. }
  536. },
  537. 'nonexistent-default': { type: 'string' }
  538. }
  539. };
  540. settings = (await registry.load(id)) as Settings;
  541. expect(settings.default('nonexistent-key')).toBeUndefined();
  542. expect(settings.default('foo')).toBe(defaults.foo);
  543. expect(settings.default('bar')).toBe(defaults.bar);
  544. expect(settings.default('baz')).toEqual(defaults.baz);
  545. expect(settings.default('nonexistent-default')).toBeUndefined();
  546. });
  547. });
  548. describe('#get()', () => {
  549. it('should get a setting item', async () => {
  550. const id = 'foo';
  551. const key = 'bar';
  552. const value = 'baz';
  553. connector.schemas[id] = { type: 'object' };
  554. await connector.save(id, JSON.stringify({ [key]: value }));
  555. settings = (await registry.load(id)) as Settings;
  556. const saved = settings.get(key);
  557. expect(saved.user).toBe(value);
  558. });
  559. it('should use schema default if user data not available', async () => {
  560. const id = 'alpha';
  561. const key = 'beta';
  562. const value = 'gamma';
  563. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  564. type: 'object',
  565. properties: {
  566. [key]: {
  567. type: typeof value as ISettingRegistry.Primitive,
  568. default: value
  569. }
  570. }
  571. });
  572. settings = (await registry.load(id)) as Settings;
  573. const saved = settings.get(key);
  574. expect(saved.composite).toBe(schema.properties![key].default);
  575. expect(saved.composite).not.toBe(saved.user);
  576. });
  577. it('should let user value override schema default', async () => {
  578. const id = 'alpha';
  579. const key = 'beta';
  580. const value = 'gamma';
  581. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  582. type: 'object',
  583. properties: {
  584. [key]: {
  585. type: typeof value as ISettingRegistry.Primitive,
  586. default: 'delta'
  587. }
  588. }
  589. });
  590. await connector.save(id, JSON.stringify({ [key]: value }));
  591. settings = (await registry.load(id)) as Settings;
  592. const saved = settings.get(key);
  593. expect(saved.composite).toBe(value);
  594. expect(saved.user).toBe(value);
  595. expect(saved.composite).not.toBe(schema.properties![key].default);
  596. expect(saved.user).not.toBe(schema.properties![key].default);
  597. });
  598. it('should be `undefined` if a key does not exist', async () => {
  599. const id = 'foo';
  600. const key = 'bar';
  601. connector.schemas[id] = { type: 'object' };
  602. settings = (await registry.load(id)) as Settings;
  603. const saved = settings.get(key);
  604. expect(saved.composite).toBeUndefined();
  605. expect(saved.user).toBeUndefined();
  606. });
  607. });
  608. describe('#remove()', () => {
  609. it('should remove a setting item', async () => {
  610. const id = 'foo';
  611. const key = 'bar';
  612. const value = 'baz';
  613. connector.schemas[id] = { type: 'object' };
  614. await connector.save(id, JSON.stringify({ [key]: value }));
  615. settings = (await registry.load(id)) as Settings;
  616. let saved = settings.get(key);
  617. expect(saved.user).toBe(value);
  618. await settings.remove(key);
  619. saved = settings.get(key);
  620. expect(saved.composite).toBeUndefined();
  621. expect(saved.user).toBeUndefined();
  622. });
  623. });
  624. describe('#save()', () => {
  625. it('should save user setting data', async () => {
  626. const id = 'foo';
  627. const one = 'one';
  628. const two = 'two';
  629. connector.schemas[id] = { type: 'object' };
  630. settings = (await registry.load(id)) as Settings;
  631. await settings.save(JSON.stringify({ one, two }));
  632. let saved = settings.get('one');
  633. expect(saved.composite).toBe(one);
  634. expect(saved.user).toBe(one);
  635. saved = settings.get('two');
  636. expect(saved.composite).toBe(two);
  637. expect(saved.user).toBe(two);
  638. });
  639. });
  640. describe('#set()', () => {
  641. it('should set a user setting item', async () => {
  642. const id = 'foo';
  643. const one = 'one';
  644. connector.schemas[id] = { type: 'object' };
  645. settings = (await registry.load(id)) as Settings;
  646. await settings.set('one', one);
  647. const saved = settings.get('one');
  648. expect(saved.composite).toBe(one);
  649. expect(saved.user).toBe(one);
  650. });
  651. });
  652. });
  653. });