validate.spec.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (c) Jupyter Development Team.
  2. import 'jest';
  3. import { Kernel, KernelMessage } from '../../src';
  4. import { validateMessage, validateModel } from '../../src/kernel/validate';
  5. describe('kernel/validate', () => {
  6. describe('validateMessage', () => {
  7. it('should pass a valid message', () => {
  8. const msg = KernelMessage.createMessage({
  9. msgType: 'comm_msg',
  10. channel: 'iopub',
  11. session: 'foo',
  12. content: { comm_id: 'foo', data: {} }
  13. });
  14. validateMessage(msg);
  15. });
  16. it('should throw if missing a field', () => {
  17. const msg = KernelMessage.createMessage({
  18. msgType: 'comm_msg',
  19. channel: 'iopub',
  20. session: 'baz',
  21. content: { comm_id: 'foo', data: {} }
  22. });
  23. delete msg.channel;
  24. expect(() => validateMessage(msg)).toThrowError();
  25. });
  26. it('should throw if a field is invalid', () => {
  27. const msg = KernelMessage.createMessage({
  28. msgType: 'comm_msg',
  29. channel: 'iopub',
  30. session: 'baz',
  31. content: { comm_id: 'foo', data: {} }
  32. });
  33. (msg as any).header.username = 1;
  34. expect(() => validateMessage(msg)).toThrowError();
  35. });
  36. it('should throw if the parent header is given an invalid', () => {
  37. const msg = KernelMessage.createMessage({
  38. msgType: 'comm_msg',
  39. channel: 'iopub',
  40. session: 'baz',
  41. content: { comm_id: 'foo', data: {} }
  42. });
  43. msg.parent_header = msg.header;
  44. (msg as any).parent_header.username = 1;
  45. expect(() => validateMessage(msg)).toThrowError();
  46. });
  47. it('should throw if the channel is not a string', () => {
  48. const msg = KernelMessage.createMessage({
  49. msgType: 'comm_msg',
  50. channel: 'iopub',
  51. session: 'baz',
  52. content: { comm_id: 'foo', data: {} }
  53. });
  54. (msg as any).channel = 1;
  55. expect(() => validateMessage(msg)).toThrowError();
  56. });
  57. it('should validate an iopub message', () => {
  58. const msg = KernelMessage.createMessage({
  59. msgType: 'comm_close',
  60. channel: 'iopub',
  61. session: 'baz',
  62. content: { comm_id: 'foo', data: {} }
  63. });
  64. validateMessage(msg);
  65. });
  66. it('should ignore on an unknown iopub message type', () => {
  67. const msg = KernelMessage.createMessage({
  68. msgType: 'foo',
  69. channel: 'iopub',
  70. session: 'baz',
  71. content: {}
  72. } as any);
  73. validateMessage(msg);
  74. });
  75. it('should throw on missing iopub message content', () => {
  76. const msg = KernelMessage.createMessage<KernelMessage.IErrorMsg>({
  77. msgType: 'error',
  78. channel: 'iopub',
  79. session: 'baz',
  80. content: {} as any
  81. } as any);
  82. expect(() => validateMessage(msg)).toThrowError();
  83. });
  84. it('should throw on invalid iopub message content', () => {
  85. const msg = KernelMessage.createMessage<KernelMessage.IClearOutputMsg>({
  86. msgType: 'clear_output',
  87. channel: 'iopub',
  88. session: 'baz',
  89. content: { wait: 1 as any }
  90. });
  91. expect(() => validateMessage(msg)).toThrowError();
  92. });
  93. it('should throw on invalid iopub status message content', () => {
  94. const msg = KernelMessage.createMessage<KernelMessage.IStatusMsg>({
  95. msgType: 'status',
  96. channel: 'iopub',
  97. session: 'baz',
  98. content: { execution_state: 'invalid-status' as Kernel.Status }
  99. });
  100. expect(() => validateMessage(msg)).toThrowError();
  101. });
  102. it('should handle no buffers field', () => {
  103. const msg = KernelMessage.createMessage({
  104. msgType: 'comm_msg',
  105. channel: 'iopub',
  106. session: 'foo',
  107. content: { comm_id: 'foo', data: {} }
  108. });
  109. delete msg['buffers'];
  110. validateMessage(msg);
  111. });
  112. });
  113. describe('#validateModel()', () => {
  114. it('should pass a valid id', () => {
  115. const id: Kernel.IModel = { name: 'foo', id: 'baz' };
  116. validateModel(id);
  117. });
  118. });
  119. });