Skip to content

Commit 51df4a0

Browse files
committed
test: add simple test
1 parent 210c97d commit 51df4a0

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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+
});

packages/myst-compat/src/downgrade/version_2_1.ts

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Parent } from 'mdast';
22
import type { Outputs as Outputs2, Output as Output2 } from '../types/v2.js';
33
import type { Output as Output1 } from '../types/v1.js';
44
import { visit, SKIP } from 'unist-util-visit';
5+
import { squeeze } from '../utils.js';
56

67
export function downgrade(ast: Parent) {
78
visit(ast as any, 'outputs', (node: Outputs2, index: number | null, parent: Parent | null) => {
@@ -20,6 +21,9 @@ export function downgrade(ast: Parent) {
2021
id,
2122
_future_ast: structuredClone(node),
2223
};
24+
// Drop any undefined members (assume all members are optional if undefined)
25+
squeeze(nextOutput);
26+
2327
if (parent) {
2428
parent.children[index!] = nextOutput as any;
2529
}

packages/myst-compat/src/utils.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function squeeze<T>(record: Record<string, T | unknown>) {
2+
Object.keys(record).forEach((key) => {
3+
if (record[key] === undefined) {
4+
delete record[key];
5+
}
6+
});
7+
}

0 commit comments

Comments
 (0)