|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { downgrade } from './version_2_1.js'; |
| 3 | +import type { Parent } from 'mdast'; |
| 4 | +import { warn } from 'console'; |
| 5 | + |
| 6 | +const SIMPLE_AST: Parent = { |
| 7 | + type: 'root', |
| 8 | + children: [ |
| 9 | + { |
| 10 | + // @ts-expect-error: unknown type |
| 11 | + type: 'block', |
| 12 | + children: [ |
| 13 | + { |
| 14 | + // @ts-expect-error: invalid child type |
| 15 | + type: 'paragraph', |
| 16 | + children: [ |
| 17 | + { |
| 18 | + type: 'text', |
| 19 | + value: 'This is an ', |
| 20 | + }, |
| 21 | + { |
| 22 | + type: 'emphasis', |
| 23 | + children: [ |
| 24 | + { |
| 25 | + type: 'text', |
| 26 | + value: 'interesting', |
| 27 | + }, |
| 28 | + ], |
| 29 | + }, |
| 30 | + { |
| 31 | + type: 'text', |
| 32 | + value: ' file. See ', |
| 33 | + }, |
| 34 | + { |
| 35 | + type: 'link', |
| 36 | + url: '#other', |
| 37 | + children: [], |
| 38 | + // @ts-expect-error: unknown field |
| 39 | + urlSource: '#other', |
| 40 | + }, |
| 41 | + { |
| 42 | + type: 'text', |
| 43 | + value: ' for more.', |
| 44 | + }, |
| 45 | + ], |
| 46 | + }, |
| 47 | + { |
| 48 | + // @ts-expect-error: invalid child type |
| 49 | + type: 'code', |
| 50 | + lang: 'python', |
| 51 | + value: 'Some code', |
| 52 | + }, |
| 53 | + ], |
| 54 | + }, |
| 55 | + ], |
| 56 | +}; |
| 57 | + |
| 58 | +const SIMPLE_V2_AST_WITH_OUTPUT: Parent = { |
| 59 | + type: 'root', |
| 60 | + children: [ |
| 61 | + { |
| 62 | + // @ts-expect-error: unknown type |
| 63 | + type: 'block', |
| 64 | + children: [ |
| 65 | + { |
| 66 | + // @ts-expect-error: invalid child type |
| 67 | + type: 'outputs', |
| 68 | + id: 'abc123', |
| 69 | + children: [ |
| 70 | + { |
| 71 | + // @ts-expect-error: invalid child type |
| 72 | + type: 'output', |
| 73 | + children: [], |
| 74 | + jupyter_data: { |
| 75 | + output_type: 'display_data', |
| 76 | + execution_count: 3, |
| 77 | + metadata: {}, |
| 78 | + data: { |
| 79 | + 'application/octet-stream': { |
| 80 | + content_type: 'application/octet-stream', |
| 81 | + hash: 'def456', |
| 82 | + path: '/my/path/def456.png', |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + ], |
| 88 | + }, |
| 89 | + ], |
| 90 | + }, |
| 91 | + ], |
| 92 | +}; |
| 93 | + |
| 94 | +const SIMPLE_V1_AST_WITH_OUTPUT: Parent = { |
| 95 | + type: 'root', |
| 96 | + children: [ |
| 97 | + { |
| 98 | + // @ts-expect-error: unknown type |
| 99 | + type: 'block', |
| 100 | + children: [ |
| 101 | + { |
| 102 | + // @ts-expect-error: invalid child type |
| 103 | + type: 'output', |
| 104 | + id: 'abc123', |
| 105 | + // @ts-expect-error: invalid type |
| 106 | + data: [ |
| 107 | + { |
| 108 | + output_type: 'display_data', |
| 109 | + execution_count: 3, |
| 110 | + metadata: {}, |
| 111 | + data: { |
| 112 | + 'application/octet-stream': { |
| 113 | + content_type: 'application/octet-stream', |
| 114 | + hash: 'def456', |
| 115 | + path: '/my/path/def456.png', |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + ], |
| 120 | + children: [], |
| 121 | + _future_ast: { |
| 122 | + type: 'outputs', |
| 123 | + id: 'abc123', |
| 124 | + children: [ |
| 125 | + { |
| 126 | + type: 'output', |
| 127 | + children: [], |
| 128 | + jupyter_data: { |
| 129 | + output_type: 'display_data', |
| 130 | + execution_count: 3, |
| 131 | + metadata: {}, |
| 132 | + data: { |
| 133 | + 'application/octet-stream': { |
| 134 | + content_type: 'application/octet-stream', |
| 135 | + hash: 'def456', |
| 136 | + path: '/my/path/def456.png', |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + ], |
| 142 | + }, |
| 143 | + }, |
| 144 | + ], |
| 145 | + }, |
| 146 | + ], |
| 147 | +}; |
| 148 | + |
| 149 | +describe('downgrade 2->1', () => { |
| 150 | + it('leaves a simple AST unchanged', () => { |
| 151 | + const ast = structuredClone(SIMPLE_AST); |
| 152 | + downgrade(ast); |
| 153 | + expect(ast).toStrictEqual(SIMPLE_AST); |
| 154 | + }); |
| 155 | + it('downgrades a v2 AST with outputs', () => { |
| 156 | + const ast = structuredClone(SIMPLE_V2_AST_WITH_OUTPUT); |
| 157 | + downgrade(ast); |
| 158 | + expect(ast).toStrictEqual(SIMPLE_V1_AST_WITH_OUTPUT); |
| 159 | + }); |
| 160 | +}); |
0 commit comments