@@ -2,13 +2,15 @@ import { describe, expect, it } from "vite-plus/test";
22
33import {
44 getThemeColorsForMode ,
5+ parseThemeFile ,
56 themeColorToHex ,
67 THEME_FILE_VERSION ,
78 toCanonicalThemeColor ,
89} from "./themePalette" ;
910import {
1011 isVsCodeThemeFile ,
1112 pairVsCodeThemes ,
13+ parseThemeImport ,
1214 parseVsCodeThemeFile ,
1315 resolveThemeLabelCollisions ,
1416} from "./vscodeThemeImport" ;
@@ -63,6 +65,59 @@ const VSCODE_DARK = {
6365 tokenColors : [ ] ,
6466} ;
6567
68+ describe ( "theme import text" , ( ) => {
69+ it ( "preserves conversion of ordinary VS Code JSON" , ( ) => {
70+ expect ( parseThemeImport ( JSON . stringify ( VSCODE_DARK ) ) ) . toEqual (
71+ parseVsCodeThemeFile ( VSCODE_DARK ) ,
72+ ) ;
73+ } ) ;
74+
75+ it . each ( [
76+ '{ // line comment\n "name": "Test", "colors": { "editor.background": "#002b36" } }' ,
77+ '{ /* block comment */ "name": "Test", "colors": { "editor.background": "#002b36" } }' ,
78+ '{ "name": "Test", "colors": { "editor.background": "#002b36", }, "tokenColors": [ { "scope": ["comment",], "settings": {}, }, ], }' ,
79+ ] ) ( "imports VS Code JSONC without changing conversion: %s" , ( source ) => {
80+ const theme = parseThemeImport ( source ) ;
81+ expect ( theme . label ) . toBe ( "Test" ) ;
82+ expect ( theme . appearance ) . toBe ( "dark" ) ;
83+ expect ( asHex ( theme . colors . canvas ) ) . toBe ( "#002b36" ) ;
84+ } ) ;
85+
86+ it . each ( [
87+ "" ,
88+ '{ "name": "Test", "colors": { "editor.background": "#002b36" }' ,
89+ '{ "name": "Test" "colors": { "editor.background": "#002b36" } }' ,
90+ '{ "name": "Test", "colors": { "editor.background": "#002b36" } } /* unterminated' ,
91+ '{ "name": "Test", "colors": { "editor.background": "#002b36" } } extra' ,
92+ ] ) ( "rejects malformed input rather than using parser recovery: %s" , ( source ) => {
93+ expect ( ( ) => parseThemeImport ( source ) ) . toThrow ( "That theme file is not valid JSON." ) ;
94+ } ) ;
95+
96+ it ( "preserves native T3 theme parsing and strict JSON syntax" , ( ) => {
97+ const nativeTheme = {
98+ version : THEME_FILE_VERSION ,
99+ name : "Native theme" ,
100+ appearance : "dark" ,
101+ colors : { canvas : "#002b36" } ,
102+ } ;
103+ const source = JSON . stringify ( nativeTheme ) ;
104+ expect ( parseThemeImport ( source ) ) . toEqual ( parseThemeFile ( nativeTheme ) ) ;
105+ expect ( ( ) => parseThemeImport ( source . replace ( "{" , "{ /* native theme */" ) ) ) . toThrow ( ) ;
106+ expect ( ( ) => parseThemeImport ( source . slice ( 0 , - 1 ) + ",}" ) ) . toThrow ( ) ;
107+ } ) ;
108+
109+ it ( "still validates theme structure after parsing JSONC" , ( ) => {
110+ expect ( ( ) =>
111+ parseThemeImport ( '{ /* not a theme */ "colors": { "editor.foreground": "#fff" } }' ) ,
112+ ) . toThrow ( 'no "editor.background"' ) ;
113+ expect ( ( ) =>
114+ parseThemeImport (
115+ '{ "version": 1, "name": "Bad native", "appearance": "dark", "colors": { "unknown": "#fff" } }' ,
116+ ) ,
117+ ) . toThrow ( '"unknown" is not a supported theme color role.' ) ;
118+ } ) ;
119+ } ) ;
120+
66121describe ( "VS Code theme import" , ( ) => {
67122 it ( "recognises workbench themes and rejects our own files" , ( ) => {
68123 expect ( isVsCodeThemeFile ( VSCODE_DARK ) ) . toBe ( true ) ;
0 commit comments